Good morning! I have to do this for an exercise:
Since the Square and Cube geometric figures are drawn from points, write a program that defines the Point, Square, and Cube classes. Proceed using inheritance;
So I think I did well? Anyway, I hope... However, there is a part that does not seem to work, the volume
#include <iostream>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
using namespace std;
//########### Définition de la classe Point ###########
class Point {
public:
Point(int x, int y) {
this->x = x;
this->y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
private:
int x, y;
};
//########### Définition de la classe Carré ###########
class Square : public Point {
public:
Square(int x, int y, int side) : Point(x, y) {
this->side = side;
}
int getArea() {
return side * side;
}
private:
int side;
};
//########### Définition de la classe Cube ###########
class Cube : public Square {
public:
Cube(int x, int y, int side) : Square(x, y, side) {
}
int getVolume() {
return side * side * side;
}
private:
int side;
};
int main() {
Cube cube(1, 1, 2);
cout << cube.getX() << endl; // 1
cout << cube.getY() << endl; // 1
cout << cube.getArea() << endl; // 4
cout << cube.getVolume() << endl; // 8
return 0;
}
But I don't know why it returns 0 in the volume :'c It should be simple though! It's just an int of the side * side * side;
But when i run it it shows me 1 1 4 0
Why does this happend to the cube.getVolume()?
CodePudding user response:
Your Cube
subclass overrides the variable side
. So you now have two "side" values: Square::side
and Cube::side
. The latter is never initialized.
If you were to simply remove Cube::side
, then you have an access problem because Square::side
is declared private. If you want Cube
to access that member, it should be protected instead.
class Point {
public:
Point(int x, int y)
: x(x)
, y(y)
{ }
int getX() const { return x; }
int getY() const { return y; }
protected:
int x, y;
};
class Square : public Point {
public:
Square(int x, int y, int side)
: Point(x, y)
, side(side)
{ }
int getArea() const { return side * side; }
protected:
int side;
};
class Cube : public Square {
public:
Cube(int x, int y, int side)
: Square(x, y, side)
{ }
int getVolume() const { return side * side * side; }
};
CodePudding user response:
I would like to add that, since you have declared the variable side
in Square
class as private
, Cube
can not inherit from super class Square
. So you can just assign the value of side
in the constructor of Cube
. Or make side
of Square
accessible to inherited class.
#include <iostream>
#include <ctype.h>
#include <stdio.h>
using namespace std;
//########### Définition de la classe Point ###########
class Point {
public:
Point(int x, int y) {
this->x = x;
this->y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
private:
int x, y;
};
//########### Définition de la classe Carré ###########
class Square : public Point {
public:
Square(int x, int y, int side) : Point(x, y) {
this->side = side;
}
int getArea() {
return side * side;
}
private:
int side;
};
//########### Définition de la classe Cube ###########
class Cube : public Square {
public:
Cube(int x, int y, int side) : Square(x, y, side) {
this->side = side;
}
int getVolume() {
return side * side * side;
}
private:
int side;
};
int main() {
Cube cube(1, 1, 2);
cout << cube.getX() << endl; // 1
cout << cube.getY() << endl; // 1
cout << cube.getArea() << endl; // 4
cout << cube.getVolume() << endl; // 8
return 0;
}