Point, square and cube by inheritance

So im trying to code this inheritance thing with Point, square and cube (for a homework). Thing is im having a problem: I want the class square to get the area from the 2 points (0,0) and (5,0) for example. But im currently stuck. I dont know what to put in int area and int volume. Im a beginner so it might be something very easy.

Thank you

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

using namespace std;

class Point {
public:
    int x1,y1;
    void set_values (int x, int y)
    {x1=x; y1=y;}
};

class Square: public Point {
public:
    int area()
    {return  ;}
};

class Cube: public Square {
public:
    int volume()
    {return ;}
};

int main()
{
 int cote;
 cout << "Point, square and cube" << endl;

 Point p1,p2;
 Square square1;
 Cube cube1;
 p1.set_values(0,0);
 p2.set_values(5,0);
 cout <<square1.area() << endl;
 cout <<cube1.volume() << endl;

}
Last edited on
Hi,
One point is not enough. You need at least two points before you can determine the length of a square or a cube.
Last edited on
Yes, thats why I put: p1.set_values(0,0); p2.set_values(5,0);
From there I could get the distance between the 2 points and get the area and the volume, but how to use the class point for square?
Hi, try this :
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
#include <iostream>
#include <math.h>

using namespace std;

class Point {
public:
    int x1,y1;
    void set_values (int x, int y)
    {x1=x; y1=y;}
};

class Square {
public:
    Point p1, p2;
    int length(){return  sqrt((p2.x1 - p1.x1) * (p2.x1 - p1.x1) + (p2.y1 - p1.y1) * (p2.x1 - p1.y1));}
    int area()
    {return  length() * length();}
};

class Cube: public Square {
public:
    int volume()
    {return  area() * length();}
};

int main()
{
 int cote;
 cout << "Point, square and cube" << endl;

 Point p1,p2;
 Square square1;
 Cube cube1;
 square1.p1.set_values(0,0);
 square1.p2.set_values(5,0);
 cube1.p1.set_values(0,0);
 cube1.p2.set_values(5,0);

 cout <<square1.area() << endl;
 cout <<cube1.volume() << endl;

}
Last edited on
Error at line 16: Class Point has no member named 'x'
But thanks for the help, im getting closer (had a problem with lenght, tried to use pow function and didnt work properly.

NEVERMIND, I MANAGED TO GET IT (there was a small error on line 16 ((p2.x1 - p1.y1));}

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

#include <iostream>
#include <math.h>

using namespace std;

class Point {
public:
    int x1,y1;
    void set_values (int x, int y)
    {x1=x; y1=y;}
};

class Square {
public:
    Point p1, p2;
    int length(){return  sqrt((p2.x1 - p1.x1) * (p2.x1 - p1.x1) + (p2.y1 - p1.y1) * (p2.y1 - p1.y1));}
    int area()
    {return  length() * length();}
};

class Cube: public Square {
public:
    int volume()
    {return  area() * length();}
};

int main()
{
 int cote;
 cout << "Point, square and cube" << endl;

 Point p1,p2;
 Square square1;
 Cube cube1;
 square1.p1.set_values(0,0);
 square1.p2.set_values(5,0);
 cube1.p1.set_values(0,0);
 cube1.p2.set_values(5,0);

 cout <<square1.area() << endl;
 cout <<cube1.volume() << endl;

}
Last edited on
Yea I solved it thank you very much! Now ill work on improving the program. For example, I would like the user to choose the coordinates of the 2 points.

Topic archived. No new replies allowed.