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. 关于library cache lock和row cache lock产生的常见原因

    这两个等待事件其实很少出现在top5列表中,一般都没什么印象,在此整理记录以便以后查阅. 常见的library cache lock产生的原因在<高级OWI与Oracle性能调查>这本书和 ...

  2. 导入tensorflow时DLL load failed: 找不到指定的模块

    简单暴力:卸载 重装 方法一: 先删除:pip uninstall tensorflow 再下载:pip install tensorflow 方法二: 也有可能是numpy版本不匹配的问题: 卸载: ...

  3. Javascript神器之webstorm

    推荐个编辑器主题下载的一个网站. Color Themes    网址:http://color-themes.com [点这里直接跳转] 但是,只支持几个编辑器. 各种颜色搭配的主题,随你选择!我个 ...

  4. Optaplanner终于支持多线程并行运行 - Multithreaded incremental solving

    Optaplanner 7.9.0.Final之前,启动引擎开始对一个Problem进行规划的时候,只能是单线程进行的.也就是说,当引擎对每一个possible solution进行分数计算的过程中, ...

  5. React 生命周期及使用场景

    对比版本:16.4.0 VS 16.3.0 VS 16.2.0 发现最近几次React版本更改比较大,在为17.0的大版本作准备.总结了一下React生命周期函数的变化. 综合对比图如下: 各版本分别 ...

  6. C++ 重定义、重载、覆盖

    想要用好C++继承和类自身函数实现就必须了解C++得三个概念重定义(redefine).重载(overload).重写(override). 一 基本感念 1 重定义(redefine) 派生类对基类 ...

  7. JavaScript最后的课程笔记

    一.快捷位置和尺寸 DOM已经提供给我们计算后的样式,但是还觉得不方便,所以DOM又提供给我们一些API: ele.offsetLeft ele.offsetTop ele.offsetWidth e ...

  8. 理解OpenShift(6):集中式日志处理

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  9. IDEA右侧 Maven oracle依赖包有红色波浪线

    1\下载 ojdbc14-10.2.0.4.0.jar http://www.java2s.com/Code/Jar/o/Downloadojdbc14102040jar.htm 2.将ojdbc14 ...

  10. qcom,msm8996-pinctrl.txt

    Qualcomm Technologies, Inc. MSM8996 TLMM block This binding describes the Top Level Mode Multiplexer ...