-->

Constructor And Destructor

Question
CBSEENCO12011613

Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute?

Solution
 

Constructor

Destructor

Purpose

Constructor is used to initialize the instance of a class.

Destructor destroys the objects when they are no longer needed.

When Called

Constructor is Called when new instance of a class is created.

Destructor is called when instance of a class is deleted or released.

Memory Management

Constructor allocates the memory.

Destructor releases the memory.

Arguments

Constructors can have arguments.

Destructor can not have any arguments.

Overloading

Overloading of constructor is possible.

Overloading of Destructor is not possible.

Name

Constructor has the same name as class name.

Destructor also has the same name as class name but with (~) tiled operator.

Syntex

ClassName(Arguments)
{
...Body of Constructor
}

~ ClassName()
{
}

Example:
Class Exam
{
int Eno; float Marks;
public:
Exam() ...Constructor
{
Eno=1; Marks = 100;
cout<<'Constructor executed...'<<endl;
}
void Show()
{
cout<<Eno<<'#'<<Marks<<endl;
}
~Exam() ...Destructor
{
cout<<'Exam Over'<<endl;
}

void main()
{
Exam E; ...Executes constructor
E.Show();
} ...Executes Destructor

Some More Questions From Constructor and Destructor Chapter

Observe the following C++ code and answer the question.

class Traveller
{
	long PNR;
	char TName[20];
public :
	Traveller() ...Function 1
	{
		cout<<'Ready'<<end1;
	}
	void Book(long P,char N[])...Function 2
	{
		PNR = P; strcpy(TName, N);
	}
	void Print() ...Function 3
	{
		cout<

Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code:

void main()
{
	Traveller T;
 _________ ...Line 1
 _________ ...Line 2
 }...Stops here 

Observe the following C++ code and answer the question.

class Traveller
{
	long PNR;
	char TName[20];
public :
	Traveller() //Function 1
	{
		cout<<'Ready'<<end1;
	}
	void Book(long P,char N[])//Function 2
	{
		PNR = P; strcpy(TName, N);
	}
	void Print() //Function 3
	{
		cout<<PNR << TName <<end1;
	}
	~Traveller() //Function 4
	{
		cout<<'Booking cancelled!'<<end1;
	}
};

Which function will be executed at }//Stops here? What is this function referred as?

Write the definition of a class PIC in C++ with the following description :

Private Members
– Pno            //Data member for Picture Number (an integer)
– Category      //Data member for Picture Category (a string)
– Location     //Data member for Exhibition Location (a string)
– FixLocation //A member function to assign
             //Exhibition Location as per category
            //as shown in the following table
Category Location
Classic Amina
Modern Jim Plaq
Antique Ustad Khan
Public Members
– Enter()    //A function to allow user to enter values
            //Pno, category and call FixLocation() function
– SeeAll() //A function to display all the data members

Answer the questions (i) to (iv) based on the following:

class Exterior
{
	int OrderId;
	char Address[20];
protected:
	float Advance;
public:
	Exterior();
	void Book(); 
	void View();
};
class Paint:public Exterior
{
	int WallArea,ColorCode;
protected:
	char Type;
public:
	Paint();
	void PBook();
	void PView();
};
class Bill : public Paint
{
	float Charges;
	void Calculate();
	public :
	Bill();
	void Billing();
	void Print();
};

(i) Which type of Inheritance out of the following is illustrated in the above example?
– Single Level Inheritance
– Multi-Level Inheritance
– Multiple Inheritance

(ii) Write the names of all the data members, which are directly accessible from the member functions of class Paint.

(iii) Write the names of all the member functions, which are directly accessible from an object of class Bill.

(iv) What will be the order of execution of the constructors, when an object of class Bill is declared?