Anastasia Richardson
Anastasia Richardson

Reputation: 41

Default constructor with normal constructors for classes c++

I have been trying to understand the default constructor and i think i get it if it's the only constructor in the class. But what if i have more than one constructor defined in the class. What i am trying to do is to create a class "vector", which would store two dimensional vectors. I need one constructor to set the coordinates to the values given in the main function. I also need a default constructor, which when called, would set the coordinates to 0. I can't seem to figure out how to make both work in the same code

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

class Vector {
    double x_coord, y_coord;
public:
Vector(double x_coord=0, double y_coord=0); //default contructor???

Vector (double x, double y) //normal constructor
{
    set_values (x,y);
}
void set_values(double new_x, double new_y) //function to set values for the vectors
    {
        x_coord=new_x;
        y_coord=new_y;
    }
   double get_x()
    {
        return x_coord;
    }
   double get_y()
    {
        return y_coord;
    }

};

Upvotes: 1

Views: 358

Answers (3)

Anastasia Richardson
Anastasia Richardson

Reputation: 41

Nevermind, i figured it all out. If anyone needs the answer: You can have the default and other constructors defined in Class

class Vector {
double x_coord, y_coord;
public:
Vector():  x_coord(0), y_coord(0) {};   //default constructor

Vector (double x, double y) //normal constructor
{
    set_values (x,y);
}

it's just the way you define your default constructor.

Upvotes: 1

cadaniluk
cadaniluk

Reputation: 15229

The default constructor is the constructor invoked when you omit the parantheses when defining an instance of the class. Example:

Vector vec;

Here, the default constructor (Vector::Vector(double = 0, double = 0)) is executed.

You can remove the other constructor (Vector::Vector(double, double)) and use this definition for the default constructor:

Vector(double x_coord = 0, double y_coord = 0) {
    set_values(x_coord, y_coord);
}

When you pass two arguments, this will be called automatically. Furthermore, an ambiguity is resolved: what if, with those two constructors, you passed two doubles? Which one of them should be called? The compiler would raise an error saying that the constructors are ambiguous.


Notes:

  • The set_values function does not seem helpful as it does not do any useful work. Use a member initializer list in the constructor instead to improve performance. Also, it is considered good style:

    Vector(double x_coord = 0, double y_coord = 0): x_coord(x_coord), y_coord(y_coord) { }
    
  • Your extensive use of setters and getters looks... bad. It breaks encapsulation. Provide functions, which do not expose implementation details but perform useful operations such as move.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206717

I can imagine constructing objects of the class using the following:

Vector v1;         // Construct with x = 0, y = 0
Vector v2(10);     // Construct with x = 10, y = 0
Vector v3(10, 20); // Construct with x = 10, y = 20

You can accomplish all of that with just one constructor:

Vector(double x=0, double y=0) : x_coord(x), y_coord(y) {}

You don't need the second constructor.

Upvotes: 2

Related Questions