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.
#include <iostream>
#include <math.h>
usingnamespace 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;
};