#include <iostream>

#include <string>

using namespace std;

 1.实现以下几个类的成员函数

 2.实现一个虚函数的覆盖及调用

 3.处理菱形继承问题。



植物

class Botany

{

public:

//(const string& name) // const char* name

Botany(const char* name = "")

:_name(name) //构造函数

{

//cout<<"Botany(const char* name)"<<endl;

}


Botany(const Botany& b)

:_name(b._name)

{}


Botany& operator= (const Botany& b)

{

if (this != &b)

{

_name = b._name;

}


return *this;

}


~Botany()

{}


virtual void Display ()

{

cout<<"Botany::Display"<<endl;

}


public:

protected:

string _name; //名字

};


class Tree : virtual public Botany

{

public:

Tree(const string& name, int hight)

 :Botany(name.c_str())

 ,_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 ()

{

cout<<"Tree::Display"<<endl;

}


public:

int _hight; // 高度

};


class Flower : virtual public Botany

{

public:

Flower(const string& name, int colour)

:Botany(name.c_str())

,_colour(colour)

{}


Flower(const Flower& t)

:Botany(t)

,_colour(t._colour)

{}


Flower& operator=(const Flower& t)

{

if (this != &t)

{

Botany::operator=(t);

_colour = t._colour;

}


return *this;

}


~Flower()

{}


public:

protected:

int _colour; // 颜色

};


 白兰花,即是树有时花。

class MicheliaAlba : public Tree, public Flower

{

public:

// 必须先初始化虚基类的构造函数

MicheliaAlba(const string& name, int hight, int colour)

:Botany(name.c_str())

,Flower(name, colour)

,Tree(name, hight)

{}


MicheliaAlba(const MicheliaAlba& m)

:Botany(m._name.c_str())

,Flower(m)

,Tree(m)

{}


MicheliaAlba& operator=(const MicheliaAlba& m)

{

if (this != &m)

{

Flower::operator=(m);

Tree::operator=(m);

}


return *this;

}

~MicheliaAlba()

{}


void Display()

{

//cout<<_name<<endl;

cout<<Flower::_name<<endl;

cout<<Tree::_name<<endl;

}


protected:

};


void Test()

{

Botany b1("xxx");

Tree t1("xxx", 1);


Botany& r1 = b1;

r1.Display();


Botany& r2 = t1;

r2.Display();


Botany* p3 = &t1;

p3->Display();

}


int main()

{

//Test();

MicheliaAlba m1("白玉兰", 10, 1);

MicheliaAlba m2("黄玉兰", 15, 2);


cout<<&(m1.Flower::_name)<<endl;

cout<<&(m1.Tree::_name)<<endl;


m1.Display();


return 0;

}