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

ASSIGNMENT: CLASS, CONSTRUCTOR AND DESTRUCTOR

CLASS: XII, C++


Q-1 Answer the questions i) and ii) after going through the following class:
class test { char paper[20];
int marks;
test ( ) // function 1
{ strcpy(paper,”Computer”);
marks=0; }
test(char p[]) // function 2
{ strcpy(paper,”C”);
marks=0; }
test (test &M) // function 3
test (char P[],int M) // function 4
{ strcpy (paper,”C”);
marks=M; }};
i) What changes will you have to make to the code so that statements like the following get executed?
Test T;
After suggesting the change, write a statement to invoke function 2
ii) Which feature of OOP is demonstrated using function 1, function 2, function 3 and function 4 in the above
class test?
iii) Write statement in C++ that would execute function 2 and function 4 of class test.
iv) Write the definition of function 3..
v) Which members function out of function1, 2, 3, 4 will be called on execution of statement written as line 2?
What is the function specially called.
a. Test t // line1
b. Test t1 (t) // line 2
Q-2 Answer the questions i) and iii) after going through the following class:
Consider the following class counter:
class counter
{ protected :
unsigned int count;
public :
counter()
{ count = 0; }
void inc_count()
{ count++; }
int get_count()
{ return count; } };
Write code in C++ to publically derive another class new_counter from class counter.
class new_counter should have the following additional function members in the public visibility mode:
(i) A parameterized constructor to initialize the value of count to the value of parameter.
(iii) Reset() to set the value of data member count to 0.
(ii) dec_count() to decrease the value of data member count by 1.
Q-3 Answer the questions after going through the following class:
class computer
{ char C_Name [20];
int Cno;
public:
computer (computer &); // function 1
computer ( ); // function 2
computer (chat *n, char *c); //function 3
void campin( ); //function 4
void campout( ); }; //function 5
i) Complete the definition of the function1. .
ii) Write a statements in C++ to execute function 2 and function 4
iii) Which feature of OOP shown by function 1, function 2, function 3 and function 4 in the above class test?
Q-4 Answer the questions (i) and (ii) after going through the following class:
class player { int health;
int age;
public:
player() { health=6; age=18 } //Constructor1
player(int s, int a) {health =s; age = a ; } //Constructor2
player( player &p) { } //Constructor3
~player() { cout<<”Memory Deallocate”; } //Destructor };
void main()
{ player p1(7,24); //Statement1
player p3 = p1; //Statement3 }
i) When p3 object created specify which constructor invoked and why?
ii) Write complete definition for Constructor3?
Q-5 Answer the questions i) and ii) after going through the following class:
class DeCryption
{ int code; char ans[20];
public :
DeCryption(int c); //Function 1
DeCryption(DeCryption & ); //Function 2
~DeCryption(); //Function 3
void InDe(); }; //Function 4
i) What is Function 2 specifically known as in C++? Write a statement(s) to invoke it
ii) Out of the following which of the option is correct for calling function 1
Option 1 DeCryption D(4);
Option 2 DeCryption D(D1);
And also Write a statement to call function 4
Q-6 Define a class PRODUCT in C++ with the following descriptions. (4)
Private members:
A data member Pno(product number) of type long
A data member name of type string
A data member Price of type float
A data member category of type string
A member function Calc_Sellprice( ) that returns the selling price of the product after considering the
following
Category Selling price
“Indian” price + Tax(3% of the price)
“Imported” price + Tax(5% of the price)
Public members :
 A constructor to assign initial values of Pno as 101, name as “Raju”, price as 50, Category as “Indian”.
 A function Enter( ) to allow user to enter values for Pno,Name,Price and category.
 A function Display ( ) to allow user to view the Pno,name and Selling price.(Selling price to be calculated
by calling function Calc_Sellprice( )

Q-7 Define a class CHANNEL in C++ with the following description:


private members : id of type long
channel_name of type string
channel_type of type char( N: News M: Movie K: kids)
no_prog of type int (number of programs)
rating of type int
public members:
 void Enter(int Lid) : A function to assign id as Lid ,allow user to type in
channel_name,channel_type,no_prog and rating
 void output( ) : A function to display id, channel_name, channel_type, no_prog and call Star
function.
Star( ) : A function to dispaly * (Star) symbol rating number of times in a line. For Example if rating is 3 should
display *** .
Q-8 Observe the following C++ code and answer the question I and II
class passenger
{ long PNR;
char Name[20];
public :
passenger ( ) // function 1
{ cout<<”Ready ”<<endl; }
void Book(long P char N[ ]) // function 2
{ PNR=P;
Strcpy (Name ,N); }
void print() // function 3
{ cout<<PNR<<Name<<endl; }
~passenger () // function 4
{ cout<<”Booking cancelled”<<endl; }
};

i) 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( )
{ Passenger P;
_______________ //line 1
_______________ // line 2
}//ends here
ii) Which function will be executed at }//ends here What is this function referred as ?
Q-9 Define a class PhoneBill in C++ with the following descriptions.
Private members:
CustomerName of type character array
PhoneNumber of type long
No_of_units of type int
Rent of type int
Amount of type float.
calculate ( ) This member function should calculate the value of amount as Rent+ cost for the units. Where cost for
the units can be calculated according to the following conditions.
No_of_units Cost
First 50 calls Free
Next 100 calls 0.80 @ unit
Next 200 calls 1.00 @ unit
Remaining calls 1.20 @ unit

