C++ 多重继承(环状继承)

不羁岁月 提交于 2019-12-15 07:10:09
  • 虚基类解决二义性模糊性问题

ambigous 二义性、模糊性 继承定义虚基类 (子类继承父类的时候,在父类前加 virtual)

在这里插入图片描述

#include<iostream>
using namespace std;
//基类 
class Beauty{
	public:
		string name;
		double height;
	protected:
		int age;
	private:
		double weight;
	//
	public:
	Beauty(){	}
	Beauty(string n,int a,double h,double w)
		{
			name=n;age=a; height =h; weight=w;
		}		
		
		void setinfo(string n,int a,double h,double w)
		{
			name=n;age=a; height =h; weight=w;
		}
		double get_weight(){return weight;		}
		
		void   show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<weight<<endl<<endl;
		}
	~Beauty(){	}		
}; 

//派生类
class BeautyNT :public virtual Beauty{
	protected:
		string nation;
	public:
		BeautyNT(){}
		BeautyNT(string n,int a,double h,double w,string nation):Beauty(n,a,h,w){
			this->nation = nation;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<this->nation<<endl<<endl;
		}
};
class BeautyStu :public virtual Beauty{
	protected:
		double score;
	public:
		BeautyStu(){}
		BeautyStu(string n,int a,double h,double w,double score):Beauty(n,a,h,w){
			this->score = score;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<this->score<<endl<<endl;
		}
};

/*
	多继承  环状继承,通过虚基类解决模糊性 
	1.ambigous 二义性、模糊性    继承定义虚基类  (子类继承父类的时候,在父类前加 virtual) 
	2.声明指向父类的指针,打印发现值少了   
		这是早绑定问题
		(在函数名前加 virtual)虚函数、纯虚函数(没有函数体,=0)  
			纯虚函数就是抽象类
			
			运行时多态  --- 虚函数
			编译时多态  --- 重载
	 
*/
class BeGirl :public BeautyNT, public BeautyStu{//多重继承 
	private:
		bool sex;
	public:
		BeGirl(){}
		BeGirl(string n,int a,double h,double w,double score,string nation,bool sex):BeautyNT(n,a,h,w,nation),BeautyStu(n,a,h,w,score),Beauty(n,a,h,w){
			this->sex = sex;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<score<<"  "<<nation<<"  "<<this->sex<<endl<<endl;
		}
};


int main(){
	
	BeautyNT bnt("小王王",19,165,100,"中国");
	bnt.show();
	cout<<"--------------------------------"<<endl;
	BeautyStu bst("大王王",20,165,100,100.0);
	bst.show();
	cout<<"--------------------------------"<<endl;
	BeGirl bg("大大王王",22,165,100,100.0,"中国",1);
	bg.show();
	
	
	return 0;
}

当用指针指向对象的时候,指针.show方法会少打印东西
这是早绑定问题,用虚函数或者纯虚函数

  • 虚函数

在这里插入图片描述

#include<iostream>
using namespace std;
//基类 
class Beauty{
	public:
		string name;
		double height;
	protected:
		int age;
	private:
		double weight;
	//
	public:
	Beauty(){	}
	Beauty(string n,int a,double h,double w)
		{
			name=n;age=a; height =h; weight=w;
		}		
		
		void setinfo(string n,int a,double h,double w)
		{
			name=n;age=a; height =h; weight=w;
		}
		double get_weight(){return weight;		}
		
		void  virtual show()	//虚函数 
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<weight<<endl<<endl;
		}
	~Beauty(){	}		
}; 

//派生类
class BeautyNT :public virtual Beauty{
	protected:
		string nation;
	public:
		BeautyNT(){}
		BeautyNT(string n,int a,double h,double w,string nation):Beauty(n,a,h,w){
			this->nation = nation;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<this->nation<<endl<<endl;
		}
};
class BeautyStu :public virtual Beauty{
	protected:
		double score;
	public:
		BeautyStu(){}
		BeautyStu(string n,int a,double h,double w,double score):Beauty(n,a,h,w){
			this->score = score;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<this->score<<endl<<endl;
		}
};

class BeGirl :public BeautyNT, public BeautyStu{//多重继承 
	private:
		bool sex;
	public:
		BeGirl(){}
		BeGirl(string n,int a,double h,double w,double score,string nation,bool sex):BeautyNT(n,a,h,w,nation),BeautyStu(n,a,h,w,score),Beauty(n,a,h,w){
			this->sex = sex;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<score<<"  "<<nation<<"  "<<this->sex<<endl<<endl;
		}
};


int main(){

	Beauty *p;
	
	BeautyNT bnt("小王王",19,165,100,"中国");	//值少打印了,是早绑定问题,用虚函数或者纯虚函数 
	p = &bnt;
	p->show();
	cout<<"--------------------------------"<<endl;
	BeautyStu bst("大王王",20,165,100,100.0);
	p = &bst;
	p->show();
	cout<<"--------------------------------"<<endl;
	BeGirl bg("大大王王",22,165,100,100.0,"中国",1);
	p = &bg;
	p->show();
	
	return 0;
}
  • 纯虚函数

在这里插入图片描述

#include<iostream>
using namespace std;
//基类 
class Beauty{
	public:
		string name;
		double height;
	protected:
		int age;
	private:
		double weight;
	//
	public:
	Beauty(){	}
	Beauty(string n,int a,double h,double w)
		{
			name=n;age=a; height =h; weight=w;
		}		
		
		void setinfo(string n,int a,double h,double w)
		{
			name=n;age=a; height =h; weight=w;
		}
		double get_weight(){return weight;		}
		
//		void  virtual show()	//虚函数 
//		{
//			cout<<name<<"  "<<age<<"  "<<height<<"  "<<weight<<endl<<endl;
//		}
		void  virtual show()=0;	//纯虚函数 

	~Beauty(){	}		
}; 

//派生类
class BeautyNT :public virtual Beauty{
	protected:
		string nation;
	public:
		BeautyNT(){}
		BeautyNT(string n,int a,double h,double w,string nation):Beauty(n,a,h,w){
			this->nation = nation;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<this->nation<<endl<<endl;
		}
};
class BeautyStu :public virtual Beauty{
	protected:
		double score;
	public:
		BeautyStu(){}
		BeautyStu(string n,int a,double h,double w,double score):Beauty(n,a,h,w){
			this->score = score;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<this->score<<endl<<endl;
		}
};

class BeGirl :public BeautyNT, public BeautyStu{//多重继承 
	private:
		bool sex;
	public:
		BeGirl(){}
		BeGirl(string n,int a,double h,double w,double score,string nation,bool sex):BeautyNT(n,a,h,w,nation),BeautyStu(n,a,h,w,score),Beauty(n,a,h,w){
			this->sex = sex;
		}
		void  show()
		{
			cout<<name<<"  "<<age<<"  "<<height<<"  "<<get_weight()<<"  "<<score<<"  "<<nation<<"  "<<this->sex<<endl<<endl;
		}
};


int main(){

	Beauty *p;
	
	BeautyNT bnt("小王王",19,165,100,"中国");	//值少打印了,是早绑定问题,用虚函数或者纯虚函数 
	p = &bnt;
	p->show();
	cout<<"--------------------------------"<<endl;
	BeautyStu bst("大王王",20,165,100,100.0);
	p = &bst;
	p->show();
	cout<<"--------------------------------"<<endl;
	BeGirl bg("大大王王",22,165,100,100.0,"中国",1);
	p = &bg;
	p->show();
	
	return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!