Professional Documents
Culture Documents
Class Constructor Destructor PDF
Class Constructor Destructor PDF
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-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-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?