Public members:
* A constructor to assign initial values of CustomerName as “Raju”, PhoneNumber as 259461, No_of_units as 50,
Rent as 100, Amount as 100.
* A function accept ( ) which allows user to enter CustomerName, PhoneNumber, No_of_units and Rent and
should call function calculate ().
* A function Display ( ) to display the values of all the data members on the screen
Q-10 Find the output of the following program
#include<iostream.h>
class Train
{ int Tno,Tripno,pcount;
public :
Train( int Tmno=1)
{ Tno=Tmno;
Tripno=0;
pcount=0; }
void trip(int TC=100)
{ Tripno++;
pcount+=TC; }
void show( )
{ cout<<Tno<<":"<<Tripno<<":"<<pcount<<endl; } };

void main( )
{ Train T(5),n;
n.trip();
T.show();
T.trip(50);
n.trip(35);
n.show();
T.show(); }

Q-11 Observe the following C++ code carefully and obtain the output, which will appear on the screen after
execution of it.
#include <iostream.h>
claas Aroundus
{ int Place, Humidity, Temp;
public:
Aroundus(int P=2)
{ Place=P; Humidity=60; Temp=20;}
void Hot(int T)
{ Temp+=T;}
void Humid(int H)
{ Humidity+=H;}
void JustSee()
{ cout <<Place << " : " <<Temp< < " : " <<Humidity < < " % ” <<endl ;} };
void main ( )
{ Aroundus A,B(5),
A.Hot(10),
A.JustSee(),
B.Humid(15);
B.Hot(2);
B.JustSee(),
A.Humid(5);
A.JustSee(); }

Q-12 Define a class Tourist in C++ with the following specification:


Data Members
Camo - to store Bus No
Origin - to store Place name
Destination - to store Place name
Type - to store Car Type such as „E‟ for Economy
Distance - to store the Distance in Kilometers
Charge - to store the Car Fare
Public : Member Functions
A constructor function to initialize Type as „E‟ and Freight as 250
A function CalcCharge() to calculate Care as per the following criteria :
Type Charge
'E' l6*Distance
'A' 22*Distance
„L‟ 30*Distance
A function Enter () to allow user to enter values for Camo, Origin, Destination, Type and Distance. "also, this
function should call CalcCharge() to calculate Fare.
A function Show() to display the content of all the data members on screen.

Q-13 Write the definition of a class FRAME in C++ with following description :
Private Members
FID // data member of integer type
Height // data member of float type
Width // data member of float type
Amount // data member of float type
GetAmount() // Member function to calculate and assign // Amount as 10∗Height∗Width
Public Members
GetDetail() // A function to allow user to enter values of // FID, Height, Width. This Function should
// also call GetAmount() function to calculate // Amount
DispDetail() // A function to display the // values of all data members
Q-14 Write the output of the following C++ program code :
Note : Assume all required header files are already being included in the program.
class Eval
{ char Level;
int Point;
public: Eval(){Level=‟E‟;Point=0;}
void Sink(int L)
{ Level-=L; }
void Float(int L)
{ Level+=L; Point++; }
void Show()
{ cout<<Level<<”#”<<Point<<endl; } };
void main()
{ Eval E;
E.Sink(3);
E.Show();
E.Float(7);
E.Show();
E.Sink(2);
E.Show(); }
Q-15 Define a class Bill in OOP with the following specification
Private members :
Bill_no type long(bill number )
Bill_period type integer (number of month)
No_of_calls type integer (number of mobile calls)
Payment_mode type string(“online” or “offline”)
Amount type float(amount of bill)
Calculate_Bill() function to calculate the amount of bill given as per the following condition

No_of_calls Calculation
Rate/Call(in Rs.)
<=500 1.0
501-1200 2.0
>1200 3.0
Also, the value of Amount should be reduced by 5% if Payment_mode is online
Public members:
A member function New_Bill() that will accept the values for Bill_no, Bill_period, No_of_calls,
Payment_mode from the user and invoke Caluclate_Bill() to assign the value of Amount
A member function Print_Bill() that will display all details of a Bill
Q-16 Define a class DanceAcademy in C++ with following description
Private members :
Enrollno of type int
Name of type string
Style of type string
Fee of type float
A member function chkfee( ) to assign the value of fee variable according to the style entered by the user according
to the criteria as given below:
Style Fees`
Classical 10000
Western 8000
Freestyle 11000
Public Members
A function enrollment() to allow users to enter values for Enrollno,Name, Style and call function chkfee()to assign
value of fee variable according to the Style entered by the user
A function display () to allow users to view the details of all the data members
Q-17 Rewrite the following program after removing the syntactical error(s), if any .Underline each correction (2)
#include<iostream.h>
Class item
{ long Id, Qty=5;
public
void purchase{ cin>>iD>>Qty; }
void Sale( )
{ cout<<setw(5)<<id<<”old”<<Qty<<endl;
Cout<<”New:”<Qty<<endl;
} };
void main()
{ item I;
Purchase ( );
I. Sale();
Sale.I( ); }

Q-18 Define a class CONTESTS in C++ with the following description:


Private
Eventno - integer
Description - char(30)
Score - integer
Qualified - char
Public
 A constructor to assign initial values Eventno as 11, Description as „„School level‟‟, Score as 100, qualified
as „N‟.
 Input() – To take the input for Eventno, description and score.
 Award (int cutoffscore) – To assign qualified as „Y‟, if score is more than the cutoffscore that is passed as
argument to the function, else assign qualified as „N‟.
 Displaydata() – to display all data members
Q-19 Write the definition of a class METROPOLIS in C++ with following description:
Private Members
Mcode //Data member for Code (an integer)
MName //Data member for Name (a string)
MPop //Data member for Population (a long int)
Area //Data member for Area Coverage (a float)
PopDens //Data member for Population Density (a float)
CalDen() //A member function to calculate //Density as PopDens/Area
Public Members
Enter () //A function to allow user to enter values of //Mcode, MName, MPop, Area and call
CalDen() //function
ViewALL () //A function to display all the data members //also display a message "Highly Populated
Area" // if the Density is more than 12000
Q-20 Find and write the output of the following C++ program code: Note: Assume all required header files are
already being included in the program,
class Player { int Score, Level ;
char Game;
public: Player (char GGame='A' )
{score=0; Level=1; Game=GGame; }
void Start (int SC) ;
void Next() ;
void Disp() { cout<<Game< < " @" < <Leve1< <end1,'
cout<<Score<<end1 ; } };
void main ()
{ Player P,Q ('B') ;
P.Disp();
Q.Start (75) ;
Q.Next () ;
P. Start (120);
Q.Disp();
P.Disp(); }
void Player::Next ()
{ Game=Game==‟A‟)?‟B‟:‟A‟; }
void Player::Start (int SC)
{ Score+=SC;
If (score >= 100 )
Level=3;
else if (Score>=50 )
Level=2;
else
Level=1; }
Q-21 Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are
included:
class FICTION { long FCode;
char FTitle[20];
float FPrice;
public:
FICTION () //Member Function 1
{ cout<<"Bought"<<endl;
FCode=100; strcpy(FTitle,"Noname"); FPrice=50; }
FICTION(int C,char T[],float P) //Member Function 2
{ FCode=C;
strcpy(FTitle,T);
FPrice=P; }
void Increase (float P) //Member Function 3
{ FPrice+=P; }
void Show () //Member Function 4
{ cout<<FCode<<":"<<FTitle<<":"<<FPrice<<endl; }
~FICTION () //Member Function 5
{ cout<<"Fiction removed!"<<endl; } };
void main () //Line 1
{ //Line 2
FICTION Fl,F2(101,"Dare",75); //Line 3
for (int I=0;I<4;I++) //Line 4
{ //Line 5
Fl.Increase(20);F2.Increase(15); //Line 6
Fl.Show();F2.Show(); //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?
 Data Encapsulation
 Data Hiding
 Polymorphism
 Inheritance
ii) How many times the message "Fiction removed!" will be displayed after executing the above C++ code?
Out of Line 1 to Line 9, which line is responsible to display the message "Fiction removed! "?

Q-22 Observe the following C++ code and answer the questions (i) and (ii). Note : Assume all necessary files are
included.
class GAME { int Pcode,Round,Score;
public:
GAME() //Member Function 1
{ Pcode=1;Round=0;Score=0; }
GAME(GAME &G) //Member Function 2
{ Pcode=G.Pcode+1;
Round=G.Round+2;
Score=G.Score+10; } };
void main() {
_____________ //Statement 1 }
i) Which Object Oriented Programming feature is illustrated by the Member Function 1 and Member
Function 2 together in the class GAME ?
ii) Write Statement 1 and Statement 2 to execute Function 1 and Member Function 2 respectively. 1
Q-23 Rewrite the following program after removing the syntactical error(s), if any .Underline each correction
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>
class product { int product_code,qty,price;
char name[20];
public: product()
{ product_code=0;qty=0;price=0;
name=NULL; }
void entry() { cout<<"\n Enter code,qty,price";
cin>>product_code>>qty>>price;
gets(name); } };
void tot_price()
{ return qty*price; }
void main()
{ p product
p.entry();
cout<<tot_price(); }
Q-24 Find and write the output of the following C++ program code: Note: Assume all required header files are
already being included in the program,
class Share
{ long int Code;
float Rate;
int DD;
public:
Share(){Code=10 ;Rate=10.0; DD=1;}
void GetCode(long int C,float R)
{ Code=C;
Rate=R; }
void Update(int Change,int D)
{ Rate+=Change;
DD=D; }
void Status()
{ cout<<"Date:"<<DD<<endl; cout<<Code<<"#"<<Rate<<endl; };
void main()
{ Share S,T,U;
S.GetCode(1324,350) ;
T.GetCode(1435,250) ;
S.Update(50,28) ;
U.Update(-25,26);
S.Status();
T.Status();
U.Status(); }

Q-25 Write the output of the following C++ program code: Note: Assume all required header files are already
being included in the program.
class seminar
{ char topic[30];
int charges;
public:
seminar() { strcpy(topic,"Registration");
charges=5000; }
seminar(char t[]) { strcpy(topic,t);
charges=5000; }
seminar(int c) { strcpy(topic,"Registration with Discount");
charges=5000-c; }
void regis(char t[],int c) { strcpy(topic,t);
charges=charges+c; }
void regis(int c=2000) { charges=charges+c; }
void subject(char t[],int c) { strcpy(topic,t);
charges=charges+c; }
void show() { cout<<topic<<"@"<<charges<<endl; } };
void main()
{ seminar s1,s2(1000),s3("Genetic Mutation"),s4;
s1.show();
s2.show();
s1.subject("ICT",2000);
s1.show();
s2.regis("Cyber Crime",2500);
s2.show();
s3.regis();
s3.show();
s4=s2;
s4.show();
getch(); }
Theory question (2 marks)

1. What do you mean by static data members of a class? Explain the characteristics of a static data member.
2. What is Data hiding? Explain with example
3. How is the working of inline function different from ordinary function?
4. Write any two differences between OOPs and Procedural programming?
5. Illustrate the concept of function overloading with example?
6. What is the difference between Global Variable and Local Variable? Also, give suitable C++ code to
illustrate both.
7. What is the significance of constructor and Destructor? Explain with example
8. What is copy constructor? How it is different from parameterized constructor? explain
9. What is temporary instance? explain with example
10. What is default constructor? Why constructor with default parameter is also called default constructor?
Explain
11. What are the different ways of calling parameterized constructor?
12. Why argument is always passed as reference to copy constructor? Write any two conditions when copy
constructor can be called?

You might also like