C++ class inheritance shapes

How do I use my class shape as shown below? Class shape is one that consist of the draw method. But how do I show the inheritance from Class shape to class polygon then to class Line? Thanks in advance!

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
class Shape 
{ 
public:
  virtual void  Draw();
};

class Polygon : public Shape 
{
private:
   int x;
   int y;

public:
   Polygon() : x(0), y(0) {}
   Polygon(int x, int y) : x(x), y(y) {}

public:
   void Draw();
};

class Line : public Polygon
{
private:
   Polygon p1;
   Polygon p2;

public:
   Line() : p1(Polygon(0, 0)), p2(Polygon(0, 0)) {}
   Line(const Polygon & p1, const Polygon & p2) : p1(p1), p2(p2) {}

public:
   void Draw();
  
};
void Line::Draw()
{
   std::cout << "Line drawing\n";
}  
Last edited on
First, you seem to have confused what a Polygon is with what a Point is. Your "Polygon" class contains an x value and a y value. Your "Polygon" is quite clearly not a polygon - it's a point.


But how do I show the inheritance from Class shape to class polygon then to class Line?

I don't understand the question. Show? Show to whom? Your code already shows the inhertiance. We can look at the code and clearly see that Line inherits from Polygon, and clearly see that Polygon inherits from Shape.
It's confusing when you start a new thread for every minor variation of the same question.

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
#include <vector>

class Point
{
private:
   int x;
   int y;
public:
   Point() : x(0), y(0) {}
   Point(int x, int y) : x(x), y(y) {}
};

// Shape is pure abstract
// https://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes/Pure_Abstract_Classes
class Shape
{
public:
    virtual void  Draw() = 0;
};

// This is just abstract
// Some things are virtual, and other things are concrete.
class Polygon : public Shape {
private:
    std::vector<Point>   points;
public:
    Polygon(int n) {
        points.resize(n);
    }
    virtual void Draw() = 0;
};

// This is a real thing
class Line : public Polygon {
public:
    // A line is a Polygon with 2 points
    Line() : Polygon(2) {
    }
    // And is actually drawable
    void Draw ( ) {
    }
};
If you can draw a line then why can't you draw a polygon too? I'd do the classes this way:
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
#include <vector>

class Point
{
private:
    int x;
    int y;
public:
    Point():x(0), y(0) { }
    Point(int x, int y):x(x), y(y) { }

    // Helper function. Draw a line between this point and another
    drawLine(const Point &other);
};

// Shape is pure abstract
// https://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes/Pure_Abstract_Classes
class Shape
{
  public:
    virtual void Draw() = 0;
};

class Polygon:public Shape
{
  private:
    std::vector < Point > points;
  public:
    Polygon(int n)
    {
	points.resize(n);
    }
    void Draw();  // connect points[0] to points[1] to ... to points[n] back to points[0]
};

// This is a real thing
class Line:public Shape
{
  public:
    Point points[2];
    void Draw() { points[0].lineTo(points[1]); }
};

Hi @dhayden, I can't use #include <vector> as my school doesn't allow. So how would I implement the Class Polygon:public shape function otherwise?
For simple school exercises, you could probably get away with
Point points[10];

Ask your teacher what an acceptable number of points in a polygon is.

If it really does need to vary, then
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Polygon:public Shape
{
  private:
    Point *points;
  public:
    Polygon(int n)
    {
	points = new Points[n];
    }
    virtual ~Polygon() // must be virtual when inheritance is possible
    {
	delete [] points;
    }
    void Draw();  // connect points[0] to points[1] to ... to points[n] back to points[0]
};

Topic archived. No new replies allowed.