编写一个程序:设计一个汽车类,数据成员有轮子个数、车重。小车类是汽车类的私有...
#include<iostream>using namespace std;
class drive
{
private:
int wheel;
double weight;
public:
drive(int a,double b):wheel(a),weight(b)
{
cin>>a>>b;
wheel=a;weight=b;
}
};
class car:private drive
{
private:
double weights;
public:
car(int a,double b,double c)
{
drive(a,b);
cin>>c;
weights=c;
}
void print();
};
class truck:private drive
{
private:
double weighting;
public:
truck(int a,double b,double d)
{
cin>>a>>b;
drive(a,b);
cin>>d;
weighting=d;
}
void print1();
};
int main()
{
car s1(0.0);
truck s2(0,0.0,0.0);
s1.print();
s2.print1();
return 0;
}
void car::print()
{
cout<<"数据为:"<<endl;
cout<<drive(wheel,weight))<<endl;
cout<<weights;
}
void truck::print1()
{
cout<<"数据为:"<<endl;
cout<<drive(wheel,weight)<<endl;
cout<<weighting;
}
这个程序哪里有错啊!!!{:10_277:} 感觉都是问题啊,建议楼主把构造函数的基本定义好好梳理几遍。
像
car(int a,double b,double c)
{
drive(a,b);
cin>>c;
weights=c;
}
你定义了3个形参。
在main函数中
car s1(0.0);
你只给了1个实参。
再说最开头的drive类,你在构造函数的初始化列表中已经将a,b赋值给了成员变量,为什么还要在函数主体内又输入a,b值。
页:
[1]