So i haven't figured out what the point of constructors are i am just learning C++ and am seriously having trouble with it, i do not get why...well i kind of get default constructors because it initializes the variables but what about a constructor with arguments and the member function

5 Years
Discussion Span
Last Post by Stefano Mtangoo

oh so when there are multiple constructors you are overloading the constructors

Constructor is a "special" method. It can be overloaded like any other method. Note that Constructor should not return anything!

Edited by Stefano Mtangoo: n/a


Also if you create a constructor for your class don't forget to also provide a default constructor. A default constructor is only provided if there are no constructors defined.


ok so i'll always need a default constructor even when i add other constructors

how can i have a constructor that has member functions in it


actually i just realized that's not possible i feel like the blonde redhead i am :P i
was just reading the assignment paper wrong so ignore the member function part :s


>>ok so i'll always need a default constructor even when i add other constructors

You can do both at the same time using default parameters, this is very common. For example, consider this simple class that represents a 2D vector:

class Vector2D {
    double x,y; //two data members to hold the x and y coordinates. 
    //you can have a constructor that takes the two coordinates to initialize the object with,
    // by providing default parameters ("= 0.0"), the constructor can also be called with no parameters
    // in which case they will have values (0.0,0.0)
    Vector2D(double aX = 0.0, double aY = 0.0) : x(aX), y(aY) { };

The above is very useful to provide both a parametrized constructor and a default constructor at the same time, also making it explicit what the default values are.

To create the objects, you would do this:

int main() {
  Vector2D obj1; //created with default constructor.
  Vector2D obj2(4.0,2.0); //created with the parametrized constructor.
  std::cout << "obj1 is (" << obj1.x << "," << obj1.y << ")" << std::endl;
  std::cout << "obj2 is (" << obj2.x << "," << obj2.y << ")" << std::endl;
  return 0;

As mentioned above, the main use of constructors is object initialization. You may think that this is not an important job, but
consider this -> You want to create a vector of 10 vectors of 10 ints and you want to assign the value 25 to all of them (the ints).

The manual way to do this would be:

vector<vector<int> > my_container;


for (int i = 0; i < 10; ++i)
    for (int j = 0; j < 10; ++j)
        my_container[i][j] = 25;

Using constructors, you just have to write:

vector<vector<int> > my_container(10, vector<int>(10, 25));

Which one do you prefer?

However, this is not the only use of constructors. Another thing you
can do with them is impose limitations on the ways your objects can
be created. There are cases where you want to disable copying for
your objects (because, let's say, you don't want to share ownership of
a resource). A special kind of constructor, called the copy constructor,
is invoked when you create a new object from an existing one. If you
make this constructor private, you make your objects non-copyable
(well, you also have to make the assignment operator private).

A real world example are the ostream and istream classes. Try to make a copy of
cout -> ostream cout_copy(cout); or cin -> istream cin_copy(cin); You can't. And what does the compiler say? error: 'std::ios_base::ios_base(const std::ios_base&)' is private within this context... Or something like that.

Another example is the singleton design pattern -> http://en.wikipedia.org/wiki/Singleton_pattern


Thanks everyone you gave me good explanations and references, my head no longer hurts about constructors :D


Thanks everyone you gave me good explanations and references, my head no longer hurts about constructors :D

Welcome and enjoy!

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.