C++ create constructor that takes 2 arg

Hi, I need help with creating a constructor that takes in 2 arguments in <class Line>. I have tried to do it. But it shows a compilation error. I'm not sure about how to correct it. Anyone able to help me? Thanks!

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

class Polygon : public Shape 
	{ 
	private:
	Point *points;
	public:
		 Polygon(int n)
		{
		points = new Point[n];
		}
		virtual ~Polygon() 
		{
		delete [] points;
		}
	 virtual void Draw() = 0;
	};

class Line : public Polygon
	{
	private:
	Polygon p1;
	Polygon p2;
	public:
          Line() : Polygon(2) {}
	  Line() : p1(Polygon(2)), p2(Polygon(2)) {} //constructor(2 arguments)
	public:
       };

Match the output below:
Line l2(Point(), 
Point(100,100));	//Output: Line construction 
Last edited on
A line (line segment) has two points: one at each end.
A polygon has three or more vertices (three or more points).

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

struct point
{
    explicit point( int xx = 0, int yy = 0 ) : x(xx), y(yy) {}

    private: int x, y ;

    friend std::ostream& operator<< ( std::ostream& stm, point pt )
    { return stm << '(' << pt.x << ',' << pt.y << ')' ; }
};

struct line
{
    line() = default ;
    explicit line( point a, point b ) : from(a), to(b) {}

    private: point from, to ;

    friend std::ostream& operator<< ( std::ostream& stm, const line& ln )
    { return stm << "line from " << ln.from << " to " << ln.to ; }
};

struct polygon // : public shape
{
    polygon( std::initializer_list<point> pts ) : vertices(pts)
    { if( vertices.size() < 3 ) throw std::invalid_argument( "too few vertices" ) ; }

    // return the edges (sides) of the polygon
    // vector: https://cal-linux.com/tutorials/vectors.html
    std::vector<line> edges() const
    {
        std::vector<line> sides ;

        for( std::size_t i = 0 ; i < vertices.size() ; ++i )
           sides.push_back( line( vertices[i], vertices[ (i+1) % vertices.size() ] ) ) ;

        return sides ;
    }

    private: std::vector<point> vertices ;

    friend std::ostream& operator<< ( std::ostream& stm, const polygon& poly )
    {
        stm << "polygon with vertices: " ;
        for( point pt : poly.vertices ) stm << pt << ' ' ;
        return stm ;
    }
};

int main()
{
    // construct line with two points a and b:
    // a. point() : a default constructed point - point(0,0)
    // and b. a second point(100,100)
    const line some_line( point(), point(100,100) ) ;
    std::cout << some_line << '\n' ; // line from (0,0) to (100,100)

    const polygon a_rectangle( { point(), point(100,100), point(200,50), point(100,25) } ) ;

    std::cout << "-----\n" << a_rectangle << '\n' ; // polygon with vertices: (0,0) (100,100) (200,50) (100,25)

    const auto sides = a_rectangle.edges() ;
    std::cout << "the " << sides.size() << " sides are:\n" ;
    for( const line& ln : sides ) std::cout << "    " << ln << '\n' ;
}

http://coliru.stacked-crooked.com/a/4806071e16df0840
Hi JLBorges, thanks for the reply. Anyway, sorry that I did not mention that this is a school assignment so I'm not able to change the format of it. And I can't use #include<vector> or anything other than #include<iostream>. And i need to show the use of inheritance from class point to class polygon then to class line.
Last edited on
I don't know what your assignment specifies;
but inheriting line from polygon (or for that matter inheriting line from point) does not make any sense to me.
Topic archived. No new replies allowed.