0% found this document useful (0 votes)
7 views9 pages

Constructors and Destructors

The document discusses constructors and destructors in Object-Oriented Programming, specifically in C++. It explains the types of constructors (default and parameterized), their purpose, and the concept of constructor overloading. Additionally, it covers destructors, their characteristics, and their role in resource management upon object destruction.

Uploaded by

ayeshanoor777325
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)
7 views9 pages

Constructors and Destructors

The document discusses constructors and destructors in Object-Oriented Programming, specifically in C++. It explains the types of constructors (default and parameterized), their purpose, and the concept of constructor overloading. Additionally, it covers destructors, their characteristics, and their role in resource management upon object destruction.

Uploaded by

ayeshanoor777325
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/ 9

Object Oriented Programming

Lecture 04
 Constructors
 Types of Constructors
 Constructor Overloading
 Destructors

Book: Object-Oriented Programming in C++ by Robert Lafore, 4th Ed.


Chatper-6
Constructors
• A special member function
• Called automatically when object of that class is
instantiated/created.
• Has the same name as the class it belongs to
• No return type is used for constructors
• Constructors can be parameterized
• Constructors can be overloaded
• Purpose of Constructor
– Perform task needed at the time of object creation like
• Initialize data members
• Get hold on some locks or resources(like files)
• Allocation of run time memory
Types of constructors
• There are two types of constructors
– Default Constructor
• Have no parameters

– Parameterized Constructor
• Have parameters
Types of constructors
• Default Constructor
• Takes no parameters
• Example:
class Point{
private:
int x,y;
public:
Point(){ //Default ctor
x = 0;
y = 0;
cout<<“I am default ctor of Point class”<<endl;
}
};

• An alternative way to initialize data members


class Point{
private:
int x,y;
public:
Point() : x(0), y(0){ //Default ctor
cout<<“I am default ctor of Point class”<<endl;
}
};
Types of constructors
• Parameterized Constructor
• Have parameters
• Example:
class Point{
private:
int x,y;
public:
Point(int a, int b){ //Parameterized ctor
x = a;
y = b;
cout<<“I am parameterized ctor of Point class”<<endl;
}
};

• An alternative way to initialize data members


class Point{
private:
int x,y;
public:
Point(int a, int b) : x(a), y(b){ //Parameterized ctor
cout<<“I am parameterized ctor of Point class”<<endl;
}
};
Constructor Overloading
• Constructors can be overloaded
– It means we can have multiple constructors in a
class
– Objects can be instantiated in different ways
Constructor
Example program Counter
class Counter
{
private:
unsigned int count;
public:
Counter() : count(0)
{ /*empty body*/ }

void inc_count() //increment count


{ count++; }

int get_count() //return count


{ return count; }
};

int main()
{
Counter c1, c2; //define and initialize
cout << “\nc1=” << c1.get_count(); Output
cout << “\nc2=” << c2.get_count(); c1=0
c1.inc_count(); //increment c1
c2=0
c2.inc_count(); //increment c2
c2.inc_count(); //increment c2 c1=1
cout << “\nc1=” << c1.get_count(); c2=2
cout << “\nc2=” << c2.get_count();
}
Destructors
• A special member function
• Called automatically when object of that class is
destroyed.
• Has the same name as the class it belongs to
followed by a ~ sign
• No return type is used for destructors
• Destructors can not be parameterized
• Destructors can not be overloaded
• Purpose of Destructor
– Perform task needed at the time of object killing like
• Release resources acquired by an object
• De-allocation of run time memory
Destructors
• An example
class Counter{
private:
int count;
public:
Counter() : count(0){
cout<<“I am ctor of Counter class”<<endl;
}
~Counter(){ //Destructor of Counter class
cout<<“I am dtor of Counter class”<<endl;
}
};

void main(){
cout<<“Start of main”<<endl; Output
Counter a; Start of main
{ cout<<“Inside the block”<<endl; I am ctor of Counter class
Counter b; Inside the block
cout<<“Exiting block”<<endl; I am ctor of Counter class
} Exiting block
cout<<“Exiting main”<<endl; I am dtor of Counter class
} Exiting main
I am dtor of Counter class

You might also like