/*
题目:带有多对象成员的虚拟多继承
要求:1、虚函数 多态 多态表现为 基类 基类指针和继承间的关系
2、带有多对象成员。定义
3、体现继承 虚拟继承(要通过至少三层 父类父类子类)
虚函数 (3层 纵向关系)
水平方向上:体现出继承顺序 先虚拟继承 再普通继承(至少8个类)
通过实例化类 体现对象构造和析构的顺序
还要有函数重载 指针 指针悬挂 无参函数等问题
*/
#include <iostream>
using namespace std;
#define Max 10
class Driver //驾驶员基类
{
public:
Driver()
{
cout<<"载入Driver类构造函数"<<endl;
}
Driver(char *name, char *ID) //函数重载:构造函数的重载
{
strcpy(this->name, name);
strcpy(DriveID, ID);
cout<<"载入Driver类构造函数"<<endl;
}
void SetName(char *name)
{
strcpy(this->name, name);
}
char *GetName()
{
return name;
}
void SetDriveID(char *ID)
{
strcpy(DriveID, ID);
}
virtual void ShowMe() //虚函数,可被子类重载
{
cout<<"你好!我的名字是"<<name<<", 我的驾驶证号是"<<DriveID<<endl;
}
virtual ~Driver()
{
cout<<"载入Driver基类析构函数"<<endl;
}
protected:
char name[Max];
char DriveID[2*Max];
};
class MainDriver: public Driver //主驾驶员
{
public:
MainDriver(char *name, char *id):Driver(name, id)
{
cout<<"载入MainDriver类构造函数"<<endl;
}
virtual void ShowMe() //虚函数,可被子类重载
{
cout<<"你好!我是主驾驶员,我的名字是"<<name<<", 我的驾驶证号是"<<DriveID<<endl;
}
virtual ~MainDriver()
{
cout<<"载入MainDriver基类析构函数"<<endl;
}
};
class AssociateDriver:public Driver //副驾驶员类
{
public:
AssociateDriver(char *name, char *id):Driver(name, id)
{
cout<<"载入AssociateDriver类构造函数"<<endl;
}
virtual void ShowMe() //虚函数,可被子类重载
{
cout<<"你好!我是副驾驶员,我的名字是"<<name<<", 我的驾驶证号是"<<DriveID<<endl;
}
virtual ~AssociateDriver()
{
cout<<"载入AssociateDriver基类析构函数"<<endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////
class Vehicle //车辆基类
{
public:
Vehicle(int weight = 0)
{
Vehicle::weight = weight;
cout<<"载入Vehicle类构造函数"<<endl;
}
void SetWeight(int weight)
{
cout<<"重新设置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0; //纯虚函数,一定要被子类重写
virtual char *GetVehicle() = 0;//纯虚函数,一定要被子类重写
virtual ~Vehicle()
{
cout<<"载入Vehicle基类析构函数"<<endl;
}
protected:
int weight;
};
class Car:virtual public Vehicle//汽车,这里是虚拟继承
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
cout<<"载入Car类构造函数"<<endl;
}
void ShowMe()
{
cout<<"我是汽车!"<<endl;
}
char *GetVehicle()
{
return "Car";
}
virtual ~Car()
{
cout<<"载入Car派生类析构函数"<<endl;
}
protected:
int aird;
};
class Boat:virtual public Vehicle//船,这里是虚拟继承
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;
cout<<"载入Boat类构造函数"<<endl;
}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
char *GetVehicle()
{
return "Boat";
}
virtual ~Boat()
{
cout<<"载入Boat派生类析构函数"<<endl;
}
protected:
float tonnage;
};
class AmphibianCar:public Car,public Boat//水陆两用汽车,多重继承的体现
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重继承要注意调用基类构造函数
{
cout<<"载入AmphibianCar类构造函数"<<endl;
}
void ShowMe()
{
cout<<"我是水陆两用汽车!"<<endl;
}
char *GetVehicle()
{
return "AmphibianCar";
}
void ShowMembers()
{
cout<<" 重量:"<<weight<<"顿,"<<"空气排 量:"<<aird<<"CC,"<<"排水量:"<<tonnage<<" 顿"<<endl;
}
virtual ~AmphibianCar()
{
cout<<"载入Boat派生类析构函数"<<endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////
class Drive //驾驶类
{
public:
Drive(Driver *d, Vehicle *v)
{
cout<<"载入Drive类构造函数"<<endl;
driver = d;
vehicle = v;
}
virtual ~Drive()
{
cout<<"载入Drive类析构函数"<<endl;
delete driver;
delete vehicle;
}
void DriveVehicle()
{
cout<<driver->GetName()<<"正在驾驶"<<vehicle->GetVehicle()<<endl;
}
private:
Driver *driver;
Vehicle *vehicle;
};
int main()
{
Drive *d;
Driver *driver = new MainDriver("影儿飘飘", "123456");
driver->ShowMe();
cout<<endl<<"--------------------------------------------------"<<endl;
Boat *b = new Boat(5, 400);
b->ShowMe();
cout<<endl<<"--------------------------------------------------"<<endl;
d = new Drive(driver, b);
d->DriveVehicle();
cout<<endl<<"--------------------------------------------------"<<endl;
delete d;
//////////////////////////////////////////////////////////
cout<<endl<<"--------------------------------------------------"<<endl;
AmphibianCar *a = new AmphibianCar(4,200,1.35f);
a->ShowMe();
a->ShowMembers();
a->SetWeight(3);
a->ShowMembers();
cout<<endl<<"--------------------------------------------------"<<endl;
driver = new AssociateDriver("杨旭东", "654321");
driver->ShowMe();
cout<<endl<<"--------------------------------------------------"<<endl;
d = new Drive(driver, a);
d->DriveVehicle();
cout<<endl<<"--------------------------------------------------"<<endl;
delete d;
system("pause");
return 0;
}
- 1
- 2
- 3
前往页