Semester : SEMESTER 6
Subject : Object Oriented Programming
Year : 2018
Term : MARCH
Scheme : 2015 Full Time
Course Code : EC 312
Page:7
3a. A constructor is a member function of a class which initializes objects of a class.
In C++,Constructor is automatically called when object(instance of class) create. It is
obj.a=100; //Statement 2, Error occur
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< ما" A : "<< obj.a;
cout<< "\n B : "<< obj.b;
cout<< ما" C : "<< obj.c;
cout<< "ர 0 : "<< obj.d;
Output :
A from ClassB 0
A from ClassC : 100
B:20
6:30
ட : 40
special member function of the class.
How constructors are different from a normal member function?
A constructor is different from normal functions in following ways:
= Constructor has same name as the class itself
= Constructors don’t have return type
ഭ Aconstructor is automatically called when an object is created.
ഭ If we do not specify 8 constructor, C++ compiler generates a default constructor for
us (expects no parameters and has an empty body).
Types of Constructors
1. Default Constructors: Default constructor is the constructor which doesn’t take any argument.
It has no parameters.
// Cpp program to illustrate the
// concept of Constructors
#include
namespace std;