// Shape. h. In the header file folder # include <iostream> using namespace STD; # define PI 3.1415926 class shape {public: Virtual void print () = 0; virtual float area () = 0 ;}; class circle: Public shape {float R; public: Circle (float R); void print (); float area () ;}; class rectangle: public shape {float length, width; public: rectangle (float length, float width); void print (); float area ();};
// Shape. place CPP in the source file folder # include "shape. H "circle: Circle (float R) {This-> r = r;} void circle: Print () {cout <"the r of this circle is" <r <Endl;} float circle: Area () {return (float) pI * r ;} rectangle: rectangle (float length, float width) {This-> length = length; this-> width = width;} void rectangle: Print () {cout <"the length is" <length <Endl <"the width is" <width <Endl;} float rectangle: Area () {return length * width ;}
// Put main into the source file folder # include "shape. H "int main () {shape * s; S = new circle (442); s-> Print (); cout <"The area of the circle is" <s-> area () <Endl; Delete s; S = new rectangle (10, 5 ); s-> Print (); cout <"The area of the rectangle is" <s-> area () <Endl; Delete s; S = NULL; int N; cin> N; return 0 ;}
Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.