c++问题(需要完整代码)求大神
实验要求:类的继承1.定义vehicle类数据成员:整型MaxSpeed和Weight成员函数:默认构造函数和带形参的构造函数,函数体内"Thevehiclehasbee...
实验要求:
类的继承
1. 定义vehicle类
数据成员:整型MaxSpeed和Weight
成员函数:
默认构造函数和带形参的构造函数,函数体内"The vehicle has been constructed";
定义复制构造函数;
析构函数函数体内"The vehicle has been deconstructed";
void print() 输出数据;
void Run() 输出"The vehicle is running!";
void Stop() 输出"The vehicle has stopped!"
2. 定义motorcar类,公有继承vehicle
数据成员:整型gas
成员函数:
默认构造函数和带形参的构造函数,函数体内"The motorcar has been constructed";
定义复制构造函数;
析构函数函数体内"The motorcar has been deconstructed";
void print() 输出数据(调用vehicle类的print()输出MaxSpeed和Weight);
void Run() 输出"The motorcar is running!";
void Stop() 输出"The motorcar has stopped!"
3. 定义motorcycle类,公有继承bicycle和motorcar类
数据成员:无
成员函数:
默认构造函数和带形参的构造函数,函数体内"The motorcycle has been constructed";
定义复制构造函数;
析构函数函数体内"The motorcycle has been deconstructed";
void print() 输出数据(调用vehicle类的print()输出MaxSpeed和Weight,调用motorcar类的print()输出gas);
void Run() 输出"The motorcycle is running!";
void Stop() 输出"The motorcycle has stopped!"
4. 参照P264定义fun函数,参数为vehicle类的指针,通过指针调用run()函数
5. 主程序内声明vehicle类的指针,定义3个类的对象,分别将指针指向3个对象后调用fun函数
6. 每个类对象调用print()输出数据值 展开
类的继承
1. 定义vehicle类
数据成员:整型MaxSpeed和Weight
成员函数:
默认构造函数和带形参的构造函数,函数体内"The vehicle has been constructed";
定义复制构造函数;
析构函数函数体内"The vehicle has been deconstructed";
void print() 输出数据;
void Run() 输出"The vehicle is running!";
void Stop() 输出"The vehicle has stopped!"
2. 定义motorcar类,公有继承vehicle
数据成员:整型gas
成员函数:
默认构造函数和带形参的构造函数,函数体内"The motorcar has been constructed";
定义复制构造函数;
析构函数函数体内"The motorcar has been deconstructed";
void print() 输出数据(调用vehicle类的print()输出MaxSpeed和Weight);
void Run() 输出"The motorcar is running!";
void Stop() 输出"The motorcar has stopped!"
3. 定义motorcycle类,公有继承bicycle和motorcar类
数据成员:无
成员函数:
默认构造函数和带形参的构造函数,函数体内"The motorcycle has been constructed";
定义复制构造函数;
析构函数函数体内"The motorcycle has been deconstructed";
void print() 输出数据(调用vehicle类的print()输出MaxSpeed和Weight,调用motorcar类的print()输出gas);
void Run() 输出"The motorcycle is running!";
void Stop() 输出"The motorcycle has stopped!"
4. 参照P264定义fun函数,参数为vehicle类的指针,通过指针调用run()函数
5. 主程序内声明vehicle类的指针,定义3个类的对象,分别将指针指向3个对象后调用fun函数
6. 每个类对象调用print()输出数据值 展开
3个回答
展开全部
这题目有点问题,motorcar的print函数是输出基类的数据还是输出gas?不过程序给你了。
//头文件
#include<iostream>
using namespace std;
class vehicle
{
int MaxSpeed;
int Weight;
public:
vehicle():MaxSpeed(10),Weight(60){cout<<"The vehicle has been constructed"<<endl;}
vehicle(int Maxspeed,int Weight){cout<<"The vehicle has been constructed"<<endl;};
vehicle(const vehicle &V)
{
MaxSpeed=V.MaxSpeed;
Weight=V.Weight;
}
~vehicle(){cout<<"The vehicle has been deconstructed"<<endl;};
void print();
virtual void Run();
void Stop();
};
class motorcar : public vehicle
{
int gas;
public:
motorcar():gas(30){cout<<"The motorcar has been constructed"<<endl;}
motorcar(int gas){cout<<"The motorcar has been constructed"<<endl;}
motorcar(const motorcar &M)
{
gas=M.gas;
}
~motorcar(){cout<<"The motorcar has been deconstructed"<<endl;}
void print();
void Run();
void Stop();
};
class motorcycle:public vehicle,public motorcar
{
public:
motorcycle(){cout<<"The motorcycle has been constructed"<<endl;}
motorcycle(const motorcycle &M);
~motorcycle(){cout<<"The motorcycle has been deconstructed"<<endl;}
void print();
void Run();
void Stop();
};
// 实现函数
#include<iostream>
#include"vehicle.h"
using namespace std;
void vehicle::print()
{
cout<<MaxSpeed<<'\n'<<Weight<<endl;
}
void vehicle::Run()
{
cout<<"The vehicle is running!"<<endl;;
}
void vehicle::Stop()
{
cout<<"The vehicle has stopped"<<endl;
}
void motorcar::print()
{
vehicle::print();
cout<<this->gas<<endl;
}
void motorcar::Run()
{
cout<<"The motorcar is running!"<<endl;
}
void motorcar::Stop()
{
cout<<"The motorcar has stopped!"<<endl;
}
void motorcycle::print()
{
vehicle::print();
motorcar::print();
}
void motorcycle::Run()
{
cout<<"The motorcycle is running!"<<endl;
}
void motorcycle::Stop()
{
cout<<"The motorcycle has stopped!"<<endl;
}
void fun(vehicle *pv)
{
pv->Run();
return ;
}
//主函数
#include<iostream>
#include"vehicle.h"
using namespace std;
extern void fun(vehicle *);
int main()
{
vehicle v;
motorcar m;
motorcycle M;
fun(&v);
fun(&m);
fun(&M);
v.print();
m.print();
M.print();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询