C++类继承方式及实践
直接上图:

以及:

实践如下:
#include <iostream>
using namespace std; class Father{ private:
int father1;
int getFather1Private(){
return this->father1;
} protected:
int father2;
int getFather2Protected(){
return this->father2;
}
public:
int father3;
Father(){
this->father1 = ;
this->father2 = ;
this->father3 = ;
cout<< "无参构造器"<<endl;
}
// 基类构造器要加virtual的原因:
// 这是因为当用基类引用派生类的时候,如果此时对基类进行delete操作,对于没有虚函数析构函数,那么只会调用基类的析构函数,而对派生类的析构函数不会进行析构
virtual ~Father(){
cout<< "Father析构函数"<<endl;
}
Father(int aa){
this->father1 = aa;
this->father2 = ;
this->father3 = ;
cout<< "有参构造器"<<endl;
}
int getFather1(){
return father1;
}
int getFather2(){
return father2;
}
int getFather3(){
return this->father3;
}
}; class Son : public Father{ private:
int son1; public:
Son(){
this->son1 = ;
}
~Son(){
cout<< "Son析构函数"<<endl;
}
// 通过这样的方式调用父类构造器
Son(int fatherV, int sonV):Father(fatherV){
Father::father3 = fatherV;
this->son1 = sonV; }
int method(){
int value = son1 * Father::father3;
return value;
}
int getSon1(){
return son1;
} }; int main(){ cout << "类继承实践:" << endl; // 调用无参构造器
Son son1; // 有参构造器
Son son(,); // 在类外部仅有父类的public可以访问
// error: cout << "son.getFather1Private():" << son.getFather1Private() << endl;
// error: cout << "son.getFather2Protected():" << son.getFather2Protected() << endl;
cout << "son.getFather1():" << son.getFather1() << endl;
cout << "son.getFather2():" << son.getFather2() << endl; cout << "son.father3:" << son.father3 << endl;
// error: cout << "son.father2:" << son.father2 << endl; cout << "son.getSon1():" << son.getSon1()<< endl; //cout << "son.father3:" << son.son1 << endl;
cout << "son.method():" << son.method() << endl; // https://blog.csdn.net/u014453898/article/details/60402586
// 这是因为当用基类引用派生类的时候,如果此时对基类进行delete操作,对于没有虚函数析构函数,那么只会调用基类的析构函数,而对派生类的析构函数不会进行析构
Father *ptr = new Son();
cout << "泛型:" << ptr->father3 << endl;
delete ptr; cout << "类继承 实践 end." << endl; return ;
}
C++类继承方式及实践的更多相关文章
- threading 多线程类继承方式调用
import threading #线程import time class Work(threading.Thread): def __init__(self,n): threading.Thread ...
- C++学习笔记(十二):类继承、虚函数、纯虚函数、抽象类和嵌套类
类继承 在C++类继承中,一个派生类可以从一个基类派生,也可以从多个基类派生. 从一个基类派生的继承称为单继承:从多个基类派生的继承称为多继承. //单继承的定义 class B:public A { ...
- C++中的类继承(1) 三种继承方式
继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承是指一个对象直接使用另一对象的属性和方法.继承呈现了 面向对象程序设 计的层次结构, 体现了 由简单到复杂的认知过程. ...
- JS类继承常用方式发展史
JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...
- Object-C的类可以多重继承吗?可以实现多个接口吗?category是什么?重写一个类的方式用继承好还是分类好,为什么?
Object-C的类可以多重继承吗?可以实现多个接口吗?category是什么?重写一个类的方式用继承好还是分类好,为什么? 答:Object-c的类不可以多重继承,可以实现多个接口(协议),Cate ...
- mfc 类三种继承方式下的访问
知识点 public private protected 三种继承方式 三种继承方式的区别 public 关键字意味着在其后声明的所有成员及对象都可以访问. private 关键字意味着除了该类型的创 ...
- 理解C++类的继承方式(小白)
基类里的 public(大人) protect(青年) private(小孩) 在通过继承时 继承方式public(我是大人咯) protect(我是青少年) private(我系小孩纸啦) &qu ...
- C++ 类中的3种访问权限和继承方式
访问权限:public 可以被任意实体访问,protected 只允许子类(无论什么继承方式)及本类的成员函数访问,private 只允许本类的成员函数访问.三种继承方式分别是 public 继承,p ...
- C++ 中三种继承方式的理解
一.公有继承(public inheritance) 1.概念:当类的继承方式为公有继承时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可以直接访问. 实验一下: 我们用代 ...
随机推荐
- upupw : Apache Php5.5 的使用
1. 官网下载 1. 官网下载 apache php5.5点击下载 但是 现在有时候打不开,所以提供以下方法 2. 百度云网盘下载 https://pan.baidu.com/s/1eQ2k1Su ...
- Linux上ssh免秘钥互登
两台机器分别为:master:192.168.1.10sever1:192.168.1.20 1.检查机器名和连通性 a.查看/etc/hostname [root@master master]# m ...
- linux下mysql忘记密码解决方案
一.写随笔的原因:之前自己服务器上的mysql很久不用了,忘记了密码,所以写一下解决方案,以供以后参考 二.具体的内容: 1. 检查mysql服务是否启动,如果启动,关闭mysql服务 运行命令:ps ...
- 20、Nginx高可用架构
1.Keepalived高可用基本概述 1.1.什么是高可用 一般是指2台机器启动着相同的业务系统,当有一台机器down机了, 另外一台服务器能快速的接管, 对于访问的用户是无感知的. 1.2.高可用 ...
- Linux系统下C语言获取Time
获取时间的函数有很多,具体包括如下: time()/gettimeofday()等等,下面是获取具体到usecond的时间程序: #include <iostream> #include ...
- 记一次启动Tomcat 控制台以及log4j 乱码问题
Tomcat启动乱码 问题描述:当你发现你的Tomcat启动时乱码了,而你只是换了个Tomcat版本而已. 在找到真正的问题之前,我在网上百度了N多的资料,都试过了,但是都不行.1.修改了 windo ...
- deep_learning_Function_bath_normalization()
关于归一化的讲解的博客——[深度学习]Batch Normalization(批归一化) tensorflow实现代码在这个博客——[超分辨率]TensorFlow中批归一化的实现——tf.layer ...
- tornada-数据库
数据库 torndb安装 连接初始化 执行语句 execute execute_rowcount 查询语句 get query 与Django框架相比,Tornado没有自带ORM,对于数据库需要自己 ...
- Python测试开发必知必会-PEP
互联网发展了许多年,不仅颠覆了很多行业,还让很多职位有了更多的用武之地.产品发布迭代速度不断加快,让测试开发这个岗位简直火得不要不要的. Python语言,作为一种更接近人来自然语言的开发语言,以简洁 ...
- Itextpdf + Adobe Acrobat DC填充模板生成pdf快速入门
Itextpdf + Adobe Acrobat DC填充模板生成pdf快速入门 生成pdf有很多种方法,如通过freemarker,或 使用itextpdf.本文将使用itextpdf生成pdf 1 ...