Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

VELAMMAL VIDHYASHRAM

ASSIGNMENT – 1 of 3

GRADE: XII CONSTRUCTORS AND DESTRUCTOR SUB: C.S

1. FIND THE OUTPUT FOR THE FOLLOWING C++ CODES:

1) class Train 2) class Stock


{ int Tno, Trip_No, PC ; { long int ID;
public : float Rate;
Train(int TN=1) int Date;
{ public:
Tno=TN ; Trip_No=0 ; PC=0 ; Stock()
} {
void Trip(int TC=100) ID=1001; Rate=2000; Date =1; }
{ void Reg_Code(long int I, int R)
Trip_No++ ; {
PC+=TC ; ID=I;
} Rate=R;
void show() }
{ void Change(int New, int DT)
cout<<Tno<<“ : “<<Trip_No<<endl; {
cout<<PC<<endl; Rate+=New;
} Date=DT;
}; }
void main() void show()
{ {
Train T(10), N; cout<<”Date”<<Date<<endl;
N.Trip(); cout<<ID<<”#”<<endl;
T.show() ; }
T.Trip(70) ; };
N.Trip(40) ; void main()
N.show() ; {
T.show() ; Stock A,B,C;
} A.Reg_Code(1024,50);
B.Reg_Code(2015,300);
B.Change(100,29);
C.Change(-20,20);
A.show(); B.show(); C.show();
}

3) class Calc 4) class Product


{ char Grade; { int P_No;
int Bonus; char P_Name[20];
public: float Price;
Calc() public:
{ Grade=’E’; Bonus=0; } Product()
void Down(int G) {
{ Grade-=G; } P_No=100; strcpy(P_Name,”None”); Price=1000;
void Up(int G) }
{ Grade+=G; Bonus++; } void Assign(int I, char Name[], float P)
void show() {
{ cout<<Grade<<”#”<<Bonus<<endl; P_No+=I; strcpy(Name,P_Name); Price=P;
} }
}; void RaiseP(float P)
void main() { Price+=P; }
{ void ReduceP(float P)
Calc C; { Price-=P; }
C.Down(2); void disp()
C.show(); { cout<<P_No<<”:”<<P_Name<<”:”<<Price<<endl;
C.Up(7); } };
C.show(); void main()
C.Down(2); {
C.show(); Product O,T;
} O.disp(); O.Assign(1,”Pen”,95);
T.Assign(2,”Pencil”,55);
O.RaiseP(10); T.ReduceP(5);
T.disp(); O.disp(); }
5) class Quiz 6) class Mausam
{ {
int Round ; int City, Temp, Humidity;
float Score ; public:
public : Mausam(int C=1)
Quiz() {
{ Round=1 ; Score=0 ;} City=C;
Quiz(Quiz &Q) Temp=10;
{ Humidity=63;
Round=Q.Round+1 ; }
Score=Q.Score+10 ; void Sun(int T)
} { Temp+=T; }
void Get_Bonus(float B=5) void Rain(int H)
{ { Humidity+=H; }
Score+=B; void Check()
} {
void Show_Score() cout<<City<<”:”<<Temp<<”&”<<Humidity<<”%”<<endl;
{ }
cout<<Round<<”#”<<Score<<endl; };
} void main()
}; {
void main() Mausam M,N(2);
{ M.Sun(5);
Quiz A; M.Check();
A.Show_Score(); N.Rain(10);
A.Get_Bonus(10); N.Sun(2);
A.Show_Score(); N.Check();
Quiz B(A); B.Get_Bonus(); M.Rain(15); M.Check(); }
B.Show_Score(); }
7) class A 8) class Player
{ {
public: int Score, Level ;
A() char Game ;
{ public :
cout<<”Constructor A\n”; Player(char GGame=’A’)
} {
~A() Score=0 ;
{ Level=1 ;
cout<<”Destructor A\n”; Game=GGame ;
} }
}; void start(int SC) ;
class B void Next() ;
{ void disp()
public: {
B() cout<<Game<< ”@”<<Level<<endl;
{ cout<<Score<<endl;
cout<<”Constructor B\n”; }
} };
~B() void main()
{ {
cout<<”Destructor B\n”; Player P,Q(‘B’);
} P.disp();
}; Q.start(75);
class C Q.Next();
{ P.start(120);
public: Q.disp();
C() P.disp();
{ }
cout<<”Constructor C\n”; void Player::Next()
} {
~C() Game=(Game==’A’)?’B’:’A’;
{ }
cout<<”Destructor C\n”; void Player::start(int SC)
} {
}; Score+=SC;
if(Score>=100)
void main() Level=3;
{ else if(Score>=50)
C oc; Level=2;
B ob; else
A oa; Level=1;
} }
2. FIND THE ERROR(S) IN THE FOLLOWING C++ CODES:

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

