C++继承
来源:互联网 发布:迈克尔芬利数据 编辑:程序博客网 时间:2024/06/05 20:12
在C++中的父子类的一些注意点:
1、子类和父类指针的包容性
2、子类覆盖父类的同名函数方法
3、父类的析构函数定义成虚函数,防止子类的析构函数不被调用
4、关于虚函数的注意点【在内存中占用四个字节的内存用于指向虚函数表】
5、
#include <iostream>#include <string>using namespace std;class Father{ public: Father(int a = 0):a(a){cout << "父类构造函数" << endl;} ~Father(){cout << "父类析构函数" << endl;} //virtual ~Father(){cout << "父类 virtual 析构函数" << endl;} void get()const {cout << "我是父亲" << endl;} private: int a;};class Child : public Father{ public: Child(int a = 0):a(a){cout << "子类构造函数" << endl;} ~Child(){cout << "子类析构函数" << endl;} //virtual ~Child(){cout << "子类 virtual 析构函数" << endl;} void get()const {cout << "我是儿子" << endl;} private: int a;};int main(void){ //Father *f = new Father; Father *f = new Child; //f->get(); delete f; //Child c; //c.Father::get();}
0 0
- c继承
- C++----------------继承
- 【c#】继承
- C++:继承
- C++::继承
- [C++]继承
- 【C++】继承
- 【C#】继承
- 【c++】继承
- 【C++】继承
- c#-继承
- 【C++】 继承
- 【C#】继承
- C++|继承
- 【C++】继承
- C/C++--私有继承
- [C/C++]继承
- c++:私有继承,公有继承,保护继承
- RSA算法详解及C语言实现
- vc中ShellExecute的使用方法 ShellExecute 执行BAT命令
- MATLAB 图片浏览(简易版)
- Android API Guides---Creating a Search Interface
- idea配置
- C++继承
- UVALive 6838 Flipping Parentheses(线段树、单点更新、区间查询)
- css浮动模型初理解
- ScrollView嵌套ListView
- View的事件体系---V3.3 弹性滑动
- 指令与动作
- 项目2——友元类
- HDOJ 2196 Computer
- css层模型初理解