10.2形状クラスの中の純虚関数


/*
* Copyright (c) 2014,          
* All rights reserved.
*       :  
*     :2014   5   28  
*      :v1.0
*     :  
*     :
*            】     ,      Shape,     3    ,
*Circle(  )、Rectangle(  )、Triangle(   )。    main()  ,
*              。
*     : 
*     : 
*     : 
*/
#include <iostream>
#define pi 3.14
using namespace std;

class Shape
{
    public:
    virtual float area()const =0;
};
class Circle:public Shape
{
    public:
    Circle(float ra):r(ra){}
     virtual float area()const;
    protected:
    float r;
};
float Circle::area()const
{return pi*r*r;}
class Rectangle:public Shape
{
    public:
    Rectangle(float a,float b):l(a),m(b){}
    virtual float area()const;
    float l,m;
};
float Rectangle::area()const
{return l*m;}
class Triangle:public Shape
{
    public:
    Triangle(float l1,float m1):a1(l1),b1(m1){}
    virtual float area()const
    {
        return a1*b1;
    }
    private:
    float a1,b1;
};
int main()
{
    Circle c1(12.6),c2(4.9);//  Circle   c1,c2,      
    Rectangle r1(4.5,8.4),r2(5.0,2.5);//  Rectangle   r1,r2,      、 
    Triangle t1(4.5,8.4),t2(3.4,2.8); //  Triangle   t1,t2,           
    Shape *pt[6]= {&c1,&c2,&r1,&r2,&t1,&t2}; //        pt,                
    double areas=0.0; //areas    
    for(int i=0; i<6; i++)  //            ,      ,    ,       !!!!
    {
        areas=areas + pt[i]->area();
    }
    cout<<"totol of all areas="<<areas<<endl;   //     
    return 0;
}