无法从 int 函数返回结果

Can't return result from int function

本文关键字:返回 结果 函数 int      更新时间:2023-10-16

我正在用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

您正在对对象l调用l.LatimeaL(1,2,4);

对象tTriunghi t;一起创建。它不是通过调用Latimeal函数来初始化的。因此,您将得到一个未定义的值。

您需要调用t.Latimeal(1,2,4)函数来初始化它,在创建对象之后。