【发布时间】:2016-11-08 08:46:03
【问题描述】:
我正在用 C++ 制作一个类继承程序。它的功能是确定三角形何时正确并计算其面积。
这是程序:
#include <iostream>
using namespace std;
class Puncte
{
public:
int x1,y1;
int x2,y2;
int x3,y3;
Puncte(int a, int b, int c, int d, int e, int f):x1(a),y1(b),x2(c),y2(d),x3(e),y3(f){}
void AfisareP()
{
cout<<x1<<y1<<x2<<y2<<x3<<y3<<endl;
}
};
class Latura
{
protected:
int l1, l2, l3;
public:
void LatimeaL(int a, int b, int c)
{
l1 = a;
l2 = b;
l3 = c;
}
};
class Triunghi: public Latura
{
public:
int AriaTrDr()
{
return l1*l3/2;
}
};
int main()
{
Puncte p(2,2,2,2,2,2);
Latura l;
l.LatimeaL(1,2,4);
Triunghi t;
if (p.x1 == p.x3 && p.y1 == p.y2)
{
cout << "Triunghi dreptunghic!" << endl;
cout << t.AriaTrDr() << endl;
}
return (0);
}
一切正常,但没有显示正确的结果,但地址和我不知道如何修复它。
这是结果。
Triunghi dreptunghic!
513242112
【问题讨论】:
-
在
Latura类中,int成员在初始化后留下不确定值。 -
据我所知,
Latura l;和后续的l.LatimeaL(1,2,4);在main()中都毫无意义。应该被删除,t.LatimeaL(1,2,4);应该遵循t的声明。 -
您似乎认为对象
p、l和t以某种方式连接。它们是三个完全独立的对象。
标签: c++ class inheritance