⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18章虚拟继承.txt

📁 里面的代码是自己写的,构思来自于c++primer,对于学习c++语法有非常大的帮助,希望对c++初学者有所帮助
💻 TXT
字号:
/* 本程序在与说明虚拟继承的特点,主要是考察构造函数和类ZooAnimal的成员函数GetName()的情况
*  详细请见c++ primer 3/e P813
*  此例子的类层次结构图见P814的图18.4
*/
#include <iostream>
#include <string>
using namespace std;

/*
*
*/
class ZooAnimal {
public:
	ZooAnimal();
	ZooAnimal( const string & Name );
	string GetName() const;
    virtual ~ZooAnimal();
private:
	string m_AnimalName;
};

ZooAnimal::ZooAnimal( const string & Name ) : m_AnimalName( Name )
{
	cout << "ZooAnimal's constuctor is called" << endl;
}

ZooAnimal::ZooAnimal()
{
	cout<<"ZooAnimal's default constructor is called!"<<endl;
}

string ZooAnimal::GetName() const //经过调试,此处的const不能省略,否则出现
                                  //error C2511: “std::string ZooAnimal::GetName(void)” : “ZooAnimal”中没有找到重载的成员函数
{
     return m_AnimalName;
}
ZooAnimal::~ZooAnimal()
{
	cout<<" ZooAnimal's destructor is called !"<<endl;
}

/*
*
*
*/
class Bear : virtual public ZooAnimal {
public:
	Bear();
	Bear( string & color );
  virtual ~Bear();
private:
	string m_color;
};
Bear::Bear()
{
   cout << "Bear's default constructor is called!" << endl;
}
Bear::Bear( string & color ): m_color( color )
{
	cout << "Bear's constructor is called" << endl;
}
Bear::~Bear()
{
	cout << "Bear's destructor is called!" << endl;
}



/*
*
*/
class Endangered {
public:
	Endangered();
	Endangered( int age );
	virtual ~Endangered();
private:
	int m_age;
};

Endangered::Endangered()
{
	cout<<"Endangered's default constructor is called" << endl;
}


Endangered::Endangered( int age )
{
	m_age=age;
	cout<<"Endangered's constructor is called" << endl;
}

Endangered::~Endangered()
{
	cout << "Endangered's destructor is called! " << endl;
}





class Raccon : virtual public ZooAnimal {
public:
	Raccon();
	Raccon( string & color );
  virtual ~Raccon();
private:
	string mm_color;
};
Raccon::Raccon()
{
   cout << "Raccon's default constructor is called!" << endl;
}
Raccon::Raccon( string & color ): mm_color( color )
{
	cout << "Raccon's constructor is called" << endl;
}
Raccon::~Raccon()
{
	cout << "Raccon's destructor is called!" << endl;
}



/*
*
*/
class Panda: public Bear,public Raccon, public Endangered {
public:
	Panda();
	Panda( string & color, int age );
	void Pdosomething();
	~Panda();
};

Panda::Panda( string & color, int age ): Bear( color ), Endangered( age )
{
	cout << "Panda's constructor is called !" <<endl;
}
Panda::Panda()
{
  cout << "Panda's default constructor is called!"<<endl;
}
Panda::~Panda()
{
	cout << "Panda's destructor is called!"<<endl;
}
void Panda::Pdosomething()
{ 
	cout << "dosomething of Panda!"<<endl;
}




/*
 通过构造函数的调用考查虚拟继承。
 如果不是虚拟继承,则ZoonAnimal类的构造函数会被调用两次
*/

  int main()
{
  string color("red"); 
  int age=10;

  /*如果把类Bear 或类Raccon 申明的虚拟继承去掉(去掉一个或则两个),则Panda 类对象yingyang有两个ZooAnimal对象。
   *构造函数调用情况如下(在控制台输出的前半部分,后半部分是析构函数):
   *                                       ZooAnimal's default constructor is called!
   *                                       Bear's constructor is called
   *                                       ZooAnimal's default constructor is called!
   *                                       Raccon's default constructor is called!
   *									   Endangered's constructor is called!
   *									   Panda's constructor is called !
 
   *而当两个类都是虚拟继承了ZooAnimal类的时候,那么Panda类对象yingyang 只有一个ZooAnimal对象。
   */                                     
  Panda yingyang(color,age);  
  cout<<endl;
  
  //如果去掉虚拟继承(去掉一个也是,两个也是),则本句出错。 error C2385: 对“GetName”(在“Panda”中)的访问不明确
  //yingyang.GetName();  

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -