用抽象类设计计算二维平面图形面积的程序
2013-11-16 18:26
1001 查看
在基类TDshape中设计纯虚函数area()和printName(),area()用于计算几何图形的面积,printName()用语打印输出几何图形的类名,如Triangle类的对象就打印输出“”Triangle“。每个具体形状的类则从抽象类TDshape派生,各自需要定义其独有的数据成员和成员函数,并且定义area()和printName()的具体实现代码。要求编写以TDshape为接口的函数,借以访问具体类如Triangle和Rectangle类的成员函数area(),printName()。
代码:
代码:
#include"iostream" using namespace std; class TDshape{ public: virtual double area()=0; virtual void printName()=0; }; class Triangle:public TDshape{ private: double width,height; public: double area(){ return (width*height/2); } void printName(){ cout << "Triangle" ; } void getwidth(double a=0){ width=a; } void getheight(double a=0){ height=a; } double setwidth(){ return width; } double setheight(){ return height; } }; class Rectangle:public TDshape{ private: double width,height; public: double area(){ return (width*height); } void printName(){ cout << "Rectangle" ; } void getwidth(double a=0){ width=a; } void getheight(double a=0){ height=a; } double setwidth(){ return width; } double setheight(){ return height; } }; void vpf(TDshape *p){ p->area(); p->printName(); cout << "Area = " << p->area() << endl; } void main() { Triangle t; t.getwidth(2); t.getheight(2); Rectangle r; r.getwidth(2); r.getheight(2); TDshape *td[2]; td[0]=&t; td[1]=&r; for(int i=0;i<2;i++) vpf(td[i]); }
相关文章推荐
- 用抽象类设计计算二维平面图形面积的程序,在基类TDshape中设计纯虚函数area()和printName()......C++的158页5题
- 抽象类设计计算二维平面图形面积
- 用抽象类设计计算二维图形面积的程序,在基类TDshape中设计纯虚函数area()和printName().are()用于计算图形的几何面积,printName()用于打印输出几何图形的类名
- AutoCAD中如何计算二维图形的面积
- Java经典编程300例之实例061 计算几何图形的面积(抽象类)
- 设计模式在二维图形程序里的应用(命令模式--command)
- 1、通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。 2、按以下要求编写程序
- java-计算几何图形的面积(抽象类)
- 用带参数的宏设计一个计算矩形面积的程序
- 用JAVA写一个已知半径,计算园周长,面积的程序
- 十三周任务四:设计一个抽象类CSolid,含有两个求表面积及体积的纯虚函数
- 程序设计中的计算复用(Computational Reuse)
- 利用Java中面向对象的思想设计一个计算圆、正方形以及长方形面积的类
- Dreamweaver杀手!Illustrator终结者?Flash的末日?图形图像设计程序之网页版
- 条件语句if 循环语句while 计算图形面积
- 定义一个图形类及其子类(三角形类和矩形类),分别计算其面积和周长。(第十周)
- 我的第一个Qt程序:使用Qt creator和Qt designer完成"HelloWorld"和计算圆面积的程序设计
- 算法模板——计算几何1(图形面积)
- 定义一个图形类及其子类(三角形类和矩形类),分别计算其面积和周长
- 十二周 计算图形的面积和周长