Sponsor Area

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

Question
CBSEENCO12011617

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

class PRODUCT
{
	int Code;
	char Item[20];
protected:
	float Qty;
public:
	PRODUCT();
	void GetIn(); void Show();
};
class WHOLESALER
{
	int WCode;
protected:
	char Manager[20];
public:
	WHOLESALER();
	void Enter();
	void Display();
};
class SHOWROOM : public PRODUCT, private WHOLESALER
{
	char Name[20],City[20];
public:
	SHOWROOM();
	void Input();
	void View();
}

(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 SHOWROOM.

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

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

Solution

(i) Multiple types of inheritance are illustrated in the given example.

(ii) Name, City, Manager, Qty are directly accessible from the memeber functions of class SHOWROOM.

(iii) Input(),View(), GetIn(), Show() are directly accessible by an object of class SHOWROOM.

(iv) When an object of class SHOWROOM is declared then the order of exection of the constructors is
(a)PRODUCT()
(b) WHOLESALER()
(c) SHOWROOM()

Question
CBSEENCO12011665

What is a copy constructor ? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it.

Solution

A copy constructor is an overloaded constructor in which an object of the same class is passed as a reference parameter.

class Point
{
	int x;
public:
	Point(){
		x=0;
	}
	Point(Point & p) // Copy constructor
	{
		x = p.x;
	}
};
void main()
{
	Point p1;
	Point p2(p1);//Copy constructor is called here
}

Question
CBSEENCO12011666

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 

Solution

void main()
{
	Traveller T;
 T.Book(1234567,'Ravi');...Line 1
 T.Print();           ...Line 2
 }                    ...Stops here