定义一个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;
}