利用继承,求三角形及矩形面积

class Area
{ double height;
double width;
public:
Area(double h,double w)
{
height=h;
width=w;
}
virtual double getArea()=0;
};

#include <iostream>
using namespace std;
class Area{
protected:
    double height;
    double width;
public:
    Area(double h,double w)
    {
        height=h;
        width=w;
    }
    virtual double getArea()=0;
};
class Rectangle:public Area{
public:
    Rectangle(double h, double w) : Area(h, w) {}

    double getArea(){
        return height*width;
    }
};
class Isosceles:public Area{
public:
    Isosceles(double h, double w) : Area(h, w) {}
    double getArea(){
        return height*width*(0.5);
    }
};
int main(){
    double a,b,c,d;
    cin>>a>>b>>c>>d;
    Rectangle rectangle(a,b);
    Isosceles isosceles(c,d);
    cout<<"the Area of Rectangle is "<<rectangle.getArea()<<endl;
    cout<<"the Area of Isosceles is "<<isosceles.getArea()<<endl;

}
发布了136 篇原创文章 · 获赞 18 · 访问量 4177

猜你喜欢

转载自blog.csdn.net/xcdq_aaa/article/details/105120176