You are on page 1of 67

Constructor

Constructor & Destructor


● In C++, user defined data types like class behaves in the same way
built in data types do.
● It means, we can initialize a class variable when it is declared in
the same way as initialization of standard variables.
● Object Oriented Programming permits objects to initialize
themselves as and when they are created.
● This is done through special type of function called constructor.
● Destructors are used to destroy the objects by releasing the
resources allocated to them when they are no longer required.
Slide Number 2
Constructor
● The constructor is a special member function whose sole role is to
initialize the objects of the class it belongs.
● The name of constructor is same as the class name it belongs to.
● It is executed automatically when an object of the class is
declared.
Constructor …
● Member function
● Special member function having name as the name of the parent
class.
● Has no return type even void is not allowed.
● Used to create object of parent class.
● Initializes the member data after creating the corresponding
object.
● Placed as public member

Slide Number 4
Characteristics of Constructor
● The name of constructor and that of the class it belongs to are
same.
● The declaration of constructor has to be undertaken in public
section.
● Constructors are used to initialize the values to the data variables
of the class.
● They do not have any return type, not even void.
● They are invoked automatically, when an object of the class is
created.
Slide Number 5
Characteristics of Constructor …
● They cannot be inherited.
● If a constructor is not defined in the class, the compiler
automatically generates the same.
● The address of the constructor cannot be taken.

Slide Number 6
Declaration and Definition of Constructor
● A constructor is a special member function with the same name as
the class it belongs to.
● The only task it performs is to initialize the objects of the class of it
belongs to.
● A constructor is invoked whenever an object of the corresponding
class is created.
● It got its name due to the action it performs.
● It constructs the value of data member of the class.

Slide Number 7
Declaration and Definition of Constructor …
● If a constructor is not defined in the class, the compiler
automatically generates the same.
● WAPC++ to declare and define constructor.
class student
{
private:
int roll;
public:

Slide Number 8
Declaration and Definition of Constructor …
student()
{
roll=0;
}
void show()
{
cout<<“Roll Number is:-”<<roll;
}

Slide Number 9
Declaration and Definition of Constructor …
}s;
void main()
{
clrscr();
s.show();
getch();
}

Slide Number 10
Declaration and Definition of Constructor …
● WAPC++ to declare and define the constructor outside the body of
the class.
class student
{
private:
int roll;
public:
student();

Slide Number 11
Declaration and Definition of Constructor …
void show();
}s;
student::student()
{
roll=0;
}

Slide Number 12
Declaration and Definition of Constructor …
void student::show()
{
cout<<“Roll Number is:-”<<roll;
}

Slide Number 13
Declaration and Definition of Constructor …
void main()
{
clrscr();
s.show();
getch();
}

Slide Number 14
Declaration and Definition of Constructor …
void main()
{
clrscr();
s.show();
getch();
}

Slide Number 15
Constructor
class student
{
private:
int roll;
public:

Slide Number 16
Constructor
student()
{
roll=0;
}
void output()
{
cout<<endl<<roll;
}

Slide Number 17
Constructor
};
void main()
{
student s; /* at the time of compilation, the compiler places
the implicit call of the corresponding constructor*/
s.output(); →outputs 0.
getch();
}

Slide Number 18
Constructor
int a;
After the successful compilation of the above declaration
statement, 2 bytes of memory space is allocated to variable a
(how?→ by binding the base address of 2 bytes of memory space
with symbol a)
The above binding is done at compile time, so it is termed as static
or compile time binding.

Slide Number 19
Types of Constructor
● Default
● Parameterized
● Copy

Slide Number 20
Type of Constructor: Parameterized
class student
{
private:
int roll;
public:

Slide Number 21
Type of Constructor: Parameterized …
student( int i)
{
roll=i;
}
void output()
{
cout<<endl<<roll;
}

Slide Number 22
Type of Constructor: Parameterized …
};
void main()
{
student s(4);
s.output(); →outputs 4.
getch();
}

Slide Number 23
Type of Constructor: Copy
class student
{
private:
int roll;
public:

Slide Number 24
Type of Constructor: Copy …
student( student a)
{
roll=a.roll;
}
void output()
{
cout<<endl<<roll;
}

Slide Number 25
Type of Constructor: Copy …
};
void main()
{
student s;
student s1(s);
s1.output(); →outputs 0.
getch();
}

Slide Number 26
Features of Object Oriented Programming:
Polymorphism
● Polymorphism → Poly means “Multiple” and Morph means
“Forms”
● Polymorphism refers to the ability of an instance to take more
than one form and behave differently in different situations.
● It allows identically named functions to exhibit different behavior
depending upon the types of objects used in the operation.

Slide Number 27
Compile Time Polymorphism (Overloading)
class student
{
private:
int roll;
public:

Slide Number 28
Compile Time Polymorphism …
student()
{
roll=0;
}
student(int i)
{
roll=i;
}

Slide Number 29
Compile Time Polymorphism …
student( student a)
{
roll=a.roll;
}
void output()
{
cout<<endl<<roll;
}

Slide Number 30
Compile Time Polymorphism …
};
void main()
{
student s;/*compiler binds the call of default constructor*/
student s1(1); /*compiler binds the call of parameterized constructor*/
student s2(s); /*compiler binds the call of copy constructor*/
s.output(); →outputs 0.

Slide Number 31
Compile Time Polymorphism …
s1.output(); →outputs 1.
s2.output(); →outputs 0.
getch();
}

Slide Number 32
Types of Constructor
● There are three types of constructors.
● They are
● Default Constructor
● Parameterized Constructor
● Copy Constructor

Slide Number 33
Types of Constructor
● There are three types of constructors.
● They are
● Default Constructor
● Parameterized Constructor
● Copy Constructor

