【问题标题】:Can't return result from int function无法从 int 函数返回结果
【发布时间】: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 的声明。
  • 您似乎认为对象plt 以某种方式连接。它们是三个完全独立的对象。

标签: c++ class inheritance


【解决方案1】:

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

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

创建对象后,您需要调用t.Latimeal(1,2,4) 函数对其进行初始化。

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2021-12-14
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多