博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++学习-继承
阅读量:4445 次
发布时间:2019-06-07

本文共 2095 字,大约阅读时间需要 6 分钟。

 

继承

#include 
using namespace std;class father{public: void getHeight(){cout<
<
weight<
height<
age<

 

子类对象赋值给父类

#include 
using namespace std;class father{public: int height;};class son: public father{public: int weight;};int main(){ son a; father b; a.height=1; b=a;//子类赋给父类}

父类引用指向子类对象

#include 
using namespace std;class father{public: int height;};class son: public father{public: int weight;};int main(){ father *p; son jack; p=&jack; //父类引用指向子类对象 父类还可以作为子类的别名(虚函数) p->height=110; cout<
<

 

#include 
using namespace std;class father{protected: int height;};//私有派生用的不多,因为继承的成员都变成私有的了,不可以访问class son: private father{ //继承的父类的protected,public全部变成 privatepublic: int weight; int getHeight() { return height; } void setHeight(int x) { this->height=x; }};int main(){ son a ; a.setHeight(1); cout<
<

 

继承中构造函数的执行顺序(先构造基类,后构造子类, 先析构子类,在析构基类)

#include 
using namespace std;class father{private: int height;public: father(){cout<<"father construct"<

 

多重继承,以 继承的顺序进行构造

 

向基类构造函数传递参数

#include 
using namespace std;class father{public: int height; int weight;public: father(int height, int weight ){ this->height=height; this->weight=weight; cout<<"father construct"<
age=age; cout<<"son construct"<

 

多继承的歧义(作用域操作符)

#include 
using namespace std;//class father{////public:// int height;// int weight;//public:// father(int height, int weight ){// this->height=height;// this->weight=weight;// cout<<"father construct"<
age=age;// cout<<"son construct"<

 

解决两异性(虚基类)

#include 
using namespace std;class common{public: void stand(){}};class a: virtual public common{public: void hello(){cout<<"a hello"<

 

转载于:https://www.cnblogs.com/siqi/p/4591822.html

你可能感兴趣的文章
C++11中lock_guard和unique_lock的区别
查看>>
解决find命令报错: paths must precede expression
查看>>
LVS 手册学习
查看>>
Lua's performance
查看>>
seajs快速了解
查看>>
Java Spring MVC项目搭建(二)——项目配置
查看>>
Async分析
查看>>
js 组件化
查看>>
图的应用:哈密尔顿路径
查看>>
js计算日期相减天数
查看>>
MATLAB实现Catmull-Clark细分(CC细分)
查看>>
jquery 判断元素是否隐藏
查看>>
第一百九十五天 how can I 坚持
查看>>
Swift 入门之简单语法(五)
查看>>
多视几何——三角化求解3D空间点坐标
查看>>
Drag+Drop和MouseClick
查看>>
AWS RDS 使用笔记
查看>>
Puppeteer VS Puppeteer-core
查看>>
Rxjava 执行阻塞的原因分析 tolist() observable.from()等。
查看>>
[转载]解决TCP网络传输“粘包”问题
查看>>