GRADE: XII CONSTRUCTORS AND DESTRUCTOR SUB: C.S

1. Identify the type of the constructor and name of the function


(Member/Non-Member) from the following codes:
1) class TVIS 2) class Sample
{ {
int a,b; int r_no,age,mark,total ;
public: public:
TVIS() { } Sample();
void get(); Sample(int,int);
void print(); void input();
}; };
TVIS t3 ; void main()
void main() {
{ TVIS t1,t2; } Sample s1,s2,s3,s4; void print(); }

3) class Tour 4) class State


{ {
int pass_no,seat_no; int total_population , ratio;
char pass_name[20]; char name[10];
public: public:
Tour(){ ---- } void read();
Tour(Tour & s){ --- } State(int,int) { ----- }
void read(); State(int ,float y=10);
}; State (State &E) {---}
int a=10; };
void main() int x
{ int y;
void show(); void main()
Tour r1,r2,r3; {
r1.get(); State py(20,23),mh(10),dl(py);
r1.show(); } void print();
x=100;
y=200; tn.read(); tn.print();}

I. Answer the questions after going through the following classes:


1) class Seminar
{
int time;
public:
Seminar() //Function 1
{ Time=30; }
void Lecture() // Function 2
{ cout<<”Welcome” ;}
Seminar (int Duration) //Function 3
{
time=Duration;
cout<<”Seminar starts now”;
}
~Seminar() //Function 4
{
cout<<”Vote of thanks”;
}
};

(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?

(ii) In OOP, which concept is illustrated by Function 1 and Function 4 together?


Write an example illustrating the calls for these two functions.
7) 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”;
}
Match(Match & m) //Function 4
{
Time=m.Duration;
cout<<”Like Previous Match”;
}
};
(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.
8) class Work
{
int work_id; char work_type;
public:
~Work() //Function 1
{
cout<<”un-Allocated”<<endl;
}
void status() //Function 2
{
cout<<work_id<<”:”<<work_type<<endl;
}
Work() //Function 3
{
work_id=10; work_type=’ T ’;
}
Work( Work & w) //Function 4
{
work_id=w.work_id+12;
work_type=w.work_type+1;
}
};
(i) Which member function out of Function 1, Function 2, Function 3 & Function 4
shown in the above definition of class Work is called automatically, When the scope of an
object gets over? is it known as Constructor or Destructor or Overloaded Function
or Copy Constructor?

(ii) Work w; //Statement 1


Work Y(w); // Statement 2

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.

11) class Sample


{ int age;
public:
Sample()//Constructor 1
{ ----- }
Sample(int x, int y) //Constructor 2
{ ---- }
Sample (Sample & R) //Constructor 3
{ ----- }
};

(i) Create an object, such that invokes Constructor 1&2&3.


(ii) Write complete definition for Constructor 2.

12. class book


{ long code;
char title[20];
float price;
public:
book( ) // member function 1
{ cout<<”Bought “<<endl;
code=10;
Strcpy(title,”notitle”);
Price=100;
}
book(int c,char t[],float p) // member function 2
{ code=c;
strcpy(title, t);
price=p;
}
void update(float p) // member function 3
{
price+=p;
}
void display() // member function 4
{
cout<<code<<”:”<<title<<”:”<<price<<Endl;
}
~book() // member function 5
{
cout<<”book discarded !”<<endl; }};
void main() // Line 1
{ // Line 2
Book b, c(101, “Truth”,350); // Line 3
for(int I=0;I<4;I++) //Line 4
{ // Line 5
b.update(50); c.update(20); // Line 6
b.display( ); c.display( ); // line 7
} // Line 8
} // Line 9

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

GRADE: XII CONSTRUCTORS AND DESTRUCTOR SUB: C.S

1. Define a class CABS in C++ with the following specifications:

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.

2. Define a Class Garments in C++ with the following descriptions:

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.

4. Define a class RESORT in C++ with the following specifications:

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:

 Get_Info() – A function to enter the content R_No, Name, Charges and


Days.
 Disp_Info() – A function to display R_No, Name, Charges, Days and
Amount( Amount to be displayed by calling function
Compute()).

5. Define a class ADMISSION in C++ with the following specifications:


Private Members:
 AD_No - Integer Ranges (10-2000).
 Name - Array of characters (string).
 CLASS - Character.
 Fees - float.

Public Members:

 Function Read() to read an object of ADMISSION type.


 Function Disp() to display the details of an object.
 Function Draw__Nos() to choose 2 students randomly and call Disp() to
display their details.

_____________________________________________________________________________________
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.

__________________________________________________________________________________

You might also like