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. Mysql ssl 连接

    在Azure创建了一个Mysql5.7服务,因为默认使用ssl连接,需要下载Azure的证书,并使用openssl生成客户端的证书.具体流程参考官方文档 大致步骤: 下载根证书, 安装openssl, ...

  2. Mediawiki PlantUML Graphviz 图片 中文 乱码

    安装Mediawiki 的  PlantUML  Graphviz   插件后,生成图片时,中文成乱码问题. 环境:Ubuntu 16.04 MediaWiki 1.31.1 PHP 7.0.32-0 ...

  3. Element UI toggleRowExpansion用法

    背景: 官方说明文档没有具体代码示例 一.官方文档 方法名: toggleRowExpansion 说明: 用于可展开表格,切换某一行的展开状态,如果使用了第二个参数,则是设置这一行展开与否(expa ...

  4. xmind 8 便携版:关联文件后,双击打开文件,在当前文件夹产生configuration子文件的问题解决办法

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.xmind] @="XMind.Workbook.3" " ...

  5. MVC object htmlAttributes,IDictionary<string, object> htmlAttributes 写法

    MVC object htmlAttributes:new {style="color:red",width="12px",height="10px& ...

  6. Solr4.7.0连接MySQL

    1.把Mysql的Jar包  例如:mysql-connector-java-5.1.8-bin.jar  或其他版本 放到D:\apache-tomcat-7.0.57\webapps\solr\W ...

  7. 学会学习:高效学习方式(使用vscode-snippet有感)

    入职以来我们团队一直都在使用vscode编辑器,后来也有人开始使用webstorm.很久之前我突然为每天重复的编写.vue文件里面的export.<script lang="scss& ...

  8. CentOS 与Ubuntu 下配置IP地址

    1.CentOS配置方法如下: 用vi打开配置文件 [root@haha3 ~]#  vi /etc/sysconfig/network-scripts/ifcfg-eth0 写入以下配置 DEVIC ...

  9. centos7 Ruby环境变量配置

    ruby安装参考博客:https://blog.csdn.net/yelllowcong/article/details/78362370  (Redis之集群redis-trib.rb环境的搭建-y ...

  10. note 8 字符串

    字符串String 一个字符的序列 使用成对的单引号或双引号括起来 或者三引号""" 和 ''' 表示块注释 字符串运算 长度 len()函数 first_name = ...