21-22.关于类的继承与派生
来源:互联网 发布:淘宝9秒视频制作软件 编辑:程序博客网 时间:2024/05/16 04:59
#include <iostream> 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;}
多继承
#include <iostream> using namespace std; // 基类 Shapeclass Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height;}; // 基类 PaintCostclass 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;}
阅读全文
0 0
- 21-22.关于类的继承与派生
- 关于c++中类的继承与派生
- 类的继承与派生中关于构造函数
- 类的继承与派生
- 类的继承与派生
- 类的继承与派生
- 类的继承与派生
- 类的继承与派生
- 类的继承与派生
- 类的派生与继承
- 类的继承与派生
- 类的继承与派生
- 类的派生与继承
- 类的继承与派生
- 关于继承与派生的小结
- 派生类与继承
- 继承与派生类
- 继承与派生2:派生类的定义
- openGL ES学习二
- purge的用法
- 利用spring session解决共享Session问题
- duilib各种布局的作用,相对布局与绝对布局的的意义与用法
- 面试怎么能说这种话?失败率奇高!
- 21-22.关于类的继承与派生
- PEP8 Python 编码规范整理
- js递归调用注意return返回值
- 【FFmpeg】FFmpeg常用基本命令
- android 如何判断一个 View textview imageview etc 是否绘制完成
- errno 含义表
- JS正则
- Contect、SharedPreferences及Intent学习小记
- 树的重心