通过继承机制,可以利用已有的数据类型来定义新的数据类型。
所定义的新的数据类型不仅拥有新定义的成员,而且还同时拥有旧的成员。
我们称已存在的用来派生新类的类为基类(base class),又称为父类。由已存在的类派生出的新类称为派生类(derived class),又称为子类。
继承的通用形式:
class derived-class:access base-class{
//
}
access 是可选的,
- 默认的话,是private (派生类是class). 或 public (派生类是struct).
- 如果使用的话,必须是 public,private 或 protected.
基类的访问控制
#include <iostream>
using namespace std;
class base_class{
int i,j;
public:
void set(int a, int b){
i=a;
j=b;
}
void show(){
cout<<i<<" "<<j<<"\n";
}
};
class derived_class:public base_class{
int k;
public:
derived_class(int x){
k=x;
}
void show_k(){
cout<<k<<"\n";
}
};
int main(){
derived_class ob(3);
ob.set(1,2); //访问基类的成员函数.
ob.show(); // 将输出 1 2
ob.show_k(); //访问派生类的成员函数. 将输出 3
return 0;
}
使用保护成员 (protected)
- protected成员可以被同类中的其他成员访问,也可以被派生类的成员访问。
Q.What's the difference between public, protected, and private members of a class ?
A.Private members are accessible only by (1)members and (2)friends of the class.
Protected members are accessible by (1)members and (2)friends of the calss and by (3)members and friends of derived classes.
Public members are accessible by everyone.
多重继承
-派生类可以从两个或多个基类中继承.
#include <iostream>
using namespace std;
class base_class1{
public:
base_class1(){
cout<<"Constructing base_class1\n";
}
~base_class1(){
cout<<"Destructing base_class1\n";
}
};
class base_class2{
public:
base_class2(){
cout<<"Constructing base_class2\n";
}
~base_class2(){
cout<<"Destructing base_class2\n";
}
};
class derived_class:public base_class1,public base_class2{
public:
derived_class(){
cout<<"Constructing derived_class\n";
}
~derived_class(){
cout<<"Destructing derived_class\n";
}
};
int main(){
derived_class ob;
return 0;
}
输出结果:
Constructing base_class1
Constructing base_class2
Constructing derived_class
Destructing derived_class
Destructing base_class2
Destructing base_class1
分享到:
相关推荐
return this 马克-to-win Java视频this指针的详细介绍
C++中按值返回和返回值优化的程序代码
这是一个电脑鼠走迷宫(大迷宫)中原路返回的程序,驱动也做过一定的调整,可用于测试
Lab2-Return-To-Libc,包含代码、实验说明、相关论文。
A novel packet format with non-return-to-zero (NRZ) label and duobinary carrier-suppressed return-to-zero (DCS-RZ) payload is proposed for optical packet switching networks. NRZ label is followed by ...
Bypassing non-executable-stack during exploitation using return-to-libc.pdf
Return-to-libc攻击实验-内含源码以及设计说明书(可以自己运行复现).zip
Return to a healthy L-term growth trend.pdf
复旦大学_软件安全_SEED labs_2-Return_to_libc实验 是从雪城大学SEED labs上找的实验 资源包括:原始文件夹、攻击代码、实验报告详细版
(b) the name of the table, the names of the table's attributes, the data types of the table's attributes, the formats of the table's attributes, and the maximum number of rows that the table can have...
us-18-Boeck-Young-Return-Of-Bleichenbachers-Oracle-Threat 安全分析 网络信息安全 安全 数据安全 金融安全
这是一套自用的主题,我对于wordpress的关注已经有很长时间了,一直以来...开发了这套针对设计师与开发者的轻量简洁风格的wordpress主题【return】,在这免费分享给我的设计师与开发者朋友们,同时也为开源社区做点贡献
$ npm install react-basic-tabs 用法 var React = require ( 'react' ) var TabPanel = require ( 'react-basic-tabs' ) var App = React . createClass ( { getInitialState : function ( ) { return { ...
使用CommandsSource时如何返回DialogResult 此代码示例描述了在使用时如何返回 。 允许您设置自定义对话框按钮。 传递一个对象集合到属性自定义按钮添加到DXDialogWindow。 将按钮的属性设置为的值,以为自定义...
set the safe parmeter to False 解决: return JsonResponse({“name”: “tom”}, safe=False) 增加safe=false,使其接受列表 补充知识:python 里面 JsonResponse (book_list,safe=False) 代码为: # 查询...
语言:English Instagram已删除查看喜欢和评论计数的功能。 我们再次提供它们。 Instagram已停止在某些区域显示喜欢和评论的数量-这使社交媒体用户的生活变得非常复杂,因此我们考虑伸出援手。 该扩展程序显示...
该项目旨在创建古代Mechanik火车模拟游戏的副本。
large datasets of SIFT and GIST vectors demonstrate that because of the denser subdivision, inverted multi-indices are able to return much shorter candidate lists with higher recall. Augmented with a ...
基本递延是管理异步调用的方法。 Basic deferred是在考虑简单性的前提下开发的延迟库。 是一种工厂函数,它返回一个可链接的实用程序对象,该对象具有将多个回调注册到回调队列,调用... } return deferred.promise();}
return-to-abort() - 使用代码内省来防止堆栈崩溃