您的位置:首页 > 编程语言 > C语言/C++

菱形继承

2016-03-20 20:18 489 查看
#include<iostream>
#include<string>
using namespace std;
// 植物
class Botany
{
public:
Botany(const char* name)
:_name(name)
{
++_sCount;
//cout<<"Botany()"<<endl;
}
Botany(const Botany& b)
:_name(b._name )
{
//cout<<"Botany (const Botany& b)"<<endl;
}
Botany& operator=(const Botany& b)
{
//cout<<"Botany operator=(const Botany& b)"<<endl;
if(this!=&b)
{
_name=b._name ;
}
return *this;
}
virtual ~Botany()
{
// cout<<"~Botany()"<<endl;
}
virtual void Display ()
{
cout<<"name:"<<_name<<endl;
}
protected:
string _name;		//名字
static int _sCount;
};
int Botany::_sCount =0;
class Tree : virtual public Botany
{
public:
Tree(const char* name,int hight)
:Botany(name)
,_hight(hight)
{
//cout<<"Tree()"<<endl;
}
Tree(const Tree& t)
:Botany(t)
,_hight(t._hight )
{
// cout<<"Tree(const Tree& t)"<<endl;
}
Tree& operator=(const Tree& t)
{
//cout<<"Tree& operator=(const Tree& t)"<<endl;
if(this!=&t)
{
Botany::operator=(t);
_hight=t._hight ;
}
return *this;
}
virtual ~Tree()
{
// cout<<"~Tree()"<<endl;
}
virtual void Display()
{
Botany::Display();
cout<<"hight:"<<_hight<<endl;
}
protected:
int _hight;		// 高度
};
class Flower : virtual public Botany
{
public:
Flower(const char* name,const char* colour)
:Botany(name)
,_colour(colour)
{
//cout<<"Flower()"<<endl;
}
Flower(const Flower& f)
:Botany(f)
,_colour(f._colour )
{
// cout<<"Flower(const Flower& f)"<<endl;
}
Flower& operator=(const Flower& f)
{
// cout<<"Flower& operator=(const Flower& f)"<<endl;
if(this!=&f)
{
Botany:: operator=(f);
_colour=f._colour ;
}
return *this;
}

virtual ~Flower()
{
// cout<<"~Flower()"<<endl;
}
virtual void Display()
{
Botany::Display();
cout<<"colour:"<<_colour<<endl;
}
protected:
string _colour;	// 颜色
};
// 白兰花,即是树有时花。
class MicheliaAlba : public Flower, public Tree
{
public:
MicheliaAlba(const char* name,int hight,const char* colour)
:Botany(name)
,Tree(name,hight)
,Flower(name,colour)
{
// cout<<"MicheliaAlba()"<<endl;
}
MicheliaAlba(const MicheliaAlba& m)
:Botany(m)
,Tree(m)
,Flower(m)
{
// cout<<"MicheliaAlba(const MicheliaAlba& m)"<<endl;
}
MicheliaAlba& operator=(const MicheliaAlba& m)
{
// cout<<"MicheliaAlba& operator =(const MicheliaAlba& m)"<<endl;
if(this!= &m)
{
Botany:: operator=(m);
Tree:: operator=(m);
Flower:: operator=(m);
}
return *this;
}
virtual ~MicheliaAlba()
{
// cout<<"~MicheliaAlba()"<<endl;
}
virtual void Display()
{
cout<<"name:"<<_name<<"    colour:"<<_colour<<"    hight:"<<_hight<<endl;
}
protected:

};
void testBotany()
{
Botany b1("rose");
b1.Display ();
Botany b2("aaaa");
b2.Display ();
b1=b2;
b1.Display ();
}
void testTree()
{
Tree t1("Tree1",2);
t1.Display ();
Tree t2("Tree2",5);
t2.Display ();
t1=t2;
t1.Display ();
}
void testFlower()
{
Flower f1("f1","red");
f1.Display ();
Flower f2("f2","green");
f2.Display ();
f2=f1;
f2.Display ();
}
void testMicheliaAlba()
{
MicheliaAlba m1("bailanhua",20,"red");
m1.Display ();
MicheliaAlba m2("rrr",50,"blue");
m2.Display ();
m1=m2;
m1.Display ();
}
int main()
{
//testBotany();
//testTree();
//testFlower();
testMicheliaAlba();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 菱形继承 植物类