定义一个shape 在此基础上派生出Rectangle 和Circle,二者都有GetArea和GetPrim

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class shape
{
    public:
        shape(){};
        virtual ~shape(){}
        virtual double GetArea()=0;
        virtual double GetPrim()=0;
};
class Rectangle:public shape
{
    private:
        int a,b;
    public:
        Rectangle(int x,int y):a(x),b(y){}
        ~Rectangle(){}
        double GetArea()
        {
            return a*b;
        }
        double GetPrim()
        {
            return 2*(a+b);
        }
};
class Circle:public shape
{
    private:
    double radious;
    public:
        Circle(double r):radious(r){}
        ~Circle(){}
        double GetArea()
        {
            return radious*radious*3.14;
        }
        double GetPrim()
        {
            return 2*3.14*radious;
        }
};
int main()
{
    Rectangle r1(3,4);
    Circle c1(1);
    cout<<r1.GetArea()<<endl;
    cout<<r1.GetPrim()<<endl;
    cout<<c1.GetArea()<<endl;
    cout<<c1.GetPrim()<<endl;
}
 

    原文作者:liqiluan
    原文地址: https://blog.csdn.net/qq_39352109/article/details/88750651
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