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()成员函数。 展开
展开全部
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询