HOME ASSIGNMENT – 3
TOPIC : CONSTRUCTOR & DESTRUCTOR
3.1 What is a constructor? Write its characteristics.
3.2 What is a destructor? Write its characteristics.
3.3 How many types of constructors are there? Explain with suitable example.
3.4 In copy constructor, object is passed by reference only. Why?
3.5 Answer the questions (i) and (ii) after going through the following class:
class Seminar
{ int Time;
public:
Seminar() //Function 1
{ Time=30;cout<<”Seminar starts now”<<endl; }
void Lecture() //Function 2
{ cout<<”Lectures in the seminar on”<<endl; }
Seminar(int Duration) //Function 3
{ Time=Duration;cout<<”Seminar starts now”<<endl; }
~Seminar() //Function 4
{ cout<<”Vote of thanks”<<end1; }
};
i) In Object Oriented Programming, what is Function 4 referred as and when does it get
invoked/called?
ii) In Object Oriented Programming, which concept is illustrated by Function 1 and Function 3
together? Write an example illustrating the calls for these functions.
3.6 Answer the questions (i) and (ii) after going through the following program:
class Match
{ int Time;
public:
Match() //Function 1
{ Time=0;
cout<<”Match commences”<<endl;}
void Details() //Function 2
{ cout<<”Inter Section Basketball Match”<<endl; }
Match(int Duration) //Function 3
{ Time=Duration;
cout<<”Another Match begins now”<<endl; }
Match(Match &M) //Function 4
{ Time=M.Duration;
cout<<”Like Previous Match ”<<end1; }
};
i) Which category of constructor - Function 4 belongs to and what is the purpose of using it?
ii) Write statements that would call the member Functions 1 and 3.
CHALLENGING QUESTION
3.7. Define a class named Admission in C++ with following description?
Private members:
admno integer (ranges 10-1500)
name string of 20 characters
cls integer
fees float
Public members:
A constructor which initialized admno with 10, name with “NULL”, cls with 0 & fees with 0
Function getdata() to read the object of Admission type.
Function putdata() to print the details of object of admission type.
Function draw_nos() to generate the admission no. randomly to match with admno and display the detail
of object.
LAB EXERCISES – 3
TOPIC : ARRAY OF OBJECTS, CONSTRUCTOR & DESTRUCTOR
LAB 3.1 Define a class Candidate in C++ with the following descriptions:
Private Members:
RNo of type long
Name of type string
Score of type float
Remarks of type string
A function AssignRem( ) to assign Remarks as per the Score obtained by a candidate. Score range
and the respective Remarks are shown as follows:
SCORE REMARKS
>=50 Selected
Less than 50 Not selected
Public Members:
A function Enter( ) to allow user to enter values for RNo, Name, Score and call function
AssignRem() to assign the Remarks.
A function DISPLAY( ) to view the content of all the data members.
A function Return_Score() to return the score
Now write main function to do the following:
i) To create an array of 5 candidates
ii) To obtain values of the data members of all candidates by calling Enter() function repeatedly
iii) To show details of all the data members by calling Display() function repeatedly
iv) To display the details of the candidates who were selected. (Add appropriate function in the
class)
v) To display the details of a candidate whose name is entered by the user. (Add appropriate
function in the class)
LAB 3.2 Define a class outfit with the following specifications:-
Private Members:
O_Type of type string
O_Code of type string
O_Size of type int
O_Fabric of type string
O_Price of type float
A function int_price() which calculates and assigns value of O_Price as follows:
For the value of O_Fabric=”DENIM”:
O_Type O_Price
TROUSER 1500
JACKET 2500
For O_Fabric other than “DENIM” the above price gets reduced by 25%.
Public Members:
Constructor to assign initial values of O_Code,O_Type and O_Fabric with the word “NOT
INITIALISED” and O_Size and O_Price with 0.
A function Input() to input the values of O_Code,O_Type,O_Size and O_Fabric and invoke the
function int_price().
A function Display() to Display the contents of all data members.
Now write main function() to do the following:
i) To create an array of 5 objects of class Outfit
ii) To enter the values in all the data members of the objects
iii) To display the list of only those objects whose fabric is “DENIM”
iv) To display the details of all the objects