Sponsor Area
Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute?
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) |
~ 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
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?
(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()
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.
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
}
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
void main()
{
Traveller T;
T.Book(1234567,'Ravi');...Line 1
T.Print(); ...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?
Function 4
OR
~Traveller()
It is a Destructor function
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
class PIC
{
int Pno;
char Category[20];
char Location[20];
void FixLocation();
public:
void Enter();
void SeeAll();
};
void PIC::FixLocation()
{
if(strcmpi(Category,'Classic')==0){
strcpy(Location,'Amina');
}
else if(strcmpi(Category,'Modern')==0){
strcpy(Location,'Jim Plaq');
}
else if (strcmpi(Category,'Antique')==0)
{
strcpy(Location,'Ustad Khan');
}
}
void PIC::Enter()
{
cin>>Pno;gets(Category);
FixLocation();
}
void PIC:: SeeAll()
{
cout<<Pno<<Category<<Location<<endl;
}
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?
(i) Multi-Level Inheritance is illustrated in the given example.
(ii) WallArea, ColorCode, Type, Advance are directly accessible from the member functions of class paint.
(iii)Billing(), Print(), PBook(), PView(), Book(), View() are directly accessible from an object of class Bill
(iv) When an object of class Bill is declared, the order of execution of the constructors is Exterior(), Paint(), Bill()
Sponsor Area
Sponsor Area