习题 12.4 写一个程序,定义抽象基类Shape,由它派生出3个派生类:Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上。。。

C++程序设计(第三版) 谭浩强 习题12.4 个人设计

习题 12.4 写一个程序,定义抽象基类Shape,由它派生出3个派生类:Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。

代码块:

#include 
#include 
using namespace std;
//基类Shape
class Shape
{
public:
	Shape(){}
	virtual ~Shape(){}
	virtual void printArea() const{}
	virtual void shapeName() const =0;
};
class Circle: public Shape
{
public:
	Circle(double r){radius=r;}
	~Circle(){}
	virtual void printArea() const {cout<shapeName();
	pt->printArea();
	pt=&rec;
	pt->shapeName();
	pt->printArea();
	pt=&tr;
	pt->shapeName();
	pt->printArea();
	system("pause");
	return 0;
}

你可能感兴趣的:(C++程序设计,(第三版),谭浩强,课后答案)