REF:

http://www.runoob.com/cplusplus/cpp-inheritance.html

一、基类和派生类

程序:

#include "stdafx.h"
#include <iostream>
//#include <string>
//#include <vector>
//#include <cctype>
//#include <cstring> //using std::string;
//using std::vector;
//using std::isalpha;
//using std::cin;
using std::cout;
using std::endl;
using namespace std; //基类
class Shape
{
public:
void setWidth(int w)
{
width = w;
} void setHeight(int h)
{
height = h;
} protected:
int width;
int height;
}; //派生类
class Rectangle :public Shape
{
public:
int getArea()
{
return (width*height);
} }; int main(void)
{
Rectangle Rect; Rect.setWidth(5);
Rect.setHeight(7); //输出对象面积
cout << "Total area:" << Rect.getArea() << endl; return 0;
}

执行结果:

二、访问控制和继承

三、继承类型

四、多继承

C++ 类可以从多个类继承成员

程序:

#include <iostream>

using namespace std;

// 基类 Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
}; // 基类 PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
}; // 派生类
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
}; int main(void)
{
Rectangle Rect;
int area; Rect.setWidth(5);
Rect.setHeight(7); area = Rect.getArea(); // 输出对象的面积
cout << "Total area: " << Rect.getArea() << endl; // 输出总花费
cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0;
}

执行结果:

Total area: 35
Total paint cost: $2450

  

[RUNOOB]C++继承的更多相关文章

  1. Kotlin 继承

    Kotlin 中所有类都继承该 Any 类,它是所有类的超类,对于没有超类型声明的类是默认超类: class Example // 从 Any 隐式继承 Any 默认提供了三个函数: equals() ...

  2. Exception类的学习与继承总结

    日期:2018.11.11 星期日 博客期:023 Exception类的学习与继承总结 说起来我们上课还是说过的!老师提到了报错问题出现主要分Exception和Error两类!第一次遇见这个问题是 ...

  3. java-线程(runoob.com)

    参考链接: https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblogs.com/dolphin0520/p/3920373.h ...

  4. CSS 基础 优先级 选择器 继承

    1.样式优先级 (内联样式)Inline style     >    (内部样式)Internal style sheet     >     (外部样式)External style ...

  5. python类的继承-1

    #!/usr/bin/python3 #类定义 class people: #定义基本属性 name = '' age = 0 #定义私有属性,私有属性在类外部无法直接进行访问 __weight = ...

  6. 从Runoob的Django教程学到的

    Windows 10家庭中文版,Python 3.6.4,Django 2.0.3 这个月开始学习Django,从网上找到了RUNOOB.COM网站找到了一份Django教程,在“认真”学习之后,初步 ...

  7. Java 学习笔记 ------第六章 继承与多态

    本章学习目标: 了解继承的目的 了解继承与多态的关系 知道如何重新定义方法 认识java.lang.object 简介垃圾回收机制 一.继承 继承是java面向对象编程技术的一块基石,因为它允许创建分 ...

  8. Java-Runoob-面向对象:Java 继承-u1

    ylbtech-Java-Runoob-面向对象:Java 继承 1.返回顶部 1. Java 继承 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类 ...

  9. Django模板继承后出现logo图片无法加载的问题

    父文件:index.html <!DOCTYPE html> <html lang="en"> <head> <title>{% b ...

随机推荐

  1. day059-60 ajax初识 登录认证练习 form装饰器, form和ajax上传文件 contentType

    一.ajax 的特点 1.异步交互:客户端发出一个请求后,需要等待服务器响应结束后, 才能发出第二个请求 2.局部刷新:给用户的感受是在不知不觉中完成请求和响应过程. 二.ajax 模板示例 ($.a ...

  2. 图像三维灰度分布图——matlab

    p=imread('C:\Users\wangd\Documents\MATLAB\1.jpg'); g=rgb2gray(p); % 转为灰阶图 gg=double(g); % 转为数值矩阵 gg= ...

  3. Linux上静态库和动态库的编译和使用

    linux上静态库和动态库的编译和使用(附外部符号错误浅谈) 这就是静态库和动态库的显著区别,静态库是编译期间由链接器通过include目录找到并链接到到可执行文件中,而动态库则是运行期间动态调用,只 ...

  4. CSS3 Vendor-prefixing

    Browser vendors needed a way to add support for new features that were not yet standardized, but wit ...

  5. (转)PHP线程安全与非线程安全的区别:如何选择用哪一个?

    PHP线程安全与非线程安全的区别:如何选择用哪一个? 很多时候,我们在做PHP环境配置的时候,很多人都是直接去乱下载PHP版本的,但是他不清楚:从2000年10月20日发布的第一个Windows版的P ...

  6. 2018-2019-2 20165312《网络攻防技术》Exp3 免杀原理与实践

    2018-2019-2 20165312<网络攻防技术>Exp3 免杀原理与实践 课上知识点总结 1.恶意软件检测机制 基于特征码的检测(需要定期更新病毒库) 启发式恶意软件检测(实时监控 ...

  7. 2019年1月16日22:50:28 白糖SR1905

    很好的机会,只拿了点皮毛,如果说都是因为上班时间不充裕那是给自己找借口,最主要原因没别的:思维不清,策略不明- 这里的入场初衷是周线区间下沿,日线向下脱离中枢失败后回拉一笔,那么这一单的做法就应该很明 ...

  8. OPNET下op_pk_copy()函数使用注意事项

    1)op_pk_copy()是生成新的数据包,函数op_pk_create_time_get()获取的是新数据包的生成时间.在统计数据包的端到端时延,以及服务时延需要注意. 2)此外发用数据包时使用o ...

  9. Python-10 字典dict

    #1 创建 dict1={'欢欢':'i love','小高高':'you'} dict2={1:'one',2:'two',3:'three'} dict3={} #2 访问元素 print('欢欢 ...

  10. flutter 列表展示

    内容: 1.列表展示 2.轮播图 3.其他 本次的内容也是在上一节的基础上进行操作 我们就搞这个story模块. 目录: story.dart  story主页面 import 'package:fl ...