C++问题,急求解答!!! 50
1.先设计一个vehicle基类,包含Run()和Stop()等成员函数,由此vehicle基类派生出bicycle类和motocar类,并由这两个派生类继续派生出mot...
1. 先设计一个vehicle基类,包含Run()和Stop()等成员函数,由此vehicle基类派生出bicycle类和motocar类,并由这两个派生类继续派生出motocycle类,每个派生类中都有自己的与基类中功能不同的Run()和Stop()成员函数。
要求:
(1)在主函数中分别声明vehicle类、bicycle类、motocar类、motocycle类的对象,通过这些对象调用Run()和Stop()成员函数,要求每个对象能够调用对象所属派生类自己的成员函数。
(2)声明vehicle类的指针,让此对象指针依次指向vehicle类、bicycle类、motocar类、motocycle类的对象,并同时用该对象指针调用调用Run()成员函数。 展开
要求:
(1)在主函数中分别声明vehicle类、bicycle类、motocar类、motocycle类的对象,通过这些对象调用Run()和Stop()成员函数,要求每个对象能够调用对象所属派生类自己的成员函数。
(2)声明vehicle类的指针,让此对象指针依次指向vehicle类、bicycle类、motocar类、motocycle类的对象,并同时用该对象指针调用调用Run()成员函数。 展开
2个回答
展开全部
public abstract class vechicle
{
public virtual float Run(folat distance)
{
return distance/speed;
}
public virtual void Stop(bool color)
{
if (color ==true) cout<<"stop";
}
}
public class bicycle:vehicle
{
public override folat Run(float distance)
{
写要求的代码
}
public override void Stop()
{
写要求的代码
}
public bicycle()
{根据情况写构造函数}
}
public class motocar:vehicle
{
public override folat Run(float distance)
{
写要求的代码
}
public override void Stop()
{
写要求的代码
}
public motocar()
{根据情况写构造函数}
}
public class motocycle :motocar,bicycle
{
public override folat Run(float distance)
{
写要求的代码
}
public override void Stop()
{
写要求的代码
}
public motocycle()
{根据情况写构造函数}
}
void Main()
{
bicycle a;
motocar b;
motocycle c;
}
{
public virtual float Run(folat distance)
{
return distance/speed;
}
public virtual void Stop(bool color)
{
if (color ==true) cout<<"stop";
}
}
public class bicycle:vehicle
{
public override folat Run(float distance)
{
写要求的代码
}
public override void Stop()
{
写要求的代码
}
public bicycle()
{根据情况写构造函数}
}
public class motocar:vehicle
{
public override folat Run(float distance)
{
写要求的代码
}
public override void Stop()
{
写要求的代码
}
public motocar()
{根据情况写构造函数}
}
public class motocycle :motocar,bicycle
{
public override folat Run(float distance)
{
写要求的代码
}
public override void Stop()
{
写要求的代码
}
public motocycle()
{根据情况写构造函数}
}
void Main()
{
bicycle a;
motocar b;
motocycle c;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
意法半导体(中国)投资有限公司
2023-06-12 广告
2023-06-12 广告
单片机,即单片微控制器,也称为单片微型计算机,是将中央处理器(CPU)、存储器(ROM,RAM)、输入/输出接口和其他功能部件集成在一块 在一个小块的集成电路上,从而实现对整个电路或系统的数字式控制。单片机不是完成某一个逻辑功能的芯片,而是...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
展开全部
#include <iostream>
#include <string>
using namespace std;
class vehicle
{
public:
void Run();
void Stop();
};
void vehicle::Run()
{
cout<<"Run in vehicle"<<endl;
}
void vehicle::Stop()
{
cout<<"Stop in vehicle"<<endl;
}
//------------------------
class bicycle : public vehicle
{
public:
void Run();
void Stop();
};
void bicycle::Run()
{
cout<<"Run in bicycle"<<endl;
}
void bicycle::Stop()
{
cout<<"Stop in bicycle"<<endl;
}
//-------------------------
class motocar : public vehicle
{
public:
void Run();
void Stop();
};
void motocar::Run()
{
cout<<"Run in motocar"<<endl;
}
void motocar::Stop()
{
cout<<"Stop in motocar"<<endl;
}
//-------------------------
class motocycle : public motocar, public bicycle
{
public:
void Run();
void Stop();
};
void motocycle::Run()
{
cout<<"Run in motocycle"<<endl;
}
void motocycle::Stop()
{
cout<<"Stop in motocycle"<<endl;
}
//--------------------------
int main()
{
vehicle* pv = NULL;
vehicle oVehicle;
bicycle oBicycle;
motocar oMotocar;
motocycle oMotocycle;
oVehicle.Run();
oBicycle.Run();
oMotocar.Run();
oMotocycle.Run();
pv = &oVehicle;
pv->Run();
pv = &oBicycle;
pv->Run();
pv = &oMotocar;
pv->Run();
pv = (vehicle*)(motocar*)(&oMotocycle);
pv->Run();
system("pause");
}
#include <string>
using namespace std;
class vehicle
{
public:
void Run();
void Stop();
};
void vehicle::Run()
{
cout<<"Run in vehicle"<<endl;
}
void vehicle::Stop()
{
cout<<"Stop in vehicle"<<endl;
}
//------------------------
class bicycle : public vehicle
{
public:
void Run();
void Stop();
};
void bicycle::Run()
{
cout<<"Run in bicycle"<<endl;
}
void bicycle::Stop()
{
cout<<"Stop in bicycle"<<endl;
}
//-------------------------
class motocar : public vehicle
{
public:
void Run();
void Stop();
};
void motocar::Run()
{
cout<<"Run in motocar"<<endl;
}
void motocar::Stop()
{
cout<<"Stop in motocar"<<endl;
}
//-------------------------
class motocycle : public motocar, public bicycle
{
public:
void Run();
void Stop();
};
void motocycle::Run()
{
cout<<"Run in motocycle"<<endl;
}
void motocycle::Stop()
{
cout<<"Stop in motocycle"<<endl;
}
//--------------------------
int main()
{
vehicle* pv = NULL;
vehicle oVehicle;
bicycle oBicycle;
motocar oMotocar;
motocycle oMotocycle;
oVehicle.Run();
oBicycle.Run();
oMotocar.Run();
oMotocycle.Run();
pv = &oVehicle;
pv->Run();
pv = &oBicycle;
pv->Run();
pv = &oMotocar;
pv->Run();
pv = (vehicle*)(motocar*)(&oMotocycle);
pv->Run();
system("pause");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询