i thought constructor are only a function

"The compiler implicitly creates a default constructor in a class that does not define a constructor. Such a constructor does not initialize the class’s data members,but does call the default constructor for each data member that’s an object of another class. An uninitialized variable typically contains a “garbage” value."
so what is this thing talking about
Last edited on
It's talking about constructors. What don't you understand?
Such a constructor does not initialize the class’s data members,but does call the default constructor for each data member that’s an object of another class.
i understood that constructors are functions with same name as the class, but right here it talks about data member having default constructor and i understood data members to be Variables so thats why i am lost
A constructor is not a normal function.
Lets say I have two classes, classOne and classTwo.

Both of them have constructors. Whenever an instance of classOne is created, its constructor is called. Whenever an instance of classTwo is created, its constructor is called.

Here's classOne:

1
2
3
4
5
class classOne
{
  classTwo x;
  classTwo y;
};


When I create an instance of classOne, the constructor of classOne is called. This constructor then has to create the member variable x and the member variable y. What does it do? When creating the variable x, it calls the classTwo constructor. When creating the variable y, it calls the classTwo constructor.
Last edited on
@peter yeah i understand that it cant return anything, but its still a function right ? so why does this refer to variables as having constructors.
Last edited on
Do you understand that variables can have their own functions? These are commonly called "member functions".

A constructor is one of these functions.
Last edited on
so why does this refer to variables as having constructors.

A variable can be an instance of a class.
@moschops yeah i got that , i get it now great example thanks
Topic archived. No new replies allowed.