練習問題12.4抽象ベースクラスShapeを定義するプログラムを書き、それによって3つの派生クラス:Circle(円形)、Rectangle(矩形)、Triangle(三角形)を派生し、1つの関数printAreaでそれぞれ以上を出力する.
1028 ワード
C++プログラム設計(第三版)譚浩強習題12.4個人設計
練習問題12.4抽象ベースクラスShapeを定義するプログラムを書き、それによって3つの派生クラス:Circle(円形)、Rectangle(矩形)、Triangle(三角形)を派生し、1つの関数printAreaで以上の3つの面積をそれぞれ出力し、3つの図形のデータはオブジェクトを定義する際に与えられる.
コードブロック:
練習問題12.4抽象ベースクラスShapeを定義するプログラムを書き、それによって3つの派生クラス:Circle(円形)、Rectangle(矩形)、Triangle(三角形)を派生し、1つの関数printAreaで以上の3つの面積をそれぞれ出力し、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;
}