2016-11-08 225 views
-3

我正在C++中製作一個類繼承的程序。它的功能是確定一個三角形是否正確並計算其面積。無法從int函數返回結果

下面是程序:

#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); 
} 

寄託都工作正常,但它不顯示正確的結果,但ADRESS,我不知道如何解決它。

這就是結果。

Triunghi dreptunghic! 
513242112 
+1

在'Latura'類中,'int'成員在初始化後留下*不確定值*。 –

+1

從我所看到的,'latura l;'和後面的'l.LatimeaL(1,2,4);'在main()中都沒有意義。應該被刪除,'t.LatimeaL(1,2,4);'應該遵循't'的聲明。 – WhozCraig

+0

看起來你認爲對象'p','l'和't'以某種方式連接。他們是三個完全獨立的對象。 – molbdnilo

回答

0

您呼叫的對象ll.LatimeaL(1,2,4);

對象t使用Triunghi t;創建。它不通過調用Latimeal函數進行初始化。因此你得到一個未定義的值。

您需要在創建對象之後調用t.Latimeal(1,2,4)函數對其進行初始化。