[RUNOOB]C++继承
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++继承的更多相关文章
- Kotlin 继承
Kotlin 中所有类都继承该 Any 类,它是所有类的超类,对于没有超类型声明的类是默认超类: class Example // 从 Any 隐式继承 Any 默认提供了三个函数: equals() ...
- Exception类的学习与继承总结
日期:2018.11.11 星期日 博客期:023 Exception类的学习与继承总结 说起来我们上课还是说过的!老师提到了报错问题出现主要分Exception和Error两类!第一次遇见这个问题是 ...
- java-线程(runoob.com)
参考链接: https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblogs.com/dolphin0520/p/3920373.h ...
- CSS 基础 优先级 选择器 继承
1.样式优先级 (内联样式)Inline style > (内部样式)Internal style sheet > (外部样式)External style ...
- python类的继承-1
#!/usr/bin/python3 #类定义 class people: #定义基本属性 name = '' age = 0 #定义私有属性,私有属性在类外部无法直接进行访问 __weight = ...
- 从Runoob的Django教程学到的
Windows 10家庭中文版,Python 3.6.4,Django 2.0.3 这个月开始学习Django,从网上找到了RUNOOB.COM网站找到了一份Django教程,在“认真”学习之后,初步 ...
- Java 学习笔记 ------第六章 继承与多态
本章学习目标: 了解继承的目的 了解继承与多态的关系 知道如何重新定义方法 认识java.lang.object 简介垃圾回收机制 一.继承 继承是java面向对象编程技术的一块基石,因为它允许创建分 ...
- Java-Runoob-面向对象:Java 继承-u1
ylbtech-Java-Runoob-面向对象:Java 继承 1.返回顶部 1. Java 继承 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类 ...
- Django模板继承后出现logo图片无法加载的问题
父文件:index.html <!DOCTYPE html> <html lang="en"> <head> <title>{% b ...
随机推荐
- 2018-2019-2 网络对抗技术 20165308 Exp2 后门原理与实践
2018-2019-2 网络对抗技术 20165308 Exp2 后门原理与实践 1.实验内容 (3.5分) (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用soc ...
- C putchar() 和 getchar()
C 库函数 int getchar(void) 从 终端输入获取一个字符 : 返回值:该函数以无符号 char 强制转换为 int 的形式返回读取的字符,如果到达文件末尾或发生读错误,则返回 EO ...
- mysql用户管理及授权
以mariadb5.5版本为例 新建用户 登录mariadb # mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Com ...
- python之format函数
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...
- VisualSVN设置提交时必须输入日志信息
VisualSVN设置提交时必须输入日志信息 1.svn提交时强制输入提交信息 为了阻止SVN提交空日志信息和垃圾文件可以在SVN服务器端强制必须填写日志信息,这时需用到pre-commit钩子脚本. ...
- 服务器对接码云webhooks
服务安装git php代码(外网必须可以访问) <?php //本地路径滚adminasdfdasfasdf $file = "/wwwroot/webhooksLog.txt&quo ...
- Ubuntu平台rm误删的文件如何恢复
安装:Ubuntu下也可以直接用apt-get来获取extundelete 以我自己的Ubuntu14.04.3来看: df 命令是linux系统上以磁盘分区为单位来查看文件系统的命令,后面可以加上不 ...
- Windows下安装配置Flutter
Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面. Flutter可以与现有的代码一起工作.在全世界,Flutter正在被越来越多的开发者和组织使用,并且 ...
- 聊一聊isinstance与type
聊一聊isinstance与type 最近写代码的时候遇到了一个关于isinstance与type的坑,这里给大家分享下,如果大家也遇到了同样的问题,希望本文能为大家解决疑惑把. isinstance ...
- MySQL binlog_format中sbr 和rbr(Statement-Based and Row-Based Replication)的优缺点
Advantages of statement-based replication 1 技术成熟 2 对于大量的更新删除等操作,仅仅会写入少量的变更结果,加速日志获取或者备份的速度 3 日志文件包含了 ...