码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 7_5

时间:2016-06-02 19:36:13      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<iostream>
 2 using namespace std;
 3 const double PI=3.14;
 4 
 5 class Shape
 6 {
 7 public:
 8     Shape (float a,float b=0.0){this->a=a;this->b=b;}
 9 protected:
10     float a,b;
11 };
12 
13 class Rectangle:public Shape
14 {
15 public:
16     Rectangle(float l,float w):Shape(l,w){}
17     float getArea(){return a*b;}
18 };
19 
20 class Circle:public Shape
21 {
22 public:
23     Circle(float r):Shape(r){}
24     float getArea(){return a*a*PI;}
25 };
26 
27 class Square:public Rectangle
28 {
29 public:
30     Square(float l):Rectangle(l,l){}
31     float getArea(){return a*a;}
32 };
33 
34 
35 int main()
36 {
37     Rectangle R(3,4);
38     Circle C(5);
39     Square S(5);
40     cout<<"The Rectangle area is "<<R.getArea()<<endl;
41     cout<<"The Circle area is "<<C.getArea()<<endl;
42     cout<<"The Square area is "<<S.getArea()<<endl;
43     return 0;
44 }

 

C++ 7_5

标签:

原文地址:http://www.cnblogs.com/PLifer/p/5553938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!