default constructor Line class

I have a Line class and a FRIEND Point class in it .The default constructor of the point class takes 2 int arguements that is x-location and y-location .

eg. Point p(10,3);

I am trying to make the default constructor for the line class which satisfies the point slope form of the Line that is the constructor for the Line class takes 2 arguements one of the type Point and the other of the form double(for slope).


But I am finding that all of the below code is working .....is it all correct ?

1
2
3
4
5
6

// default constructor 
Line(Point = 0,double  = 0); 
Line(Point = (0,0),double = 0);
Line(Point = (0,0,0), double = 0);  //?


Thanks !
Does Point have constructor takinf one argument? Because this is what you call in all cases.
yeah copy constructor ....
Last edited on
No, of type int. Maybe several arguments with some default values?

Point = (0,0,0) is equivalent to Point = 0. Only way for this to work is to have Point(int) constructor.
You should pass the Point by const reference to avoid having to copy it. That complicates the constructor but there's a way to deal with it:

1
2
3
4
5
6
class Line {
public:
    Line (const Point &p = dummy, double slope =0);
private:
    static Point dummy;
};


The default argument for p is a private static member of the class called dummy. You have to define this is the file that implements the class.
1
2
3
4
5
6
7
8
9
10
11
12
class Point
{ public:
    Point(double=0.0,double=0.0);    // default constructor
    Point(const Point&);             // copy constructor
    ~Point();                        // destructor
    Point& operator=(const Point&);  // assignment operator
    double x() const;
    double y() const;
    string toString() const;
  private:
    double _x, _y;
};
(0,0,0) is equals to 0
(0, 0) is also equals to 0
Read info on , operator
So both (0, 0) and (0, 0, 0) calls your "default" constructor with second parameter taking default value.

As there is no sense to convert integer (or double) I suggest to make default constructor explicit.
Topic archived. No new replies allowed.