Sarvajanik College Of
Engineering And Technology
Subject Name :- Object Oriented
Programming With C++
Subject Code :- 2140705
Branch :- Computer Engineering Eve. (COE)
1
Group Members And Topic
⮚ 170420107559 – Uttam Thummar
⮚ 170420107560 – Dhriti Uttamchandani
⮚ 170420107561 – Keyur Vadodariya
⮚ 170420107562 – Aditya Vaghela
⮚Topic:- Constructors & Destructors
⮚ Guided By :- Prof. Dipali Kasat
Prof. Jaydeep Barad
2
What Does Constructor Mean?
⮚ A constructor is a special method of a class or structure in object oriented
programming that initializes an object of that type.
⮚ A constructor is an instance method that usually has the same name an the
class.
⮚ A constructor can be used to set the values of the member of an object ,
either to default or to user-defined values.
Need For Constructor
⮚ It is very common for some part of an object to require initialization
before it can be used.
⮚ Suppose you are working on 100's of objects and the default value
of a particular data member is needed to be zero.
⮚ Initializing all objects manually will be very tedious job.
⮚ Instead, you can define a constructor function which initializes that
data member to zero.
⮚ Then all you have to do is declare object and constructor will
initialize object automatically.
Syntax Of Constructor
class classname
{
…
public:
classname( Arguments )
{
//Statements
}
};
Characteristics Of Constructor.
⮚ They should be declared in the public section.
⮚ They are invoked automatically when the object are created.
⮚ They do not have return type not even void and there for they cannot
return any values.
⮚ They cannot be inherited, a derived class can call the base class
constructor.
⮚ They make implicit calls to the operator new and delete when memory
allocation is required.
Two Ways For Calling a Constructor
⮚ Implicit call
Class circle
{
float radius;
Public:
circle()
{
radius = 0;
}
circle (float r)
{
radius = r;
}
};
Circle ob (7.6);
⮚ Explicit call
Circle ob;
Ob = circle(7.6);
Static Initialization Of Object
Class demo int main ( )
{ {
int pp; demo (2,3);
public ( ): return 0;
demo (int x , int y); }
{
a=x;
b=y;
}
};
Dynamic initialization of object
Class demo int main()
{ {
int a,b; int a,b;
public: cout<<“enter 2 no.”;
demo(int x,int y) cin>>a>>b;
{ demo obj (a,b);
a=x; }
b=y;
}
};
Types Of Constructors
⮚ There are basically three main types of constructors.
1. Default Constructor
▪ One which is Implicitly called, when an object is created.
2. Parameterized Constructor
▪ One which has arguments.
3. Copy Constructor
▪ One which copies one object to another.
10
Default Constructor
⮚ It is a constructor which is
implicitly called at time of
object creation.
⮚ It is automatically declared
and defined by the compiler
if not by the user.
⮚ This constructor has no
arguments and may or may
not have the body
statements.
⮚ So it is also called Non-Arg.
Constructor.
⮚Syntax :-
▪ By User
Classname(no arguments)
{
//Statements
}
▪ By Compiler
Classname(no arguments)
{
//Empty Body
}
11
Parameterized Constructor
⮚ Sometimes it becomes necessary
to initialize the various data
elements of an objects with
different values when they are
created.
⮚ This is achieved by passing
arguments to the constructor
function when the objects are
created.
⮚ The constructors that can take
arguments are called
parameterized constructors.
⮚Syntax :-
Classname(arguments)
{
//Statements
}
12
Copy Constructor
⮚ It is basically a constructor which
copies one object to another of the
same class.
⮚ The object which is to be copied is
passed as an argument o the
constructor.
⮚ The object in which the data is to
be copied is the object used to
calling constructor.
⮚Syntax :-
classname(classname &object)
{
var = object.var ;
}
13
Constructor Overloading
⮚ C++ permits the use of more than one Constructors in a class in a
single program.
⮚ Such a situation is called Constructor Overloading.
⮚Rules For Constructor Overloading :-
⮚ Constructors should have different no.’s of arguments.
⮚ Constructors should have different types of arguments.
14
Example Of Constructor Overloading
⮚ #include <iostream>
using namespace std;
class circle
{ int r;
public:
//Default Cons.
circle()
{
r = 0;
}
//Parameterized Cons.
circle(int x)
{ r = x;
}
//Copy Cons.
circle(circle &x)
{ r = x.r;
}
void display()
{ cout <<
r;
}
};
int main()
{ circle r1;
r1.display();
r1 = circle(20);
circle r2(r1);
r1.display();
r2.display();
}
⮚ Output :-
0
20
20
15
Dynamic Constructor
•The constructors can also be used to allocate memory while
creating objects.
•This will enable 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
known as dynamic construction of objects.
•The memory is created with the help of the new operator.
16
Destructors
● A destructor is used to destroy the objects that have been
created by a constructor.
● Like constructor, the destructor is a member function whose
name is the same as the class name but is preceded by a tilde .
eg: ~ integer ( ) { }
● Whenever new is used to allocate memory in the constructor,
we should use delete to free that memory.
● There can only be 1 destructor in a class.
17
Rules Of Destructors
● Destructors cannot be overloaded.
● Destructors take no arguments.
● Destructors don't return any value.
● Destructure cannot be inherited.
A destructor function is called automatically when the object goes out of
scope:
● Function ends
● Program ends
● A block containing local variables ends
● A delete operator is called
18
When is a Destructor called?
Example of New and Delete Operators
19
#include <iostream>
using namespace std;
class Test
{
private:
int n;
float *a;
public:
Test()
{
cout << "Enter n: ";
cin >> n;
a = new float[n];
cout << "Enter array
elements." << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1<<”
element:”;
cin >> *(a + i);
}
}
~Test()
{ delete[] a; }
void Display()
{
cout << "nArray
Elements:" << endl;
for (int i = 0; i < n; i++)
cout << *(a + i) <<”t”;
}
};
int main() {
Test s;
s.Display();
}
⮚ Output :-
Enter n:3
Enter array
elements:
Enter 1 element:2
Enter 2 element:4
Enter 3 element:6
Array elements:
2 4 6
Limitations Of Constructors
⮚ They should only be declared in public section.
⮚ They don’t have any return type, not even void, so they do not return
any value.
⮚ They cannot be Inherited. However a Derived Class can call a Base
Class Constructor.
⮚ Name of the constructor must be same as that of class.
⮚ Constructors may not be Static as well as Virtual.
⮚ We cannot fetch the address of the Constructor Function.
20
Thank You21

