继承&多态

// 1.实现以下几个类的成员函数
// 2.实现一个虚函数的覆盖及调用
// 3.处理菱形继承问题。
//


// 植物
#include<iostream>
using namespace std;
#include<string>


class Botany
{
public:




	Botany(string & name)
		:_name(name)
	{
		++_sCount;
	}


	Botany& operator=(const	Botany& b)
	{
		if(this != &b)
		{
			_name = b._name;
		}
		return *this;
	}


	~Botany()
	{}


	virtual void Display ();


protected:
	string _name;


public:
	static int _sCount;//名字
};


int Botany::_sCount = 0;


void Botany::Display() 
{
	cout<<"name:"<<_name<<endl;
}


class Tree : virtual public Botany
{
public:


	Tree(string &name ,int hight )
		:Botany(name)
		,_hight(hight)
	{}


	Tree(const Tree &t)
		:Botany(t)
		,_hight(t._hight )
	{}


	Tree &operator=(const Tree &t)
	{
		if(this != &t)
		{
			Botany::operator =(t);
			_hight = t._hight ;
		}
		return *this;	
	}


	~Tree()
	{}


	virtual void Display ();


protected:
	int _hight;		// 高度
};


void Tree::Display() 
{
	cout<<"name: "<<_name<<endl;
	cout<<"hight: "<<_hight<<endl;
}


class Flower :virtual public Botany
{
public:
	//...实现默认的成员函数
	Flower( string &name,string colour)
		:Botany(name)
		,_colour(colour)
	{}


	Flower(const Flower &f)
		:Botany(f)
		,_colour(f._colour )
	{}


	Flower &operator=(Flower &f)
	{
		if(this != &f)
		{
			Botany::operator =(f);
			_colour = f._colour ;
		}
		return *this;	
	}


	~Flower()
	{}


	virtual void Display ();
protected:
	string _colour;	// 颜色
};


void Flower::Display() 
{
	cout<<"name: "<<_name<<endl;
	cout<<"color: "<<_colour<<endl;
}


// 白兰花,既是树又是花。
class MicheliaAlba : public Flower, public Tree
{
public:
	void Display()
	{
		cout<<"name: "<<Flower::_name<<endl;
		cout<<"hight: "<<Tree::_hight<<endl;
		cout<<"Flower: "<<Flower::_colour <<endl;
	}
private:
	// ...
};


void Test()
{
	string name = "Magnolia";
	Tree t1(name,20);
	t1.Display ();	
	Flower f1(name,"white");
	f1.Display ();
	cout<<"The num of botanies is: "<<Botany::_sCount<<endl;


}


int main()
{
	Test();
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值