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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
展开全部
#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");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询