C++学习笔记--抽象基类
来源:互联网 发布:什么牌子微波炉 知乎 编辑:程序博客网 时间:2024/05/30 05:15
There is another solution: You can abstract from the Ellipse and Circle classes what they have in common and place those feature in an Abstract Base Class.
椭圆和圆有很多相似的地方,但是圆又不能直接从椭圆中继承,比较好的解决方法是把它们共有的部分放到抽象基类中。
When a class declaration contains a pure virtual function, you can't create an object of that class.
当一个类含有纯虚函数时,你不能创建那个类的对象。
#include <iostream>using namespace std;// Base classclass Shape{public: // Pure virtual function providing interface framework. virtual int getArea() = 0; void setWidth(int w) { width = w; } void setHeight(int h) { height = h; }protected: int width; int height;};// Derived classesclass Rectangle : public Shape{public: int getArea() { return (width * height); }};class Triangle : public Shape{public: int getArea() { return (width * height) / 2; }};int main(void){ Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0;}输出:
wang@wang:~/c++$ ./a.out
Total Rectangle area: 35
Total Triangle area: 17
0 0
- C++学习笔记--抽象基类
- c#学习笔记 --接口和抽象类的区别?
- 抽象类学习笔记
- 【学习笔记】抽象类
- C++学习笔记14: 抽象基类
- C++学习笔记-抽象容器类型
- 【C++】【学习笔记】【024】抽象方法
- C#:抽象类的学习
- 抽象类的学习笔记
- j2se学习笔记-抽象类
- JAVA学习笔记--抽象类
- Java学习笔记-抽象类
- .Net C#(类的继承/虚函数/抽象函数/密封类/静态类/抽象类)——学习笔记
- C++primer学习:面向对象程序设计(2):抽象基类
- 【从C到C++学习笔记】类声明/公有私有保护成员/数据抽象和封装
- java学习笔记7--抽象类与抽象方法
- 【Java学习笔记】接口 抽象类 抽象方法
- C#回顾学习笔记三十一:抽象类与抽象方法
- web前端-JavaScript 变量 -006
- 网页数据提取---雅虎网站
- 第三节基本开发控件介绍
- C语言的学习整理
- 孩子,我们回家
- C++学习笔记--抽象基类
- 设计模式——空设计模式
- Android数据加密之异或加密算法
- Web静态资源处理方案
- 2016 上半年 VR 报告:潜在用户4.5亿
- Flume-ng 例子
- 独立版本TortoiseSVN的安装和使用
- dispatch_group_t使用
- 一篇关于windows系统U盘安装的总结