c++形状区域:获取区域0
C++ Area of Shape: Getting Area 0
下面代码中三角形和矩形的面积为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();
}
提前感谢。需要快速解决这个问题,因为我有点失望
s
、t
和r
是完全不相关的对象。调用s.get_data()
只修改s.x
和s.y
,不能修改t.x
和t.y
,也不能修改r.x
和r.y
。您需要分别为t
和r
调用shape::get_data
:
p=&t;
p->get_data();
p->display_area();
p->get_data();
p=&r;
p->display_area();
相关文章:
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 提升获取文本的区域设置"Conversion failed"
- 有没有办法使用 Boost 获取区域时间格式?
- 使用二进制掩码 C++ ITK 获取感兴趣区域
- 为 pcl::P ointCloud 数据指定一个区域并获取可用点数
- 遍历区域并在 OpenCV 中获取平均像素值
- 如果不在派生类实现中执行此操作,"basic_streambuf"是否会创建自己的获取/放置区域?
- C++获取用户首选的编码或区域设置编码
- 获取Qpainter的可见区域
- 在 c++ 中的区域语言设置下获取当前位置值
- 获取 QToolbar 的当前停靠区域的简单方法
- 如何获取我的环境的当前区域设置
- 获取确切的窗口区域大小 - 创建窗口窗口大小不正确
- 如何获取我的区域设置的空格字符串?
- 如何通过虚拟寻址区域中的指针获取物理寻址区域中的地址
- 如何使用VS2015获取区域设置名称
- 获取当前在C++中设置的全局区域设置
- Windows消息传递-获取非客户端区域的设备上下文的各种方法
- c++形状区域:获取区域0
- 获取二进制文件的数据和代码区域的大小