0% found this document useful (0 votes)
49 views13 pages

Lecture 15-17

The document discusses constructors and destructors in C++. It covers: 1) Constructor overloading allows constructors with the same name but different parameters. The appropriate constructor is chosen based on the arguments passed. 2) Objects can be initialized dynamically at runtime using constructors. This provides flexibility in initialization formats. 3) Destructors are special member functions that are called automatically when an object goes out of scope to destroy/clean up the object created by the constructor. They do not have arguments or return values.

Uploaded by

K S Nagill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views13 pages

Lecture 15-17

The document discusses constructors and destructors in C++. It covers: 1) Constructor overloading allows constructors with the same name but different parameters. The appropriate constructor is chosen based on the arguments passed. 2) Objects can be initialized dynamically at runtime using constructors. This provides flexibility in initialization formats. 3) Destructors are special member functions that are called automatically when an object goes out of scope to destroy/clean up the object created by the constructor. They do not have arguments or return values.

Uploaded by

K S Nagill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 15-17

Constructors and
Destructors
Contents

• Constructor Overloading
• Dynamic Initialization of objects
• Dynamic Constructors
• Destructor
Constructor Overloading

▪ Constructor can be overloaded in similar way as


function overloading.
.
▪ Overloaded constructors have the same name
(name of the class) but different number (or types
or both) of arguments.

▪ Depending upon the number and type of arguments


passed, specific constructor is called.
Example of Constructor Overloading
#include<iostream> example::example(int x, int y)
using namespace std; {
a=x;
class example b=y;
{ }
private:
example::example(example &p)
int a,b; {
public: a=p.a;
example(); //Default Constructor b=p.b;
}
example(int, int); //Parameterized Constructor
example(example &); //Copy Constructor void example::display()
{
void display();
cout<<a<<endl<<b;
};
}
int main()
example::example() {
{ example e1;
cout<<“Constructor is called”; example e2(4, 5);
} example e3(e2);
e3.display();
return 0;
}
Dynamic Initialization of Objects
▪ Class objects can be initialized dynamically also i.e. the initial
value of an object can be provided during run time.

▪ Advantage:
➢ Various initialization formats can be provided using
overloaded constructors.
➢ Provides the flexibility of using different format of data at
run time depending upon the situation.
Example
void factorial::display()
/*Program to find factorial of a {
number using constructor*/ int fact=1;
if(n==0)
#include<iostream> cout<<"\n factorial=1";
else
using namespace std; for(int i=1; i<=n; i++)
class factorial {
{ fact=fact *i;
int n; }
cout<<"\n factorial="<<fact;
public: }
factorial(int);
void display(); int main()
}; {
int x;
cout<<"\n enter the number to find its
factorial::factorial(int number) factorial";
{ cin>>x;
factorial obj(x); //dynamic initialization of
n=number;
object
} obj.display();
return 0;
}
Dynamic Constructor

▪ The constructor can also be used to allocate


memory while creating objects.

▪ This enables 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 time of their


construction is known as dynamic construction of
objects.

▪ The memory is allocated with the help of new


operator.
Example 1
#include<iostream>
void test::display()
using namespace std; {
class test cout<<"The value of object's pointer is:
{ "<<*ptr<<endl;
int *ptr; }
public:
test(); int main()
{
test(int);
test obj;
void display(); test obj1(40);
}; obj.display();
test::test() obj1.display();
{ return 0;
ptr=new int; }
*ptr=100;
} OUTPUT:
test::test(int t) The value of object's pointer is:100
{ The value of object's pointer is:40
ptr=new int;
*ptr=t;
}
Example 2
#include<iostream> void example::display()
{
using namespace std;
cout<<name<<endl;
class example }
{ int main()
char *name; {
int length; char *a= “Welcome to";
public: example e1(a), e2(“C++"), e3(“World");
e1.display();
example();
e2.display();
example(char *);
e3.display();
void display(); return 0;
}; }
example::example()
{
length = 0; OUTPUT:
name = new char[length+1]; Welcome to
} C++
example::example(char *e) World
{
length = strlen(e);
name = new char[ length+1];
strcpy( name,e);
}
Introduction to Destructors
▪ A destructor is a special
member function of class
which is used to destroy the
objects that have been created
by a constructor. class A
{
public:
▪ The destructor is called
~A(); //Destructor
automatically by the compiler declaration
when the object goes out of };
scope.

▪ Like a constructor, the


destructor is a member
function whose name is the
same as class name but is
preceded by a tilde ~ sign.
Introduction to Destructors (Cont..)

▪ Destructor never takes any argument.

▪ Destructors does not return any value.

▪ Destructor cannot be overloaded.

▪ Whenever new is used to allocate memory in the


constructors, delete should be used to free that
memory.

Note: Objects are destroyed in reverse order of their creation.


Example
#include<iostream>
using namespace std; ABC::~ABC() //Definition of Destructor
{
cout<<"\nObject is destroyed";
class ABC
}
{
int a; int main()
public: {
ABC(int); //Constructor ABC obj1(10); Destructor is
automatically called
void display(); obj1.display();
by the compiler
return 0;
~ABC(); //Destructor once the object
} goes out of scope.
};
ABC::ABC(int x)
{
a=x; OUTPUT:
} a=10
void ABC::display() Object is destroyed
{
cout<<"a="<<a;
}
Thank You

You might also like