lab8_2.cpp
来自「清华大学出版社出版的c++程序设计课本」· C++ 代码 · 共 71 行
CPP
71 行
#include <iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
vehicle(){MaxSpeed=0; Weight=0;};
~vehicle(){};
virtual void Run() {cout << "A vehicle is running!" << endl; }
virtual void Stop() {cout << "A vehicle has stopped!" << endl; }
};
class bicycle : virtual public vehicle
{
private:
int Height;
public:
bicycle(){};
~bicycle(){};
void Run() {cout << "A bicycle is running!" << endl; }
void Stop() {cout << "A bicycle has stopped!" << endl; }
};
class motorcar : virtual public vehicle
{
private:
int SeatNum;
public:
motorcar(){};
~motorcar(){};
void Run() {cout << "A motorcar is running!" << endl; }
void Stop() {cout << "A motorcar has stopped!" << endl; }
};
class motorcycle : public bicycle , public motorcar
{
public:
motorcycle (){};
~motorcycle (){};
void Run() {cout << "A motorcycle is running!" << endl; }
void Stop() {cout << "A motorcycle has stopped!" << endl; }
};
void main()
{
vehicle * ptr;
vehicle a;
bicycle b;
motorcar c;
motorcycle d;
a.Run();
a.Stop();
b.Run();
b.Stop();
c.Run();
c.Stop();
d.Run();
d.Stop();
ptr = &a;
ptr->Run();
ptr = &b;
ptr->Run();
ptr = &c;
ptr->Run();
ptr = &d;
ptr->Run();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?