Constructors and Destructors

  • 1.
    Sarvajanik College Of EngineeringAnd Technology Subject Name :- Object Oriented Programming With C++ Subject Code :- 2140705 Branch :- Computer Engineering Eve. (COE) 1
  • 2.
    Group Members AndTopic ⮚ 170420107559 – Uttam Thummar ⮚ 170420107560 – Dhriti Uttamchandani ⮚ 170420107561 – Keyur Vadodariya ⮚ 170420107562 – Aditya Vaghela ⮚Topic:- Constructors & Destructors ⮚ Guided By :- Prof. Dipali Kasat Prof. Jaydeep Barad 2
  • 3.
    What Does ConstructorMean? ⮚ A constructor is a special method of a class or structure in object oriented programming that initializes an object of that type. ⮚ A constructor is an instance method that usually has the same name an the class. ⮚ A constructor can be used to set the values of the member of an object , either to default or to user-defined values.
  • 4.
    Need For Constructor ⮚It is very common for some part of an object to require initialization before it can be used. ⮚ Suppose you are working on 100's of objects and the default value of a particular data member is needed to be zero. ⮚ Initializing all objects manually will be very tedious job. ⮚ Instead, you can define a constructor function which initializes that data member to zero. ⮚ Then all you have to do is declare object and constructor will initialize object automatically.
  • 5.
    Syntax Of Constructor classclassname { … public: classname( Arguments ) { //Statements } };
  • 6.
    Characteristics Of Constructor. ⮚They should be declared in the public section. ⮚ They are invoked automatically when the object are created. ⮚ They do not have return type not even void and there for they cannot return any values. ⮚ They cannot be inherited, a derived class can call the base class constructor. ⮚ They make implicit calls to the operator new and delete when memory allocation is required.
  • 7.
    Two Ways ForCalling a Constructor ⮚ Implicit call Class circle { float radius; Public: circle() { radius = 0; } circle (float r) { radius = r; } }; Circle ob (7.6); ⮚ Explicit call Circle ob; Ob = circle(7.6);
  • 8.
    Static Initialization OfObject Class demo int main ( ) { { int pp; demo (2,3); public ( ): return 0; demo (int x , int y); } { a=x; b=y; } };
  • 9.
    Dynamic initialization ofobject Class demo int main() { { int a,b; int a,b; public: cout<<“enter 2 no.”; demo(int x,int y) cin>>a>>b; { demo obj (a,b); a=x; } b=y; } };
  • 10.
    Types Of Constructors ⮚There are basically three main types of constructors. 1. Default Constructor ▪ One which is Implicitly called, when an object is created. 2. Parameterized Constructor ▪ One which has arguments. 3. Copy Constructor ▪ One which copies one object to another. 10
  • 11.
    Default Constructor ⮚ Itis a constructor which is implicitly called at time of object creation. ⮚ It is automatically declared and defined by the compiler if not by the user. ⮚ This constructor has no arguments and may or may not have the body statements. ⮚ So it is also called Non-Arg. Constructor. ⮚Syntax :- ▪ By User Classname(no arguments) { //Statements } ▪ By Compiler Classname(no arguments) { //Empty Body } 11
  • 12.
    Parameterized Constructor ⮚ Sometimesit becomes necessary to initialize the various data elements of an objects with different values when they are created. ⮚ This is achieved by passing arguments to the constructor function when the objects are created. ⮚ The constructors that can take arguments are called parameterized constructors. ⮚Syntax :- Classname(arguments) { //Statements } 12
  • 13.
    Copy Constructor ⮚ Itis basically a constructor which copies one object to another of the same class. ⮚ The object which is to be copied is passed as an argument o the constructor. ⮚ The object in which the data is to be copied is the object used to calling constructor. ⮚Syntax :- classname(classname &object) { var = object.var ; } 13
  • 14.
    Constructor Overloading ⮚ C++permits the use of more than one Constructors in a class in a single program. ⮚ Such a situation is called Constructor Overloading. ⮚Rules For Constructor Overloading :- ⮚ Constructors should have different no.’s of arguments. ⮚ Constructors should have different types of arguments. 14
  • 15.
    Example Of ConstructorOverloading ⮚ #include <iostream> using namespace std; class circle { int r; public: //Default Cons. circle() { r = 0; } //Parameterized Cons. circle(int x) { r = x; } //Copy Cons. circle(circle &x) { r = x.r; } void display() { cout << r; } }; int main() { circle r1; r1.display(); r1 = circle(20); circle r2(r1); r1.display(); r2.display(); } ⮚ Output :- 0 20 20 15
  • 16.
    Dynamic Constructor •The constructorscan also be used to allocate memory while creating objects. •This will enable 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 known as dynamic construction of objects. •The memory is created with the help of the new operator. 16
  • 17.
    Destructors ● A destructoris used to destroy the objects that have been created by a constructor. ● Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde . eg: ~ integer ( ) { } ● Whenever new is used to allocate memory in the constructor, we should use delete to free that memory. ● There can only be 1 destructor in a class. 17
  • 18.
    Rules Of Destructors ●Destructors cannot be overloaded. ● Destructors take no arguments. ● Destructors don't return any value. ● Destructure cannot be inherited. A destructor function is called automatically when the object goes out of scope: ● Function ends ● Program ends ● A block containing local variables ends ● A delete operator is called 18 When is a Destructor called?
  • 19.
    Example of Newand Delete Operators 19 #include <iostream> using namespace std; class Test { private: int n; float *a; public: Test() { cout << "Enter n: "; cin >> n; a = new float[n]; cout << "Enter array elements." << endl; for (int i = 0; i < n; i++) { cout << i + 1<<” element:”; cin >> *(a + i); } } ~Test() { delete[] a; } void Display() { cout << "nArray Elements:" << endl; for (int i = 0; i < n; i++) cout << *(a + i) <<”t”; } }; int main() { Test s; s.Display(); } ⮚ Output :- Enter n:3 Enter array elements: Enter 1 element:2 Enter 2 element:4 Enter 3 element:6 Array elements: 2 4 6
  • 20.
    Limitations Of Constructors ⮚They should only be declared in public section. ⮚ They don’t have any return type, not even void, so they do not return any value. ⮚ They cannot be Inherited. However a Derived Class can call a Base Class Constructor. ⮚ Name of the constructor must be same as that of class. ⮚ Constructors may not be Static as well as Virtual. ⮚ We cannot fetch the address of the Constructor Function. 20
  • 21.