c++多重继承
2016-06-29 20:14
567 查看
#include<iostream> #include<stdlib.h> using namespace std; class father { public: //father(){ cout << "创建父亲" << endl; } father(int height); //father(){} virtual ~father(){ cout << "析构父亲" << endl; } virtual void smart()const{ cout << "父亲很聪明" << endl; } virtual int getheight()const{ return itsheight; } protected: int itsheight; }; father::father(int height) //:itsheight(height) { itsheight = height; cout << "创建父亲" << endl; } class mother { public: //mother(){ cout << "创建母亲" << endl; } mother(bool sex); //mother(){} virtual ~mother(){ cout << "析构母亲" << endl; } virtual void beautiful()const{ cout << "母亲很漂亮" << endl; } virtual bool getsex()const{ return itssex; } protected: bool itssex; }; mother::mother(bool sex) //:itssex(sex) { itssex = sex; cout << "创建母亲" << endl; } class son :public father, public mother { public: //son(){ cout << "创建儿子" << endl; } son(int, bool, long); ~son(){ cout << "析构儿子" << endl; } //void smart()const{ cout << "儿子很聪明" << endl; } //void beautiful()const{ cout << "儿子很帅" << endl; } virtual long getnum()const{ return num; } private: long num; }; son::son(int height, bool sex, long number):father(height), mother(sex), num(number) { //itsheight = height;//使用这种初始化方式时,调用的是基类不带参数的构造函数,若基类不存在不带参数的构造函数,则报错 //itssex = sex; //num = number; //father(height); cout << "创建儿子" << endl; } int main() { //father*p = new father; //p->smart(); //delete p; //father*q = new son; //q->smart(); //delete q; //mother*e = new mother; //e->beautiful(); //delete e; //mother*r = new son; //r->beautiful(); //delete r; son*ps = new son(5, true, 3); ps->beautiful(); ps->smart(); cout << "小儿子有" << ps->getheight(); cout << "五英尺高" << endl; delete ps; system("pause"); return 0; }
相关文章推荐
- c++多态性
- 树莓派GPIO控制--C语言篇
- ubuntu系统vim基本配置
- C++错误累积
- #define a int[10] typedef int a[10]
- 大家好,这是我第一篇在CSDN上的博文,本篇主要讨论小型内存池的设计
- C++ 11 创建和使用 shared_ptr
- C++经典书籍推荐
- 《深入探索C++对象模型》第三章奇怪语句解释
- C++ 服务器端学习之心跳控制
- C++编程笔记:dll的生成与使用
- 解决 C/C++ 程序执行一闪而过的方法
- C++如何输出特定格式浮点数
- C++ explicit
- const在函数前与函数后的区别
- 矩阵取数游戏noip2006(c++ BigInteger的第一次运用)---重点!!
- C++中的异常处理(三)
- C++中的异常处理(二)
- C++中的异常处理(一)
- 快排算法及利用快排思想寻找第k大(小)的数