Slide Number 34
Default Constructor
●A constructor without any argument or parameter list is called
default constructor.
● WAPC++ to implement default constructor.
class student
{
private:
int roll;
public:

Slide Number 35
Default Constructor
student()
{
roll=0;
}
void show()
{
cout<<“Roll Number is:-”<<roll;
}

Slide Number 36
Default Constructor
}s;
void main()
{
clrscr();
s.show();
getch();
}

Slide Number 37
Parameterized Constructor
●A constructor with argument(s) or parameter(s) list is called
parameterized constructor.
● This can be achieved by passing arguments to the constructor
when the object is created.
● WAPC++ to implement parameterized constructor.
class student
{
private:
int roll;
Slide Number 38
Parameterized Constructor …
public:
student(int i)
{
roll=i;
}
void show()
{
cout<<“Roll Number is:-”<<roll;
}
Slide Number 39
Parameterized Constructor …
}s(5);
void main()
{
clrscr();
s.show();
getch();
}

Slide Number 40
Constructor Overloading
● A constructor can have more than one definition.
● This is called compile time polymorphism.
● WAPC++ to overload constroctor.
class student
{
private:
int roll;
public:

Slide Number 41
Constructor Overloading
student()
{
roll=a;
}
student(int i)
{
roll=i;
}

Slide Number 42
Constructor Overloading
void show()
{
cout<<“Roll Number is:-”<<roll;
}
}s,s1(5);

Slide Number 43
Constructor Overloading
void main()
{
clrscr();
s.show();
s1.show();
getch();
}

Slide Number 44
Copy Constructor
●A copy of constructor is having a reference to an instance of its
own class as an argument.
● It is used to declare and initialize one object from another.
● student s1;
● student s2=s1;
● In the immediate above statement, one object is initialized with
another object of same class during definition.
● The members of s1 are copied into s2.
● The statement student s2(s1) is equivalent to student s2=s1;

Slide Number 45
Copy Constructor …
● The syntax to declare and define copy constructor is
className( className &objectName)
● WAPC++ to declare and define copy constructor
class student
{
private:
int roll;
char Name[20];

Slide Number 46
Copy Constructor …
public:
student(int a, char n[] )
{
roll=a;
strcpy(name,n);
}

Slide Number 47
Copy Constructor …
student(student &x)
{
roll=x.roll;
strcpy(name, x.name);
}

Slide Number 48
Copy Constructor …
void show()
{
cout<<“Roll Number is:-”<<roll;
cout<<endl<<”Name is:-”;
puts(name);
}
};

Slide Number 49
Copy Constructor …
void main()
{
clrscr();
student s1(1,”Amit”);
student s2(s1);
student s3=s2;
student s4;
s4=s3;

Slide Number 50
Copy Constructor …
s1.show();
s2.show();
s3.show();
s4.show();
getch();
}

Slide Number 51
Copy Constructor …
●A reference variable has been used as an argument to the copy
constructor.
● We cannot pass the argument by value to a copy constructor.
● When no copy constructor is defined, the compiler supplies its
own copy constructor.

Slide Number 52
Dynamic Constructor
● The constructor can also be used to allocate memory while
creating objects.
● This helps the system to allocate the right amount of memory for
each object when the objects are not of the same size.
● Allocation of memory to objects at the time of their construction
is called “Dynamic construction” of objects.
● The “new” operator is used to allocate memory.

Slide Number 53
Dynamic Constructor …
class student
{
private:
int length;
char *name;
public:

Slide Number 54
Dynamic Constructor …
student()
{
length=0;
name=new char[length+1];
}

Slide Number 55
Dynamic Constructor …
student(char *s)
{
length=strlen(s);
name=new char[;ength+1];
strcpy(name,s);
}

Slide Number 56
Dynamic Constructor …
void show()
{
cout<<endl<<“Name is:-”<<name;
}

Slide Number 57
Dynamic Constructor …
void add(student &a, student &b)
{
length=a.length+b.length;
delete;
name=new char[length+1];
strcpy(name,a.name);
strcat(name, “ ”);
strcat(name,b.name);
}
Slide Number 58
Dynamic Constructor …
void main()
{
clrscr();
char *n=“Anil”;
student Name1(n);
student Name2(“ Kumar”);
student Name3(“Singh”);
student s1,s2;

Slide Number 59
Dynamic Constructor …
s1.add(Name1,Name2);
s2.add(s1,Name3);
Name1.show();
Name2.show();
Name3.show();
s1.show();
s2.show();
getch();
}
Slide Number 60
Constructor with Default Argument
● Like other functions, constructors can be defined with default
arguments.
● If any argument(s) is/are passed during the creation of an object,
the compiler selects the suitable constructor with default
argument(s).
● When a constructor is called with no argument(s), it becomes a
default constructor.

Slide Number 61
Constructor with Default Argument …
class student
{
private:
int roll;
public:
student(int a=5)
{
roll=a;
}
Slide Number 62
Constructor with Default Argument …
void show()
{
cout<<“Roll Number is:-”<<roll;
}
}s,s1(1);

Slide Number 63
Constructor with Default Argument …
void main()
{
clrscr();
s.show();
s1.show();
getch();
return 0;
}

Slide Number 64
Constant Object
● We can create and use constant objects.
● The keyword “const” is placed before the object declaration.
● The example is
class student
{
private:
int roll;
public:

Slide Number 65
Constant Object …
student(int a=5)
{
roll=a;
}
void show()
{
cout<<“Roll Number is:-”<<roll<<endl;
}

Slide Number 66
Constant Object …
};
void main()
{
clrscr();
const student s(5);
s.show();
getch();
return 0;
}
Slide Number 67

You might also like