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.概念:当类的继承方式为公有继承时,基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可以直接访问. 实验一下: 我们用代 ...
随机推荐
- 第十篇.2、python并发编程之多进程
一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...
- matplotlib:python数据处理三剑客之一
1.基本使用 import numpy as np import matplotlib.pyplot as plt import pandas as pd # 生成一系列x x = np.linspa ...
- Coinbase 雇员被 Firefox 0day 漏洞攻击
Firefox 刚刚修复的 0day 漏洞被用于攻击 Coinbase 雇员.Coinbase 安全团队的 Philip Martin 称,攻击者组合利用了两个 0day 漏洞,其一是远程代码执行漏洞 ...
- 反selenium关键字
webdriver __driver_evaluate __webdriver_evaluate __selenium_evaluate __fxdriver_evaluate __driver_un ...
- Linux下putenv()函数导致composer更新失败
bug复现: 原因: putenv() 函数设置特定的环境变量有可能是一个潜在的安全漏洞,所以这个函数在php配置文件中是默认禁止的,在 php.ini 中查找此函数,然后将此函数删除掉,重载配置即可 ...
- IO模型(epoll)--详解-03
写在前面 epoll是开发linux高性能服务器的必备技术至,epoll本质,是服务端程序员的必须掌握的知识. 七.epoll的原理和流程 本节会以示例和图表来讲解epoll的原理和流程. 创建epo ...
- SQL SERVER 2012安装配置说明(多图详解)
1. 优先安装软件 1. net framework3.5. 2. 在安装SQL SERVER 2012前需要3.5的支持.在WIN 2012系统可以在系统管理的添加角色和功能中安装,如下将[.NET ...
- LogHelper
原文链接 public class LogHelper { static string strLogCOMPath = Directory.GetCurrentDirectory() + " ...
- django 路由层(反向解析)03
目录 ORM表关系建立 Django请求生命周期流程图 urls.py 路由层 无名分组 有名分组 反向解析 无名分组的反向解析 有名分组的反向解析 以编辑功能为例 路由分发 名称空间 伪静态 虚拟环 ...
- C风格函数
很多C风格的函数用起来非常舒适,例如: if(access(sPath, 0) == 0){ ://检测文件是否存在 } 用来测试文件存在与否,以及读写权限. 而他有宽字节版与ascii码版 宽版 _ ...