C++形状区域:获取区域0
问题描述:
下面的代码中,三角形和矩形区域的面积为0。需要一些提示,我该如何解决这个问题。 也,如果我能得到一些固定在这,这将是伟大的!C++形状区域:获取区域0
#include <iostream.h>
#include <conio.h>
class shape
{
protected:
double x,y;
public:
void get_data(void);
virtual void display_area(void){ }
};
class triangle:public shape
{
protected:
double AoT;
public:
void display_area(void);
};
class rectangle:public shape
{
protected:
double AoR;
public:
void display_area(void);
};
void shape::get_data(void)
{
cout<<"Enter the vlaue of Base(x) and Height(y):"<<endl;
cin>>x>>y;
}
void triangle::display_area(void)
{
AoT=0.5*x*y;
cout<<"The area of Triangle in unit sq. is:"<<AoT<<endl;
}
void rectangle::display_area(void)
{
AoR=x*y;
cout<<"The area of Rectangle in Unit sq. is:"<<AoR<<endl;
}
main()
{
clrscr();
shape s, *p;
triangle t;
rectangle r;
s.get_data();
p=&t;
p->display_area();
p=&r;
p->display_area();
getch();
}
在先进的感谢。需要对此快速注视,因为我',米kinda despo大声笑
答
s
,t
,和r
是完全无关的对象。调用s.get_data()
只修改s.x
和s.y
,既不t.x
和t.y
也不r.x
和r.y
。你需要调用shape::get_data
为t
和r
seperately:
p=&t;
p->get_data();
p->display_area();
p->get_data();
p=&r;
p->display_area();
你是什么输入? – Downvoter
x和y是我的输入@cad – Anuj
't'和'p'不受's'输入的影响。 – MikeCAT