跪求C++派生类的两个程序。。。。。
1.定义一个哺乳动物类Mammal,有体重weight和年龄age两个成员变量,再由此派生出狗类Dog,有新成员color,定义有参数的基类和派生类的构造函数,在main...
1. 定义一个哺乳动物类Mammal, 有体重weight和年龄age两个成员变量, 再由此派生出狗类Dog, 有新成员color, 定义有参数的基类和派生类的构造函数,在main中使用Dog类。
2. 定义一个车辆(vehicle)基类,具有MaxSpeed等成员变量,Run和Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车类有高度(height)等属性,汽车类有座位数(Seat)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类。在main中使用这三个类,注意使用虚基类。 (每句后面有注释最好,答对给分!!人格担保!!!1) 展开
2. 定义一个车辆(vehicle)基类,具有MaxSpeed等成员变量,Run和Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车类有高度(height)等属性,汽车类有座位数(Seat)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类。在main中使用这三个类,注意使用虚基类。 (每句后面有注释最好,答对给分!!人格担保!!!1) 展开
2个回答
展开全部
class Mammal
{
public:
Mammal(float w,int a)
{weight=w;age=a;}
protected:
float weight;
int age;
};
class Dog:public Mammal{
public:
Dog(float w,int a,int c)
{weight=w;age=a;color=c;}
void print()
{cout<<weight<<age<<c;}
protected:
int color;
};
void main()
{
Dog dog(2.4,2,23);
dog.print();
} //这一个简单,自己看吧
class vehicle{
public:
vehicle(float spd=0);
virtual void run(float s);
virtual void Stop()=0;// 这是抽象类,不能定义对象。
protected:
float MaxSpeed;
float speed;
};
class bicycle:virtual public vehicle{
//bicycle为从vehicle继承的虚基类
public:
bicycle(){height=80; speed=60;} }
void run(){cout<<"bicycle run"<<speed;} //虚函数
void Stop(){speed=0;} //虚函数
protected:
float height;
};
class motorcar:virtual public vehicle{
public:
motorcar(){Seat=30;speed=200;} }
void run(){cout<<"car run"<<speed;}
void Stop(){speed=0;}
protected:
int Seat;
};
class motorcycl:public bicycl,motorcar{
public:
void run(){cout<<"motorcycl run"<<speed;}
void Stop(){speed=0;}
};
void main()
{ bicycl b;motorcar m;motorcycl mc;
b.Stop();cout<<b.speed<<endl;
m.Stop();cout<<m.speed<<endl;
mc.Stop();cout<<mc.speed<<endl;
}
另外你的题目要求说的不是很清楚啊,主函数要怎么用到这三个类呢?
{
public:
Mammal(float w,int a)
{weight=w;age=a;}
protected:
float weight;
int age;
};
class Dog:public Mammal{
public:
Dog(float w,int a,int c)
{weight=w;age=a;color=c;}
void print()
{cout<<weight<<age<<c;}
protected:
int color;
};
void main()
{
Dog dog(2.4,2,23);
dog.print();
} //这一个简单,自己看吧
class vehicle{
public:
vehicle(float spd=0);
virtual void run(float s);
virtual void Stop()=0;// 这是抽象类,不能定义对象。
protected:
float MaxSpeed;
float speed;
};
class bicycle:virtual public vehicle{
//bicycle为从vehicle继承的虚基类
public:
bicycle(){height=80; speed=60;} }
void run(){cout<<"bicycle run"<<speed;} //虚函数
void Stop(){speed=0;} //虚函数
protected:
float height;
};
class motorcar:virtual public vehicle{
public:
motorcar(){Seat=30;speed=200;} }
void run(){cout<<"car run"<<speed;}
void Stop(){speed=0;}
protected:
int Seat;
};
class motorcycl:public bicycl,motorcar{
public:
void run(){cout<<"motorcycl run"<<speed;}
void Stop(){speed=0;}
};
void main()
{ bicycl b;motorcar m;motorcycl mc;
b.Stop();cout<<b.speed<<endl;
m.Stop();cout<<m.speed<<endl;
mc.Stop();cout<<mc.speed<<endl;
}
另外你的题目要求说的不是很清楚啊,主函数要怎么用到这三个类呢?
展开全部
大概写写
1、
class Mammal
{
public:
Mammal(float w,int a){weight=w;age=a;}
protected:
float weight;
int age;
};
class Dog:public Mammal{
public:
Dog(float w,int a,int c){weight=w;age=a;color=c;}
void print(){cout<<weight<<age<<c;}
protected:
int color;
};
void main()
{
Dog do1(2.4,2,23);
do1.print();
}
2\
class vehicle{
protected:
float MaxSpeed;
float speed; //当前速度,自己加的
public:
virtual void run(float s){speed=s;}
virtual void Stop(){speed=0;}
};
class bicycle:public vehicle{
protected:
float height;
public:
void seth(float h){height=h;}
void run(float s){cout<<"bicycle";speed=s;}
void Stop(){speed=0;}
};
class motorcat:public vehicle{
protected:
int Seat;
public:
void sets(int s){Seat=s;}
void run(float s){cout<<"car";speed=s;}
void Stop(){speed=0;}
};
class motorcycl:public bicycl,motorcar{
};
//虚基类和虚函数有点混淆,大概是这样,自己写写吧
1、
class Mammal
{
public:
Mammal(float w,int a){weight=w;age=a;}
protected:
float weight;
int age;
};
class Dog:public Mammal{
public:
Dog(float w,int a,int c){weight=w;age=a;color=c;}
void print(){cout<<weight<<age<<c;}
protected:
int color;
};
void main()
{
Dog do1(2.4,2,23);
do1.print();
}
2\
class vehicle{
protected:
float MaxSpeed;
float speed; //当前速度,自己加的
public:
virtual void run(float s){speed=s;}
virtual void Stop(){speed=0;}
};
class bicycle:public vehicle{
protected:
float height;
public:
void seth(float h){height=h;}
void run(float s){cout<<"bicycle";speed=s;}
void Stop(){speed=0;}
};
class motorcat:public vehicle{
protected:
int Seat;
public:
void sets(int s){Seat=s;}
void run(float s){cout<<"car";speed=s;}
void Stop(){speed=0;}
};
class motorcycl:public bicycl,motorcar{
};
//虚基类和虚函数有点混淆,大概是这样,自己写写吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询