Inheritance problem

For my assignment, I have to use inheritance to represent Point, square and cube. My problem is that when I run the program, it doesnt give me the area nor the volume, I get weird numbers (like -4325645 or 5452562). Its probably a very stupid error but I cant find it.

Thank you very much

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <math.h>

using namespace std;

class Point {
    public:
        void setCoordX1(int x1)
        {CoordX1=x1;}

        void setCoordY1(int y1)
        {CoordY1=y1;}

        void setCoordX2(int x2)
        {CoordX2=x2;}

        void setCoordY2(int y2)
        {CoordY2=y2;}

    int CoordX1;
    int CoordY1;
    int CoordX2;
    int CoordY2;

};

class Carre: public Point {

    public:

        int lenght()
        {return  sqrt((CoordX2 - CoordX1) * (CoordX2 - CoordX1) + (CoordY2 - CoordY1) * (CoordY2 - CoordY1));}

        int aire()
        {return lenght()*lenght();}


};

class Cube: public Carre {

    public:
        int volume()
        {return aire()*lenght();}

};

int main() {

    Point p1,p2;
    Carre carre1;
    Cube cube1;

 cout << "***** Point, carre et cube en heritage *****" << endl;
 cout << endl;

int CoordX1,CoordY1,CoordX2,CoordY2,a,b,c,d;

 cout <<"Coordonner en X du premier point: "; cin>>a;
 cout <<"Coordonner en Y du premier point: "; cin>>b;
 cout <<"Coordonner en X du deuxieme point: "; cin>>c;
 cout <<"Coordonner en Y du deuxieme point: "; cin>>d;

 p1.setCoordX1(a);
 p1.setCoordY1(b);
 p2.setCoordX2(c);
 p2.setCoordY2(d);

 cout << endl;
 cout <<"********************************************" << endl;

 cout << "L'aire du carre est de " <<carre1.aire() << endl;
 cout << "Le volume du cube est de "<<cube1.volume() << endl;

};
Topic archived. No new replies allowed.