[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 ...
随机推荐
- Docker系列(三)Dockerfile 离线构建镜像
一.Dockfile介绍: Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令.Docker程序将这些Dockerfil ...
- TypeScript 学习资料
TypeScript 学习资料: 学习资料 网址 TypeScript Handbook(中文版)(推荐) https://m.runoob.com/manual/gitbook/TypeScript ...
- goroutine 知识点
goroutine: 协程是用户态的轻量级线程: 协程之间,通过消息传递进行通信(大多数语言通过共享内存进行通信) select 使用的场景: 正常处理业务的 routine 退出(超时.不满足某条件 ...
- vs2015 无法连接到已配置的开发web服务器
问题一:vs2015 无法连接到已配置的开发web服务器 最靠谱方法如下:(和防火墙开启无关联, 注册表增加什么键值无关联 , 缺失asp.net core.Dll文件无关联 ) 分析,因为前提是你的 ...
- 【Oracle】group by 和partition by的区别
总结: group 单纯分组 partition 也能分组,但还具备累计的功能 order by 排序,与计算函数联用,需要累加计算 0.select * from test; ---测试数据 ...
- XML的介绍使用
一.什么是XML? XML,Extensible Markup Language,扩展性标识语言.文件的后缀名为:.xml.就像HTML的作用是显示数据,XML的作用是传输和存储数据. 据说,java ...
- h5课件是什么?h5(html5)怎样实现交互动画开发?-----浅谈h5交互动画课件的优势
目前很多交互课件,尤其幼儿类的交互课件以动画和交互相结合的类型居多,越来越多的教育机构发现了这种课件对于幼儿的吸引力远大于其他类型的课件,随着flash逐渐被市场淘汰,动画和交互相结合的html5跨平 ...
- C#中CefSharp的简单使用
1. 创建32位winform项目 必须指定32位或64位 这里使用32位 2. 下载CefSharp相关文件 3. 复制CefSharp相关文件到项目debug目录并添加引用 https://blo ...
- [UE4]Spline
Spline和Spline Mesh的区别: 1.Spline Mesh是有实体表现的,Spline Mesh可以拉伸弯曲实体模型,Spline Mesh是具象. 2.Spline 只有曲线,没有实体 ...
- springmvc读取服务器磁盘图片,显示于前台页面
在项目中的config目录下有一个文件,在后台程序中获取 它并使用. springmvc提供一个方法:File file = new ClassPathResource("NonTaxVou ...