- Basics of C++
- About
- Structure of a program
- Variables and types
- Constants
- Operators
- Basic Input/Output
- Program structure
- Flow Control
- Functions
- Name visibility
- Compound data types
- Arrays
- Character sequences
- Pointers
- Dynamic memory
- Data structures
- Core C++ Concepts
- Constructor
- Friendship and inheritance
- Polymorphism
- Other language features
- Type conversions
- Exceptions
- Preprocessor directives
Constructors
Constructors are the special class functions which performs initialization of every object. Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to object.
{
int x;
public:
A(); //Constructor
};
While defining a contructor we must remeber that the name of constructor will be same as the name of the class, and contructors never have a return type.
Constructors can be defined either inside class definition or outside class definition using class name and scope resolution(::) operator.
{
int i;
public:
A(); //Constructor declared
};
A::A() // Constructor definition
{
i=1;
}
Types of Constructors
Constructors are of three types :
- Default Constructor
- Parametrized Constructor
- Copy COnstructor
Default Constructor
It is a constructor which doesn't take any argument.
Syntax :
{ Constructor Definition }
Demo
{
int side;
public:
Cube()
{
side=10;
}
};
int main()
{
Cube c;
cout << c.side;
}
Output
In this case, as the object is created constructor is called which initializes its data members.
Default constructor is important for initialization of object members, that even if we don't define a constructor explicitly, the compiler will provide a default constructor implicitly/automatically.
Demo
{
int side;
};
int main()
{
Cube c;
cout << c.side;
}
Output
In that case, default constructor provided by compiler will be called which will initialize the object data members to default value, in this case it will be 0.
Parametrized Constructor
Parametrized Constructor are the constructors with parameter. Using this Constructor we can provide different values to data members of different objects, by passing appropriate values as argument.
Demo
{
int side;
public:
Cube(int x)
{
side=x;
}
};
int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);
cout << c1.side;
cout << c2.side;
cout << c3.side;
}
Output
By using this in above case, we have initialized 3 objects with values that are user defined. We can have any number of parameters in a parameterized constructor.
Copy Constructor
It is a type of constructor which is used to create a copy of an already existing object of a class type. It is usually in the form X (X&), where X is the class name.he compiler provides a default Copy Constructor to all the classes.
Syntax of Copy Constructor
{
. . . .
}
As it is used to create an object, that is why it is called a constructor. It creates a new object, which is exact copy of the existing copy, that is why it is called copy constructor.

Fig 1.
Constructor Overloading
Just like other member functions, constructor is also a type of function so, constructors can also be overloaded. Infact when we have both default and parameterized constructors defined in our class we are having Overloaded Constructors, one with no parameter and other one with parameter.
We can have any number of Constructors in a class that differ in parameter list.
{
int rollno;
string name;
public:
Student(int x)
{
rollno=x;
name="None";
}
Student(int x, string str)
{
rollno=x ;
name=str ;
}
};
int main()
{
Student A(10);
Student B(11,"Ram");
}
Above case we have defined 2 constructors with different parameters, hence overloading the constructors.
One more important thing, if we define any constructor explicitly, then the compiler will not provide default constructor and we will have to define it our-self.
In above case if we write Student S; in main(), it will lead to a compile time error, because we haven't defined default constructor, and compiler will not provide its default constructor because we have defined other parametrized constructors.
Destructors
It is a special class function which destroys the object as soon as the scope of object ends. Destructor is called automatically by compiler when object goes out of the scope.
Syntax for destructor is same as constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it.
{
public:
~A();
};
Destructors never have any arguments.
Demo
{
A()
{
cout << "Constructor called";
}
~A()
{
cout << "Destructor called";
}
};
int main()
{
A obj1; // Constructor Called
int x=1
if(x)
{
A obj2; // Constructor Called
} // Destructor Called for obj2
} // Destructor called for obj1
In the example we will use default argument to have a single definition for both default and parametrized constructor.
Demo
{
int a;
public:
Dual(int x=0)
{
a=x;
}
};
int main()
{
Dual obj1;
Dual obj2(10);
}
Here, in this program, a single Constructor definition will take care for both these object initializations. We don't need separate default and parametrized constructors.