C++ Primer学习笔记——$19 多重继承与虚继承 (
来源:互联网 发布:怎样下载ps软件 编辑:程序博客网 时间:2024/05/29 16:44
#include<iostream>
class Base1 {
public:
void print(){}
void display(){}
void show(){}
};
class Base2 {
public:
void print(int){}
void show(int);
private:
void display(int){}
};
class Derived:public Base1,public Base2{
};
int main (){
Derived d;
d.print();
d.display();
d.show();
return 0;
}
编译结果(MinGW2.05):
Compiling source file(s)...
main.cpp
main.cpp: In function `int main()':
main.cpp:24: error: request for member `print' is ambiguous
main.cpp:13: error: candidates are: void Base2::print(int)
main.cpp:6: error: void Base1::print()
main.cpp:25: error: request for member `display' is ambiguous
main.cpp:16: error: candidates are: void Base2::display(int)
main.cpp:7: error: void Base1::display()
main.cpp:26: error: request for member `show' is ambiguous
main.cpp:14: error: candidates are: void Base2::show(int)
main.cpp:8: error: void Base1::show()
Test.exe - 9 error(s), 0 warning(s)
#include<iostream>
class B {
public:
void print(){
std::cout<<"B" <<std::endl;
}
};
class D1: public virtual B {
};
class D2: public virtual B {
public:
void print(){
std::cout<<"D2" <<std::endl;
}
};
class DD: public D1, public D2 {
};
int main (){
DD d;
d.print(); // ok: call D2::print
return 0;
}
#include<iostream>
class Character {
public:
Character(){
std::cout<<"Character Constructor" <<std::endl;
}
~Character(){
std::cout<<"Character Destructor" <<std::endl;
}
};
class BookCharacter:public Character{
public:
BookCharacter(){
std::cout<<"BookCharacter Constructor" <<std::endl;
}
~BookCharacter(){
std::cout<<"BookCharacter Destructor" <<std::endl;
}
};
class ZooAnimal {
public:
ZooAnimal(){
std::cout<<"ZooAnimal Constructor" <<std::endl;
}
~ZooAnimal(){
std::cout<<"ZooAnimal Destructor" <<std::endl;
}
};
class Bear:publicvirtual ZooAnimal{
public:
Bear() {
std::cout<<"Bear Constructor" <<std::endl;
}
~Bear(){
std::cout<<"Bear Destructor" <<std::endl;
}
};
class ToyAnimal {
public:
ToyAnimal(){
std::cout<<"ToyAnimal Constructor" <<std::endl;
}
~ToyAnimal(){
std::cout<<"ToyAnimal Destructor" <<std::endl;
}
};
class TeddyBear:public BookCharacter,public Bear,publicvirtual ToyAnimal{
public:
TeddyBear(){
std::cout<<"TeddyBear Constructor" <<std::endl;
}
~TeddyBear(){
std::cout<<"TeddyBear Destructor" <<std::endl;
}
};
int main (){
TeddyBear tb;
return 0;
}
运行结果如下:
ZooAnimal Constructor
ToyAnimal Constructor
Character Constructor
BookCharacter Constructor
Bear Constructor
TeddyBear Constructor
TeddyBear Destructor
Bear Destructor
BookCharacter Destructor
Character Destructor
ToyAnimal Destructor
ZooAnimal Destructor
Terminated with return code 0
Press any key to continue ...
如果文中有错误或遗漏之处,敬请指出,谢谢!
[2] Thinking in C++(Volume Two, Edition 2)
[3] International Standard:ISO/IEC 14882:1998
- C++ Primer学习笔记——$19 多重继承与虚继承
- C++ Primer学习笔记——$19 多重继承与虚继承
- C++ Primer学习笔记——$19 多重继承与虚继承 (
- C++ Primer学习笔记 多重继承与虚继承
- C++ Primer学习笔记——$19 多重继承与虚继承(转自:http://blogold.chinaunix.net/u/18517/showart_252162.html)
- C++ Primer笔记(十七)多重继承与虚继承
- C++primer中文第四版笔记---多重继承与虚继承
- C++ Primer 学习笔记_95_用来大型程序的工具 -多重继承与虚继承
- C++ primer第二次阅读学习笔记(第17章:用于大型程序的工具:多重继承与虚继承) .
- C++ Primer 学习笔记_96_用以大型程序的工具 -多重继承与虚继承[续1]
- C++ Primer 学习笔记_97_用来大型程序的工具 -多重继承与虚继承[续2]
- C++ Primer 有感(多重继承与虚继承)
- C++ Primer学习笔记 — 复制控制与继承
- 【学习笔记】C++primer plus 13.继承
- [javase学习笔记]-9.2 单继承与多重继承
- 多重继承与虚继承
- 多重继承与虚继承
- 多重继承与虚继承
- Xenu Link Sleuth死链接检测工具(SEO工具)
- 快捷键技巧
- 502 Bad Gateway The CGI was not CGI/1.1 compliant
- java零碎
- 【计算机网络】基础知识--套接字socket的组成及作用
- C++ Primer学习笔记——$19 多重继承与虚继承 (
- Yii使用中小知识点积累
- iPhone应用程序的启动过程
- ASP.NET MVC3 Custom ErrorPages 500/404
- jsp中如何获取客户端的浏览器和操作系统信息
- 会员策略
- Ubuntu下利用JDK的Keytool配置Tomcat7.0的SSL协议
- 用最少的并行交换完成排序
- 【乔布斯走了】别太熬夜了,身体没了,事业算啥,防癌16条