Professional Documents
Culture Documents
Constructors & Destructor Assignment
Constructors & Destructor Assignment
ASSIGNMENT – 1 of 3
1) #inc1ude<iostream.h> 2) #inClude<iostream.h>
#include<stdio.h> #include<stdio.h>
class AUTO class Employee
{ {
char Model[20] ; int Emp_Id=101;
float Price ; char Ename[20];
AUTO() public
{ Employee()
Price=0 ; { return Emp_Id;}
strcpy(Model, ”NULL”); void Join()
} {
public: cin>>Emp_Id;
void Get_Set() gets(Emp_Name);
{ } };
cin>>Model>>Price; void main()
cout<<Model<<setw(10)<<Price<<setw(2); {
} Employee E;
void main() Join.E();
{ }
Auto Car;
Car.Get_Set();
}
3) #include<iostream.h> 4) #include<iostream.h>
#include<stdio.h> #include<stdio.h>
class Players class Students
{ {
int _Id=123; int Id;
char P_name[20]; char S_name[20];
public: public:
Players() students()
{ } { }
void Join() void Join()
{ {
cin>>Id; cin>>Id;
gets(p_Name); gets(S_Name);
} }; } };
void main() void main()
{ {
Players E; Students Es;
E.Players(); Es.Join();
E.Join(); }
}
5) #include<iostream.h> 6) #include<iostream.h>
#include<stdio.h> #include<stdio.h>
class Teachers class T{
{ int T_Id;
int T_Id; float Salary;
float Salary; public:
public: Teachers(t &T)
Teachers(Teachers T) { T_id=T_Id;
{ T_id=T.T_Id; Salary=Salary;
Salary=T.Salary; }
} void Join()
void Join() {
{ cin>>Id;
cin>>Id; gets(S_Name);
gets(S_Name); } };
} }; void main()
void main() {
{ Teachers T1(1,12000),T2(T1);
Teachers T1(1,12000),T2[T1]; }
}
VELAMMAL VIDHYASHRAM
ASSIGNMENT – 2 of 3
(i) In OOP, What is Function 4 referred as and when does it get invoked/called?
(ii) In OOP, Which concept is illustrated by Function 1 and Function 3 together?
2) class Test
{
int Reg_no, Max, Min, Score;
public:
Test() //Function1
{ Reg_no=101;Max=100;Min=40;Score=pscore; }
Test(int P,int S) //Function 2
{ Reg_No=P; Max=100; Min=40; Score=s;
}
~Test() //Function 3
{ cout<<”Test Over”;
}
void display() //Function 4
{
cout<<Reg_No<<”:”<<Max<<”:”<<Min<<endl;
cout<<”Score:”<<Score;
}
};
(i) As per OOP, Function1 and Function 2 are called what type of constructor?
(ii) As per OOP, Which concept is illustrated by Function 1 and Function 2 together?
(iii) What is Function 3 specifically referred as? When Function 3 will be
invoked/called?
3) class Science
{ char Topic[20]; int weight;
public:
Science() //Function 1
{ strcpy(Topic,”Optics”); Weight=30; cout<<”Topic Activated”;
}
~Science() // Function 2
{ cout<<”Topic Deactivated”; } };
(i) Name the specific features of class shown by Function 1 & Function 2 in the
Above code?
(ii) How would you Function 1 & Function 2 get executed?
4) class Tree
{ int time;
public:
Tree() //Function 1
{ time=0; cout<<”Hi”; }
~Tree() // Function 2
{ cout<<”Hello”; }
void exam() // Function 3
{ cout<<”Good”; }
Tree(int Duration) // Function 4
{
time=Duration;
cout<<”Exam Starts”;
}
Tree(Tree & T) // Function 5
{ time=T.Duration; cout<<”Exam Finished”;
}
};
(i) In OOP, What is Function 1 referred as and when does it get invoked?
(ii) In OOP, What is Function 2 referred as and when does it get called?
(iii)Which Category of Constructors does Function 5 belong to and what is the
Purpose of using it?
(iv) Write statements that would call member Function 1 and 4.
5) class Maths
{
char chapter[20];
int marks;
public:
Maths() //Function 1
{ strcpy(chapter,”Geometry”); marks=10; cout<<”Chapter Initialized”; } ~Maths()
//Function 2
{ cout<<”Chapter Finished”; }
};
(i) Name the specific features of class shown by Function 1 & Function 2 in the
Above code.
(ii) How would you Function 1 & Function 2 get executed?
6) class Conference
{
int duration,time;
public:
Conference() // Function 1
{
Duration=8;cout<<”Inaguration”;
}
~Conference() // Function 2
{
cout<<”Concluding”;
}
void session(int s=1) // Function 3
{
cout<<”Session”<<s<<”is on”<<endl;
}
Conference(int Duration) // Function 4
{
time=Duration; cout<<”Inaguration”<<endl; } };
(i) In OOP, what is Function 2 referred to as and when does it get invoked?
Which member function out of Function 1, Function 2, Function 3 & Function 4 shown
in the above definition of class Work will be called an execution of statement written as
statement 2? What is this specifically known as out of Destructor or Copy Constructor or
Default Constructor?
9) class Bazar
{
char type[20];
char product[20];
int qty;
float price;
Bazar() //Function 1
{
strcpy(type,”Electronic”);
strcpy(product,”Calculator”);
qty=10;
price=225;
}
public:
void disp() // Function 2
{ cout<<Type<<”-”<<product<<”:”<<qty<<”@”<<price<<endl; }
};
void main()
{
Bazar b; //Statement 1
b.disp(); //Statement 2
}
(i) Will statement 1 initialize all the data members for object B with the values given in
Function 1 ?(Yes or No). Justify your answer suggesting the correction(s) to be made in
the above code.
(ii) What shall be the possible output when the program gets executed? (Assuming, if
required – the suggested correction(s) are made in the program).
10) class Interview (CBSE D 06)
{
int Month;
public:
Interview (int y) //Constructor 1
{
Month=y;
}
Interview(Interview & t); //Constructor 2
};
(i) Create an object, such that invokes Constructor 1.
(ii) Write complete definition for Constructor 2.
i) Which specific concept of object oriented programming out of the following is illustrated
by member function 1 and member function 2 combined together?
ii) How many times the message “Book Discarded!” will be displayed after executing the
above C++ code? Out of line 1 to line 9, which line is responsible to display the message
“Book Discarded!”.
VELAMMAL VIDHYASHRAM
ASSIGNMENT – 3 of 3
Private Members:
C_No – To store Cab number.
Type – To store a character ‘A’, ‘B’ or ‘C’.
PKM – To store Per Kilo Meter charges.
Dist- To store Distance travelled (in Km).
Public Members:
A constructor member function to initialize Type as ‘A’ and C_No as 1.
A function Charges() to assign PKM as per the following table:
TYPE PKM
‘A’ 25
‘B’ 20
‘C’ 15
A function Register() to allow admin to enter the values for C_No and Type. Also,
this function should call Charges() to adding PKM charges.
A function Show_Cab() to allow user to enter the value of Dist and display C_No,
Type, PKM , PKM*Dist (As amount) on screen.
Private Members:
G_Code of type string.
G_Type of type string.
G_Size of type integer.
G_Fabric of type string.
G_Price of type float.
A function Assign( ) which calculates and assigns the value of G_Price as follows:
For the value GFabric “COTTON”
G_Type G_Price
PANT 1300
SHIRT 1100
For G_Fabric other than “COTTON” the above mentioned G_Price gets
Reduced by 10%.
Public Members:
A constructor to assign initial values of G_Code, G_Type, and G_Fabric with the
word “NOT ALLOTTED” and G_Price and G_Size with 0.
A function Input() to input the values of the data members G_Code,G_Type,G_Size
and G_Fabric and invoke the Assign().
A function Display ( ) which displays the content of all the data members for a
Garment.
3. Define a Class School in C++ with the following descriptions:
Private Members:
Stu_Name of type string
Stu_Roll_No of type integer
Stu_Age of type integer
Stu_Ph_No of type integer
Stu_School of type string
Public Members:
A constructor to assign inital value Stu_School with the word “TVIS” and Ph_no as
0.
A function Input() to input the values of the data members Stu_Name,
Stu_Roll_No, Stu_Age, Stu_Ph_No.
A function Display ( ) which displays the content of all the data members for a
School.
Private Members:
R_No – Data Member to store Room number.
Name - Data Member to store customer name.
Charges - Data Member to store per day charges.
Days - Data Member to store number of days of stay.
Compute() - A function to calculate and return amount as
Days*Charges and if the value of Days*Charges is more
than 11000 then as 1.02* Days* Charges.
Public Members:
Public Members:
_____________________________________________________________________________________
CLASSES AND OBJECTS & CONSTRUCTORS AND
DESTRUCTOR
THEORY QUESTIONS
1. What is the relationship of class and objects? How is memory allocated to a class
and its objects?
2. What do you understand about member function? How does a member function
differ from an ordinary function?
3. What is the significance of acces labels?
4. While definig a class, you can define its methods(member functions) inside or
outside the class definition. How are these two definitions different?
5. What do you understand by Data Encapuslation and Data Hiding? Also, give a
suitable C++ code to illustrate both.
6. What is Constructor? Write the need of the constructor.
7. List out the characteristics of constructors.
8. Write the syntax to create constructor.
9. Can we define the constructor outside of the class? If yes, How to define it outside of
the class?
10. What will happen if you define the constructor in private?
11. List out the types of constructor.
12. Explain the role of default constructor? When it’s considered equivalent to a
parameterized constructor? or How it is equivalent to a constructor having default
arguments.
13. What is parameterized constructor? How is it useful?
14. Discuss the various situations when a copy constructor is automatically invoked.
15. Why the argument to a copy constructor is passed by reference (‘&’)?
16. Differentiate between Default constructor and copy constructor. Give an example for
each.
17. Differentiate between parameterized constructor and copy constructor. Give an
example for each.
18. Explain the types of constructor invocations.
19. Distinguish between Statement 1 and Statement 2:
Student s1 (10, 20); //Statement 1
Student s2=Student (10, 20); // Statement 2
20. What is meant by Destructor? List out the characteristics of destructors.
(or) Why is a Destructor required in classes? Illustrated with an example.
21. Differentiate between constructor and destructor.
22. How constructor and destructor are different from other member functions?
23. What do you mean by a temporary instance of a class? What is it use? How it is
created?
24. What is meant by constructor overloading? or what is multiple constructors? Can a
destructor be overloaded?
25. Explain constructor with default argument? and what is the use of it? Give an
example.
__________________________________________________________________________________