// ConsoleApplication5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#define_CRTDBG_MAP_ALLOC#include<stdlib.h>#include<crtdbg.h>#include<iostream>usingnamespace std;//抽象类classShape{protected://数据成员:价格和面积double price;double area;public://构造函数Shape():price(100),area(0){}//虚析构函数virtual~Shape(){printf("%s\n","Delete shape");}//纯虚函数:获取图形描述和获取价格virtualvoidgetDescription()=0;virtualvoidgetPrice()=0;};//接口classDraw{public://虚析构函数virtual~Draw(){printf("%s\n","Delete Draw");}//纯虚函数:输出图形周长virtualvoiddrawLen()=0;};//具体类classCircle:publicShape,publicDraw{private:double radius;public:Circle(double r):radius(r){ area =3.14* radius * radius; price =100+ area *6;}~Circle(){printf("%s%f\n","Delete circle with radius ",radius);}voidgetDescription(){printf("%s%f\n","Circle with radius ",radius);}voidgetPrice(){printf("%s%f%s%f\n","Circle with area ", area," price ",price);}voiddrawLen(){printf("%s%f\n","Circle with len ",2*3.14* radius);}};intmain(){
Circle c(5.0);
Shape* s =&c;//基类(Shape)指针指向子类(Circle)对象
s->getDescription();
s->getPrice();
Draw* d =&c;//基类(Draw)指针指向子类(Circle)对象
d->drawLen();_CrtDumpMemoryLeaks();return0;}