Member variables with no default constructor

I am trying to create Point and Line classes, to store a point in 2D space and a line connecting two points respectively. Currently, the Point class looks like this:

1
2
3
4
5
6
7
8
class Point {
  private:
    int x, y;
  public:
    Point(int x, int y) { this->x = x; this->y = y; }
    int Point::X(void) { return x; }
    int Point::Y(void) { return y; }
};


I've tried creating the Line class with two Point member variables:

1
2
3
4
5
6
7
8
9
10
class Line {
  private:
    Point start;
    Point end;
  public:
    Line(Point start, Point end) {
      this->start = start;
      this->end = end;
    }
};


but I get compiler errors saying that Point has no default constructor. What is the correct way of initializing member variables in a constructor?
With an initialization list. You can use this to call specific constructors BEFORE the object has been created.
1
2
3
4
5
6
7
8
9
class Line {
  private:
    Point start;
    Point end;
  public:
    Line(Point start, Point end) 
      : start(start), end(end)
    {  }
};


Note: I like to name my arguments and member objects in a certain manner so that I never get confused. For members, I like to use a _ prefix. That might make it a little easier to understand where these variables are declared when you are reading larger functions with many local variables, especially if your project also has global variables, pointers, and many class types. I'm doing this below so that you can see the difference between start and start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Point {
  private:
    int _x, _y;
  public:
    Point(int x, int y) { _x = x; _y = y; }
    int Point::X(void) { return _x; }
    int Point::Y(void) { return _y; }
};

class Line {
  private:
    Point _start;
    Point _end;
  public:
    Line(Point start, Point end) 
      : _start(start), _end(end)
    {  }

    Line(int x1, int y1, int x2, int y2)
      : _start( Point(x1,y1) ), _end( Point(x2,y2) ) 
    {  }
};


Edit: I also added another constructor here that gives an example of where you can construct it with 4 coordinates instead of 2 points. You can even build the points inside of the constructor instead of needing to build them outside.
Last edited on
Use the constructor initialization list.
1
2
3
4
5
6
7
8
9
10
11
12
class Line 
{
private:
	Point start;
	Point end;
public:
	Line(Point start, Point end) 
	:	start(start),
		end(end)
	{
	}
};


You should also not write Point:: in front of X and Y in the class definition.
Thank you both very much! I had come across this syntax for initializing a base class, but hadn't realized it could be used for member variables as well.
Just another thought, another solution is to use pointers:
1
2
3
4
5
6
7
8
9
10
class Line {
  private:
    Point* start;
    Point* end;
  public:
    Line(Point start, Point end) {
      this->start = new Point(start);
      this->end = new Point(end);
    }
};
Unless you pretend to make `Point' polymorphic or shared, there is no need to use pointers. It would only complicate the code, requiring a destructor, a copy constructor and an assignment operator
Topic archived. No new replies allowed.