c++类的多重继承与虚拟继承

本文通过实例解析了C++中的多重继承概念,并介绍了如何解决多重继承带来的二义性问题,即通过使用虚拟继承确保成员函数的唯一性。

在过去的学习中,我们始终接触的单个类的继承,但是在现实生活中,一些新事物往往会拥有两个或者两个以上事物的属性,为了解决这个问题,C++引入了多重继承的概念,C++允许为一个派生类指定多个基类,这样的继承结构被称做多重继承

   举个例子,交通工具类可以派生出汽车和船连个子类,但拥有汽车和船共同特性水陆两用汽车就必须继承来自汽车类与船类的共同属性。

 

//程序作者:管宁
//站点:www.cndev-lab.com

class Vehicle
{
public:
 Vehicle( int weight = 0)
 {
  Vehicle::weight  = weight;
  cout<<"载入Vehicle类构造函数"<<endl;
 }

 void SetWeight(int weight)
 {
  cout<<"重新设置重量"<< weight<<endl;;
 }

 virtual void ShowMe() = 0;
protected:
 int weight;
};

class Car: public Vehicle
{
public:
 Car(int weight = 0, int aird = 0):Vehicle(weight)
 {
  Car::aird = aird;
  cout<<"载入Car类构造函数"<<endl;
 }

 void ShowMe()
 {
  cout<<"我是汽车"<<endl;
 }

protected:
 int aird;
};

class Boat: public Vehicle
{
public:
 Boat(int weight = 0, float tonnage = 0):Vehicle(weight)
 {
  Boat::tonnage = tonnage;
  cout<<"载入Boat类构造函数"<<endl;
 }

 void ShowMe()
 {
  cout<<"我是船"<<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;
 }

 void ShowMembers()
 {
  cout<<"重量"<<endl;
 }
};

int main()
{
 AmphibianCar a(4,200, 1.35);
 a.SetWeight(3);
 
 system("pause");

 return 0;
}

 

上面的代码从表面看,看不出有明显的语发错误,但是它是不能够通过编译的。这有是为什么呢?
   这是由于多重继承带来的继承的模糊性带来的问题。

 其主要问题所在,水陆两用汽车类继承了来自Car类与Boat类的属性与方法,Car类与Boat类同为AmphibianCar类的基类,在内存分配上AmphibianCar获得了来自两个类的SetWeight()成员函数,当我们调用a.SetWeight(3)的时候计算机不知道如何选择分别属于两个基类的被重复拥有了的类成员函数SetWeight()。
   由于这种模糊问题的存在同样也导致了AmphibianCar a(4,200,1.35f);执行失败,系统会产生Vehicle”不是基或成员的错误。

 

以上面的代码为例,我们要想让AmphibianCar类既获得一个Vehicle的拷贝,而且又同时共享用Car类与Boat类的数据成员与成员函数就必须通过C++所提供的虚拟继承技术来实现。
   我们在Car类和Boat类继承Vehicle类出,在前面加上virtual关键字就可以实现虚拟继承,使用虚拟继承后,当系统碰到多重继承的时候就会自动先加入一个Vehicle的拷贝,当再次请求一个Vehicle的拷贝的时候就会被忽略,保证继承类成员函数的唯一性

   修改后的代码如下:

//程序作者:管宁
//站点:www.cndev-lab.com

class Vehicle
{
public:
 Vehicle( int weight = 0)
 {
  Vehicle::weight  = weight;
  cout<<"载入Vehicle类构造函数"<<endl;
 }

 void SetWeight(int weight)
 {
  cout<<"重新设置重量"<< weight<<endl;;
 }

 virtual void ShowMe() = 0;
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;
 }

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;
 }

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;
 }

 void ShowMembers()
 {
  cout<<"重量"<<endl;
 }
};

int main()
{
 AmphibianCar a(4,200, 1.35);
// a.SetWeight(3);

 a.ShowMe();
 a.ShowMembers();

 a.SetWeight(3);
 a.ShowMembers();


 system("pause");

 return 0;
}

 

注意观察类构造函数的构造顺序。

 

 

 

 

一个简单的汽车租赁管理系统(C++控制台程序): 利用C++实现对汽车客户信息的增、删、改等操作,并保存。 部分代码: // CarRent.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "CarData.h" #include "Menu.h" #include"CustomerData.h" int main() { Menu::welcome(); Menu::login(); //登录界面 char choice; int carid, customerid; //汽车编号,客户编号 CustomerData CustomerDB; //客户库 Customer *r; //客户 CarData CarDB; //汽车库 Car *b; //汽车 Menu::showMenu(); //显示菜单 cout <> choice; while (choice != '0') { switch (choice) { case '1': //输入客户编号要出租的汽车 cout <> customerid; try { if (customerid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } cout <> carid; try { if (carid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } r = CustomerDB.search(customerid); if (NULL == r) { cout << "不存在该客户!"; cout << endl; break; } b = CarDB.search(carid); if (b == NULL) { cout << "不存在该汽车!"; cout <borrowCar() == 0) { cout << "该汽车已租出!"; cout <borrowCar(b->getNo()); cout << "你在" <getBorTime()) << "成功出租一辆" <getName() << endl << "每日租金为(不足一天按一天计算):" <getPay(); break; case '2': //归还操作 cout <> customerid; try { if (customerid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } cout <> carid; try { if (carid <= 0) throw 1; } catch (...) { cout << "输入有误,请重新输入"; break; } r = CustomerDB.search(customerid); //按编号查找 if (r == NULL) { cout << "不存在该客户!" << endl; break; } b = CarDB.search(carid); if (b == NULL) { cout << "不存在该汽车!" <getOnshelf() == 1) { cout << "该汽车已经归还!" << endl; break; } cout << "您成功归还一辆"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值