我已经完成了 C++ 程序,其中我的基类 ieShape
负责获取数据,以及其他两个派生类Triangle
并Rectangle
计算面积。问题是我将该区域作为一些垃圾值。我已经完成了代码,请看一下并指导我。谢谢
#include<iostream>
using namespace std;
class Shape{
protected: double b,h;
public:void get_data()
{
cout<<"Enter the height\n";
cin>>h;
cout<<"Enter the breadth\n";
cin>>b;
}
virtual void display_area(){}
};
class Rectangle:public Shape
{
public:void display_area(){
cout<<"\n\nArea of Rectangle:" << b*h;
}
};
class Triangle:public Shape
{
public:void display_area(){
cout<<"\n\nArea of Triangle:"<<0.5*b*h;
}
};
int main()
{
Shape s;
Triangle t;
Rectangle r;
Shape *ptr;
ptr=&s;
ptr->get_data();
ptr=&t;
ptr->display_area();
ptr=&r;
ptr->display_area();
return 0;
}