菱形继承
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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性