Вы находитесь на странице: 1из 58

QUESTION PAPER 2015

(a) What is a copy constructor? Give a suitable example in C++ to illustrate with
its definition within a class and a declaration of an object with the help of it.

(b) Observe the following C++ code and answer the questions (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?
(c) Write the definition of a class Photo in C++ with following description:
Private Members
 Pno //Data member for Photo Number(an integer)
 Category // Data member for Photo Category(a string)
 Exhibit // Data member for Exhibition Gallery(a string)
 FixExhibit // A member function to assign exhibition Gallery as per Category as
shown in the following table

Public Members-
 Register() // A function to allow user to enter values
//Pno, Category and call FixExhibit() function
 ViewAll() //A function to display all the data members
(d) Answer the questions (i) to (iv) based on the following:
class Interior
{
int OrderId;
char Address[20];
protected:
float Advance;
public:
Interior();
void Book();
void View();
};
class Painting: public Interior
{
int WallArea, ColorCode;
protected:
char Type;
public:
Painting();
void PBook();
void PView();
};
class Billing: public Painting
{
float Charges;
void Calculate();
public:
Billing();
void Bill();
void BillPrint();
};

(i) Which type of Inheritance out of the following is illustrated in the above
example?
a. Single Level Inheritance
b. Multi Level Inheritance
c. Multiple inheritance

(ii) Write the names of all the data members, which are directly accessible from
the member functions of class Painting.

(iii) Write the names of all the member functions, which are directly accessible
from an object of class Billing.

(iv) What will be the order of execution of the constructors, when an object of
class Billing is declared?

[2+1+1+4+4=12]
Answer
(a) Copy constructor copies the value of one object into other object.
Example-

(b)
(ii)
void main()
{
Passenger P;
P.Book(45,”CPP”); //Line 1
P.Print(); //Line 2
}//Ends here
(ii) Function 4 will be executed at }//Ends here. The ~Passenger() is called the
destructor function will is used to de-allocate the memory allocated by the constructor. It
is called automatically at the end of the program.
(c)
(d)
(i) Multi Level inheritance
(ii) float Advance, int WallArea, ColorCode,Type
(iii) void Book(), void View(), void PBook(), void
PView(), void Bill(), void BillPrint()
(iv)
Interior()
Painting()
Billing()

QUESTION PAPER 2014


(a) Write 4 characteristics of a constructor function used in a class.
(b) Answer the questions (i) and (ii) after going through the following class:
class Health
{
int PID, Did;
public: Health(int PPId); //Function 1
Health(); //Function 2
Health(Health &H); //Function 3
void Entry(); //Function 4
void Display(); //Function 5
};
void main()
{
Health H(20); //Statement 1
}

i) Which of the function out of Function 1,2,3,4 or 5 will get executed when the
Statement 1 is executed in the above code?
ii) Write a statement to declare a new object G with reference to already existing
object H using Function 3.

(c) Define a class CABS in C++ with the following specification:


Data Members
• CNo-to store Cab No
• Type- to store a character ‘A’, ‘B’ or ‘C’ as City Type
• PKM- to store per Kilo Meter charges
• Dist- to store Distance travelled ( in KM)

Member Functions
• A constructor function to initialize Type as ‘A’ and CNo as ‘1111’
• A function Charges() to assign PKM as per the following table:

Type PKM

A 25

B 20

C 15



 A function Register() to allow administrator to enter the values for CNO and Type.
Also, this function should call Charges() to assign PKM charges.

• A function ShowCab() to allow user to enter the value of Distance and display
CNo, Type, PKM, PKM*Distance(as Amount) on screen.

(d) Consider the following C++ code and answer the questions from (i) to (iv):

Class Campus
{
long Id;
char City[20];
protected:
char Country[20];
public:
Campus();
void Register();
void Display();
};
class Dept: private Campus
{
long DCode[10];
char HOD[20];
protected :
double Budget;
public:
Dept();
void Enter();
void Show();
}
class Applicant: public Dept
{
long RegNo;
char Name[20];
public:
Applicant();
void Enroll();
void View();
};

1) Which type of Inheritance is shown in the above example?


2) Write the names of those member functions, which are directly accessed from the
objects of class Applicant.
3) Write the names of those data members, which can be directly accessible from the
member functions of class Applicant.
4) Is it possible to directly call function Display() of class University from an object of
class Dept? (Answer as Yes/No).
[2+2+4+4=12]
Marks:12
Answer:
Ans 2 (a) Four characteristics of a constructor function used in a class are-
1. A constructor function has the same name as that of a class name.
2. A construction function never returns any value, not even void.
3. Constructor function is used to initialize class elements with some legal
initial values.
4. Default constructor is called automatically as the object of that class is
created.

(b)
(i) The Function 1 would be called when Statement 1 would be executed.
(ii)
void main()
{
Health H(20);
Health G(H); // Object G
}

(c)
#include < iostream.h >
#include < conio.h >
class CABS
{
int CNo;
char Type;
int PKM;
int Dist;
public:
CABS()
{
CNo=1111;
Type='A';
}
int Charges()
{
if(Type=='A')
return 25;
else if(Type=='B')
return 20;
else if(Type=='C')
return 15;
else
return 0;
}
void Register()
{
cout << "nEnter Cab No:";
cin >>CNo;
cout <<"nEnter City Type(A/B/C): ";
cin >>Type;
PKM=Charges();
}
void ShowCab()
{
cout << "nEnter the value of distance (15/20/25): ";
cin >> Dist;
cout<< "nThe Cab No is: "<< CNo;
cout<< "nThe City type is: "<< Type;
cout<< "nThe charges per kilo meters is: "<< PKM;
cout<< "nThe Total Amount is: "<< PKM*Dist;
}
};
void main()
{
clrscr();
CABS C;
C.Register();
C.ShowCab();
getch();
}

(d)
(i) Multilevel Inheritance
(ii) Member Functions which are directly accessible from the objects of class Applicant
are-
void Enter(), void Show(), void Enroll(), void View()
(iiii) Data members which are directly accessible from the member functions of class
Applicant are-
double Budget, long RegNo, char Name[20]
(iv) No

QUESTION PAPER 2013


(a) Write any two similarities between constructors and destructors. Write the
function headers for the constructors and the destructors of a class Flight.
(b) Answer the questions (i) and (ii) after going through the following class:
class Race
{
int CarNo, Track;
public:
Race(); //Function 1
Race(int CN); //Function 2
Race(Race &R); //Function 3
void Register(); //Function 4
void Drive(); //Function 5
}

void main()
{
Race R;
:
}
(i ) Out of the following, which of the options is correct for calling Function2?

(ii) Name the feature of object oriented programming that is illustrated by the
Function 1, Function 2 and Function 3, combined together.

(C) Define a class Bus with the following specifications:

Data members
• Busno - to store Bus No.
• From - to store Place name of origin
• To - to store Place name of destination
• Type - to store the Distance in Kilometres
• Fare - to store the Bus Fare

Member functions
• A constructor function to initialise Type as ‘O’ and Freight as 500

• A function CalcFare() as calculate Fare as per the following criteria:


Type Fare

‘O’ 15*Distance

‘E’ 20*Distance

‘L’ 24*Distance

• A function Allocate() to allow the user to enter the values for Busno, From, To,
Type and Distance. Also, this function should call CalcFare() to calculate the
Fare.

• A function Show() to display the content of all the data members on screen.

(d) Consider the following C++ code and answer the questions from (i) to (iv):
class Personal
{
int Class, Rno;
char Section;
protected:
char Name[20];
public:
Personal();
void Pentry();
void Pdisplay();
};
class Marks: private Personal
{
float M[5];
protected:
char Grade[5];

public:
Marks();
void Mentry();
void Mdisplay();
};

class Result:public Marks


{
float Total, Avg;
public:
char FinalGrade, Comments[20];
Result();
void Rcalculate();
void Rdisplay();
};
(i) Which type of inheritance is shown in the above example?

(ii) Write the names of those data members, which can be directly accessed from
the objects of class Result.

(iii) Write the names of those member functions, which can be directly accessed
from the objects of class Result.

(iv) Write the names of those data members, which can be directly accessed from
the Mentry() function of class Marks.

[2+2+4+4 = 12]

Marks:12
Answer:
(a) The two similarities between the constructor and destructor
are as follows:
1. . Both have the same name as that of the class name.
2. . Both never return any value.

Function header for class Flight:

class Flight
{
:
Flight()
{
cout << ”Constructor.”;
}
~Flight()
{
cout << ”Desctructor.”;
}
};

(b)
(i) Option 1 – Race T(30);

(ii) The feature ‘Polymorphism’ of object oriented programming is


illustrated by the Function 1, Function 2 and Function 3 combined
together.

The function 1, Function 2 and Functions 3 are the constructor


functions having different types of argument. Therefore, they are
the illustrating concept of the ‘Constructor Overloading’ together.

(c)

#include< iostream.h >

#include< conio.h >

class Bus

int busno;

char from[20];

char to[20];
char type;

int distance;

int fare;

public:

Bus()

type='E';

int CalcFare();

public:

void Allocate()

cout << "nEnter Busno:";

cin >> busno;

cout <<"nEnter Origin: ";

cin >> from;

cout << "nEnter Bus Type: ";

cin >> type;

cout << "nEnter Destination: ";


cin >> to;

cout << "nEnter Distance: ";

cin >> distance;

fare=CalcFare();

void Show()

cout << "n Carno: "<< busno;

cout << "n Origin: "<< from;

cout << "n Destination: "<< to;

cout << "n Car Type: "<< type;

cout << "n Distance: "<< distance;

cout << "n Charge: "<< fare;

};

int Bus::CalcFare()

if((type=='O')||(type=='o'))
return 15*distance;

if((type=='E')||(type=='e'))

return 20*distance;

if((type=='L')||(type=='l'))

return 24*distance;

void main()

clrscr();

Bus B;

B.Allocate();

B.Show();

getch();

}
(d)
(i) Multilevel inheritance
(ii) Data members: FinalGrade, Comments
(iii) Member functions: Rcalculate(), Rdisplay(), Mentry(),
Mdisplay()
(iv) Data members: M, Grade, Name
QUESTION PAPER 2012
) What is the difference between the members in
private visibility mode and the members in protected visibility mode inside a
class? Also, give a suitable C++ code to illustrate both.

b) Answer the questions (i) and (ii) after going through the following class:
class Travel
{
int PlaceCode;
char Place[20];
float Charges;
public:
Travel() //Function 1
{
PlaceCode=1;
strcpy(Place,”DELHI”);
Charges=1000;
}
void TravelPlan(float C) //Function2
{
cout<< PlaceCode<< ”:”<< Place<< ”:”<< Charges << endl;
}
~Travel() //Function3
{
cout<<”Travel Plan Cancelled”<< endl;
}
Travel(int PC, char P[ ],float C) //Function4
{
PlaceCode=PC;
strcpy(Place,P);
Charges=C;
}
};
(i) In Object Oriented Programming, what are Function 1 and Function 4
combined together referred as?
(ii) In Object Oriented Programming, which concept is illustrated by
Function 3? When is this function called /invoked?
c) Define a class RESTRA in C++ with the following description:
Private Members:

FoodCode of type int


Food of type string
FType of type string
Sticker of type string
A member function GetSticker() to assign the
following values for Sticker as per the given FType:

FType Sticker
Vegetarian GREEN
Contains Egg YELLOW
Non-Vegetarian RED
Public Members:
A function GetFood() to allow the user to enter values for
FoodCode, Food, FType and call function GetSticker() to
assign Sticker.
A function ShowFood() to allow user to view the content of
all the data members.

d) Answer the questions (i) to (iv) based on the following:


class COMPANY
{
char Location[20];
double Budget, Income;
protected:
void Accounts();
public:
COMPANY();
void Register();
void Show();
};
class FACTORY: public COMPANY
{
char Location[20];
int Workers;
protected:
double Salary;
void Computer();
public:
FACTORY();
void Enter();
void Show();
};
Class SHOP: private COMPANY
{
char Location[20];
float Area;
double Sale;
public:
SHOP();
void Input();
void Output();
};
(i) Name the type of inheritance illustrated in the above C++ code.
(ii) Write the name of the data members, which are accessible from member
functions of class SHOP.
(iii)Write the names of all the member functions, which are accessible from
objects belonging to class FACTORY.
(iv)Write the names of all the members, which are accessible from objects
of class SHOP.
[2+2+4+4 = 12]
Marks:12
View Answer

 Q3
a) Write a function SWAP2BEST (int ARR[ ],int Size) in C++ to modify the
content of the array in such a way that the elements, which are multiples of
10 swap with the values present in the very next position in the array.

For example:
If the content of Array ARR is
90, 56, 45, 20, 34, 54
The content of array ARR should become
56, 90, 45, 34, 20, 54
b) Anarray T[20][10] is stored in the memory along the
column with each of the elements occupying 2 bytes.
Find out the memory location of T[10][5], if the
element[2][9] is stored at the location 7600.
c) Write a function in C++ to perform Insert operation in a static circular
Queue containing Book’s information (represented with the help of an array
of structure BOOK).
struct BOOK
{
long Accno; //Book Accession Number
char Title[20]; //Book Title
};
d) Write a function ALTERNATE (int A[ ][3], int N, int M) in C++ to display all
alternate elements from two-dimensional array A (starting from A[0][0]).
For example:
If the array is containing:
23 54 76
37 19 28
62 13 19
The output will be:
23 76 19 62 19
e) Evaluate the following POSTFIX notation. Show the status of stack after
every step of evaluation (i.e. after each operator):True, False, NOT, AND,
False, True, OR, AND
[3+3+4+2+2 = 14]
Marks:14
Answer:
(a). A C++ function SWAP2BEST(int ARR[], int Size) that
swaps the elements which are multiple of 10, with their very
next positioned element.
void SWAP2BEST (int ARR[], int Size)
{
int temp;
for(int i = 0; i < SIZE; i++)
{
if(ARR[i]%10 == 0)
{ //swap with the next element
temp = ARR[i];
ARR[i] = ARR[i+1];
ARR[i+1] = temp;
i++; //reach next to the number that has been
swapped
}
}
}

(b). B= ?
E=2 Bytes
Lr=Lc=0
S[2][9]=7600
Column Major:
S[I][J]=B+E[(I-Lr)+R(J-Lc)]
S[2][9] = B + 2[(2-0)+20(9-0)]
7600 = B + 2(182)
B = 7600-364
B = 7236
Now Address of S[5][10] is:
S[10][5] = B+ E[(I-Lr)+R(J-Lc)]
= 7236 + 2[(10-0] + 20(5-0)]
= 7236 + 2(110)
= 7236 + 220
= 7456 Ans
(c). // declare a global variables

Const int size=10;


int front,rear;
front=rear=-1;
Book B[size];

void insert_in_CQ( )
{
if(front==0 && rear==size-1)||(front==rear+1))
cout<< ”Underflow”;
else if(rear==-1)
front=rear=0;
else
{
rear++;
cout<< ”Enter Accession No:”;
cin>>B[rear].acc;
cout<< ”Enter Title: ”;
cin>>B[rear].Title;
}
}

(d). Function ALTERNATE(int A[][3], int N, int M) that displays


all alternate elements from 2Dimensional array A.

void ALTERNATE(int A[ ][3 ], int N, int M)


{

for(int i = 0; i < N*M; i = i+2)


{
cout<< A[i/M][i%M]<< "n";
}

(e). The Given postfix notation:


True, False, NOT, AND, False, True, OR, AND

Push each element in the stack bucket one by one.

Therefore the Answer is True.

QUESTION PAPER 2011


a) Differentiate between Constructor and Destructor function with respect to
Object Oriented Programming.

b) Write the output of the following C++ code. Also write the name of feature of
Object Oriented Programming used in the following program jointly illustrated by
the function [I] to [IV] :
#include< iostream.h >

void Line() //Function [I]

{
for( int L=1 ; L <= 80; L++) cout << ”-“;

void Line(int N) //Function[II]

for( int L = 1; L<= N; L++) cout << ”*”;

cout << endl;

void Line(char C, int N) //Function[III]

for(int L = 1; L <= N; L++) cout << C;

cout << endl;

void Line(int M, int N) //Function[IV]

for( int L =1 ; L <=N ; L++) cout << M*L;

cout << endl;

void main()

int A=9, B=4, C=3;

char K = '#';

Line (K,B);

Line (A,C);
}
c) Define a class Applicant in C++ with following
description:
Private Members

A data member ANo(Admission Number) of type long


A data member Name of Type string
A data member Agg(Aggregate Marks) of type float
A data member Grade of type char
A member function GradeMe() to find the Grade as per the
Aggregate Marks range and the respective Grades are shown as follows:
Aggregate Marks Grade
>=80 A
less than 80 and >=65 B
less than 65 and >=50 C
less than 50 D
Public Members

A function ENTER() to allow user to enter values for ANo,Name,Agg &
call function GradeMe() to find the Grade.
A function RESULT() to allow user to view the content of all the data
members.
d) Answer the questions (i)to (iv) based on the following:
class Student
{
int Rollno;
char SName[20];
float Marks1;
protected;
void Result();
public :
Student();
void Enroll();
void Display();
};
class Teacher
{
long TCode;
char TName[20];
protected:
Teacher();
void Enter();
void Show();
};
class Course : public Student, private Teacher
{
long CCode[10]; char CourseName[50];
char StartDate[8], endDate[8];
public :
Course();
void Commence();
void CDetail();
};

(i)Write the names of member functions, which are accessible from objects of
class Course.
(ii) Write the names of all the data members, which is/are accessible from member
function Commence of class Course.
(iii)Write the names of all the members, which are accessible from objects of
class Teacher.
(iv)Which type of Inheritance is illustrated in the above C++ code?
[ 2+2+4+4 = 12 ]
Marks:12
Answer:
(a) Constructor is a member function whose task is to initialize the objects
of its class whereas destructors are used to deallocate memory and do
other clean up tasks for a class object and its class members when the
object is destroyed.

(b) Output is -

####

91827

OOPs feature -
Polymorphism

(c)

#include< iostream.h >


#include< conio.h >
class Applicant
{
public:

long ANo;
char Name[25];
float Agg;
char Grade;

public:

void ENTER()
{
cout << "Enter Admission No :";
cin >> ANo;

cout << "Enter Name :";


cin >> Name;

cout << "Enter Aggregate Marks :";


cin >> Agg;

}
void RESULT()
{
cout << "The details of the candidate are";
cout << "Admission No: "<< ANo<<"n";
cout << "Name is : "<< Name<<"n";
cout << "Aggregate Marks: "<< Agg<<"n";
cout<<"Grade is: " << Grade;
}
void GradeMe()
{

if( Agg >= 80)


Grade = 'A';

else if( Agg < 80 && Agg >= 65)


Grade='B';

else if(Agg < 65 && Agg >= 50)


Grade=' C';

else
Grade='D';
}
};
void main()
{

Applicant d;
clrscr();

d.ENTER();
d.GradeMe();
d.RESULT();
getch();

}
(d)

(i) The member functions accessible by the object of class Course are:
Enroll(), Display(), Commerce() and CDetail()

(ii) The data members accessible are—Ccode[],CourseName[], StartDate[],


EndDate[]
(iii) Member Functions –
Enter() and Show()

(iv) Multiple inheritance


QUESTION PAPER 2010
a) What do you understand by Data Encapsulation and Data Hiding? Also, give an
example in C++ to illustrate both. (2)
b) Answer the questions (i) and (ii) after going through the following
class: (2)
class Exam
{ int Rno, MaxMarks, MinMarks, Marks;
public:
Exam()
{ //Module 1
Rno=101;
MaxMarks=100;
MinMarks=40;
Marks=75;
}
Exam(int Prno, int Pmarks)
{ //Module 2
Rno=Prno; MaxMarks=100; MinMarks=40; Marks=Pmarks;
}
~ Exam()
{ //Module 3
cout << ”Exam Over” << endl;
}
void Show()
{ //Module 4
cout << Rno << ”:” << MaxMarks << ”:” << MinMarks << endl;
cout << ” [ Marks Got ] ” << Marks << endl;
}
};
i) As per Object Oriented Programming, which concept is illustrated by
Module 1 and Module2 together?
ii) What is the module 3 specifically referred as? When do you think module
3 will be invoked/ called?
c) Define a class STOCK in C++ with following
description: (4)
Private Members

ICode of type integer (Item Code)


Item of type string (Item Name)
Price of type float (Price of each item)
Qty of type integer (Quantity in stock)
Discount of type float (Discount percentage on the item)
A member function FindDisc() to calculate discount percentage
as per the following rule:

If Qty < =50 Discount is 0


If 50 < Qty < =100 Discount is 5
If Qty > 100 Discount is 10
Public Members

A function Buy () to allow user to enter values for ICode, Item, Price,
Qty and call function. Find Disc() to calculate Discount.
A Function ShowAll() to allow user to view the content of all the data
members.

d) Answer the questions (i) to (iv) based on the


following: (4)
class Director
{ // Director Identification Number
long DID;
char Name [ 20 ] ;
protected:
char Description [ 40 ] ;
void Allocate();
public:
Director();
void Assign();
void Show();
};
class Factory: public Director
{
int FID; // Factory ID
char Address [ 20 ] ;
protected:
int NOE; // No. of Employees
public:
Factory();
void Input();
void Output();
};
class ShowRoom: private Factory
{
int SID;
char City [ 20 ] ;
public:
ShowRoom ();
void Enter();
void Display ();
};
i) Which type of inheritance out of the following is illustrated in the above C++
code?
a) Single Level Inheritance
b) Multi Level Inheritance
c) Multiple Level Inheritance
ii) Write the name of the data members which are accessible by objects of class
type ShowRoom.
iii) Write the name of all member functions which are accessible by objects of
class type ShowRoom.
iv) Write the name of all members which are accessible from member functions of
class Factory.
Marks:12
Answer:
a) Data Encapsulation: Wrapping up of a data and functions
together in a single unit is known as Data Encapsulation. In a
class, we wrap up the data and functions together in a single unit.
Data Hiding: Keeping the data in private/protected visibility mode
of the class to prevent it from accidental change is known as Data
Hiding.
For example:
class Student
{
int Data;
public:
char stud_name [ 20 ] ;
char address [ 20 ] ;
void input_data(void);
};

b) i) Polymorphism
or
Function Overloading
or
Constructor Overloading
ii) Destructor, invoked or called when scope of an Object gets
over.
c)
The class is as:
class STOCK
{
int ICode;
char Item [ 20 ] ;
float Price;a) What do you understand by Data Encapsulation and Data Hiding?
Also, give an example in C++ to illustrate
both. (2)

b) Answer the questions (i) and (ii) after going through the following
class: (2)
class Exam
{ int Rno, MaxMarks, MinMarks, Marks;
public:
Exam()
{ //Module 1
Rno=101;
MaxMarks=100;
MinMarks=40;
Marks=75;
}
Exam(int Prno, int Pmarks)
{ //Module 2
Rno=Prno; MaxMarks=100; MinMarks=40; Marks=Pmarks;
}
~ Exam()
{ //Module 3
cout << ”Exam Over” << endl;
}
void Show()
{ //Module 4
cout << Rno << ”:” << MaxMarks << ”:” << MinMarks << endl;
cout << ” [ Marks Got ] ” << Marks << endl;
}
};
i) As per Object Oriented Programming, which concept is illustrated by
Module 1 and Module2 together?
ii) What is the module 3 specifically referred as? When do you think module
3 will be invoked/ called?
c) Define a class STOCK in C++ with following
description: (4)
Private Members

ICode of type integer (Item Code)


Item of type string (Item Name)
Price of type float (Price of each item)
Qty of type integer (Quantity in stock)
Discount of type float (Discount percentage on the item)
A member function FindDisc() to calculate discount percentage
as per the following rule:

If Qty < =50 Discount is 0


If 50 < Qty < =100 Discount is 5
If Qty > 100 Discount is 10
Public Members

A function Buy () to allow user to enter values for ICode, Item, Price,
Qty and call function. Find Disc() to calculate Discount.
A Function ShowAll() to allow user to view the content of all the data
members.

d) Answer the questions (i) to (iv) based on the


following: (4)
class Director
{ // Director Identification Number
long DID;
char Name [ 20 ] ;
protected:
char Description [ 40 ] ;
void Allocate();
public:
Director();
void Assign();
void Show();
};
class Factory: public Director
{
int FID; // Factory ID
char Address [ 20 ] ;
protected:
int NOE; // No. of Employees
public:
Factory();
void Input();
void Output();
};
class ShowRoom: private Factory
{
int SID;
char City [ 20 ] ;
public:
ShowRoom ();
void Enter();
void Display ();
};
i) Which type of inheritance out of the following is illustrated in the above C++
code?
a) Single Level Inheritance
b) Multi Level Inheritance
c) Multiple Level Inheritance
ii) Write the name of the data members which are accessible by objects of class
type ShowRoom.
iii) Write the name of all member functions which are accessible by objects of
class type ShowRoom.
iv) Write the name of all members which are accessible from member functions of
class Factory.
Marks:12
Answer:
a) Data Encapsulation: Wrapping up of a data and functions
together in a single unit is known as Data Encapsulation. In a
class, we wrap up the data and functions together in a single unit.
Data Hiding: Keeping the data in private/protected visibility mode
of the class to prevent it from accidental change is known as Data
Hiding.
For example:
class Student
{
int Data;
public:
char stud_name [ 20 ] ;
char address [ 20 ] ;
void input_data(void);
};

b) i) Polymorphism
or
Function Overloading
or
Constructor Overloading
ii) Destructor, invoked or called when scope of an Object gets
over.
c)
The class is as:
class STOCK
{
int ICode;
char Item [ 20 ] ;
float Price;
int Qty;
float Discount;
void FindDisc();
public:
void Buy()
{
cin >> ICode;
gets(Iyrm);
cin >> Price >> Qty;
GetDisc();
}
void ShowAll()
{
cout << ICode << Item << Price<< Qty << Discount;
}
};
void STOCK::FindDisc()
{
if (Qty < =50)
Discount=0;
else if (Qty < =100)
Discount=5;
else
Discount=10;
}
d)
i. (b) Multi Level Inheritance
ii. None
iii. Enter(), Display()
FID, Address, NOE, Description, Input(), Output(), Assign(),
show(), Allocate()

int Qty;
float Discount;
void FindDisc();
public:
void Buy()
{
cin >> ICode;
gets(Iyrm);
cin >> Price >> Qty;
GetDisc();
}
void ShowAll()
{
cout << ICode << Item << Price<< Qty << Discount;
}
};
void STOCK::FindDisc()
{
if (Qty < =50)
Discount=0;
else if (Qty < =100)
Discount=5;
else
Discount=10;
}
d)
i. (b) Multi Level Inheritance
ii. None
iii. Enter(), Display()
FID, Address, NOE, Description, Input(), Output(), Assign(),
show(), Allocate()
QUESTION PAPER 2009
(a) What is function overloading? Give an example in C++ to illustrate function
overloading.
[2]

(b) Answer the question (i) and (ii) after going through the
following [2]
class Job
{
int JobId; char JobType;
public:
~Job() //Function 1
{ cout<< ”Resigned” << endl; }
Job() // Function 2
{ JobId=10; JobType=’ T’ ;}
void TellMe() //Function 3
{ cout << JobId<< ” : ” << JobType << endl;}
Job (Job &J)
{
JobId = J.JobId+10;JobType=J.JobType+1;
}
};
i) Which member function out of Function 1, Function 2, Function 3 and Function
4 shown in the above definition of class Job 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) Job P; //Line 1
Job Q (P); //Line 2

Which member function out of Function 1, Function 2, Function 3 and Function 4


shown in the above definition of class Job will be called on execution of
statement written as Line 2? What is this function specifically known as out of
Destructor or Copy Constructor or Default constructor?
(c) Define a class HOTEL in C++ with the following description: [4 ]
Private Members:
Rno // Data member to store Room No
Name //Data member to store customer name
Tariff // Data member to store per day charges
NOD // Data member to store number of days of stay
CALC () // A function to calculate and return Amount as NOD*Tariff and if the
value of NOD*Tariff is more than 10000 then as 1.05*NOD*Tarif
Public Members
Checkin () // A function to enter the content Rno, Name, Tariff and NOD
Checkout () // A function to display Rno, Name, Tariff, NOD and Amount (Amount
to
// be displayed by calling function CALC ())
(d) Answer the questions (i) to (iv) based on the following: [4]

class Regular
{
char SchoolCode [10];
public:
void InRegular ( );
void OutRegular ( ) ;
};
class Distance
{
char StudyCentreCode[5];
public:
void InDistance ( ) ;
void OutDistance ( ) ;
};
class Course: public Regular, private Distance
{
char Code [5];
float Fees ;
int Duration;
public:
void InCourse ( ) ;
void OutCourse ( ) ;
};
(i) Which type of Inheritance in shown in the above example?
(ii) Write names of all the member functions accessible from OutCourse function
of class Course.
(iii) Write name of all the members accessible through an object of class Course.
(iv)Is the function InRegular() accessible inside the function InDistance()? Justify
your answer.
Marks:12
Answer: (a) A function name having several definitions that are differentiable by the
number or types of their arguments, is known as overloaded function and this process is
known as function overloading.
Example :
# include < iostream.h>
void display ( int i)
{ cout << “ Here integer is” << i << endl; }
void display (double j)
{ cout << “ Here float is” << j << endl; }
void display (char *k)
{ cout << “ Here character* is” << k << endl; }
int main ()
{
display (20);
display (20.1010);
display ( “name”);
}
It will give output as
Here, integer is 10.
Here, float is 20.10 .
Here, character* is name.

(b) (i) Function 1 gets called automatically whenever the scope of an object finishes. It
is called the destructor of a class. Destructors are usually used to deallocate memory
and do other cleanup for a class object and its class members when the object is
destroyed. A destructor is called for a class object when that object passes out of scope
or is explicitly deleted.

(ii) Function 4 that is Job (Job & J) gets invoked when line 2 is executed. It is known as
the copy constructor. There are 3 important places where a copy constructor is called:
 When an object is created from another object of the same type.
 When an object is passed by value as a parameter to a function.
 When an object is returned from a function.

(c) class HOTEL


{
private :
int Rno, NOD, Amount;
char Name;
float Tariff;
void CALC() ;
public :
void Checkin();
void Checkout();
};
void HOTEL :: CALC()
{
Amount = NOD * Tariff ;
if (Amount > 10,000)
{
Amount = 1.05 * Amount;
}
}
void HOTEL :: Checkin()
{
cout << “Enter the Room no : “ ;
cin >> Rno ;
cout << “ Enter the customer name : “;
gets (Name );
cout << “Enter the Tariff : “;
cin >> Tariff ;
cout << “ Enter the number of days of stay : “;
cin >> NOD;
CALC();
}
void HOTEL :: Checkout ()
{
cout << “Name : “ << Name << endl ;
cout << “Room number : “ << Rno << endl;
cout << “The tariff : “ << Tariff < cout << “ The number of days of stay are “ << NOD <<
endl ;
cout <<“ The amount is : “ << Amount << endl;
}
d(i) Multiple inheritance is shown in the given example.
(ii) The member functions accessible in outcourse function of class course are:
InRegular(), OutRegular() , InDistance(), OutDistance() .
This is because class Regular is publicly derived in class Course, so all member
functions of class Regular are accessible in class Course and class Distance is privately
derived in class Course, so all public members of class Regular becomes private
members of class Course.
(iii) The object of class Course can access InCourse(), OutCourse(), InRegular(),
OutRegular() functions.
(iv) The function InRegular() is not accessible inside the function InDistance() as class
Course derives the class Regular publicly and class Distance privately. So, class
Course can access the member functions of class Regular and class Distance. But, the
member functions of class Distance cannot access the member functions of class
Regular as these are two independent classes.

QUESTION PAPER 2008


(a) Differentiate between public and private visibility modes in context of object oriented
programming using a suitable example illustrating each. [2]
(b) Answer the question (i) and (ii) after going through the following code: [2]
#include< iostream.h >
#include< string.h >
class Bazar
{
char Type[20];
char Product[20];
int Qty;
float Price;
Bazar() //Function1
{
strcpy (Type, "Electronic");
strcpy(Product, "Calculator");
Qty=10;
Price=225;
}
public:
void Disp()
{
cout << Type << "-" << Product << ":" << Qty << "@" << Price << endl;
}
};
void main()
{
Bazar B;
B.Disp();
}
(i) Will Statement 1 initialize all the data members for object B with the values given
in the Function 1? 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?

(c) Define a class Garments in C++ with the following descriptions:


Private members [4]
GCode of type string
GType of type string
GSize of type integer
GFabric of type string
GPrice of type float
A function Assign() which calculates and assigns the value of GPrice as follows
For the value of GFabric as “COTTON”,
GType GPrice(Rs)
TROUSER 1300
SHIRT 1100
For GFabric other than “COTTON” the above-mentioned GPrice gets reduced by 10%.
Public members:
A constructor to assign initial values of the data members GCode, GType and GFabric
with the word “NOT ALLOWED” and GSize and GPrice with 0.
A function Input() to input the values of the data members GCode, GType, GSize and
GFabric and invoke the Assign() function.
A function Display() which displays the content of all the data members for a Garment.

(d) Answer the questions (i) to (iv) based on the following code: [4]
#include< iostream.h >
#include< conio.h >
class Dolls
{
char DCode[5];
protected:
float Price;
void CalcPrice(float);
public:
Dolls();
void DInput();
void DShow();
};
class SoftDolls : public Dolls
{
char SDName[20];
float Weight;
public:
SoftDolls();
void SDInput();
void SDShow();
};
class ElectronicDolls : public Dolls
{
char EDName[20];
char BatterType[10];
int Batteries;
public:
ElectronicDolls();
void EDInput();
void EDShow();
};

(i) Which type of inheritance is shown in the above example?


(ii) How many bytes an object of the class ElectronicDolls will require?
(iii) Write name of all the data members accessible from member functions of
the class SoftDolls.
(iv) Write name of all the data member functions accessible by an object of the
class ElectronicDolls.

Marks:12
Answer:
(a) Private variables are variables that are visible only to the class to which they
belong. The derived classes cannot inherit the private data members and
member functions of the base class.

Public variables are variables that are visible to all classes. The public
members (variables or functions) can even be accessed by the code that is
outside the class.

E.g.,
class Base
{
public : int x ;
private: int y;
};
class Pub: public Base
{
Pub( );
{
x=1; // valid as anybody can access a public member
y=2; // invalid since it is a private member

}
};
(b) (i) Statement 1 will not get executed. The compiler will return an error that
Bazar::Bazar(); is not accessible since by default, the class members are private.
So, main() being outside the code of the class will not be able to access the
private members of Bazar.
(ii) When the following correction will be made, then the output will be:
Electronic_Calculator : 10@225.

(c)
class Garments
{
char GCode[20];
char GType[20];
int GSize;
char GFabric[20];
float GPrice;
void Assign()
{
if(strcmp(GType, “TROUSER”)==0)
GPrice=1300;
else if (strcmp(GType, “SHIRT”)==0)
GPrice=1100;
if(strcmp(GType, “COTTON”)!=0)
Gpice=GPrice- (GPrice * .10);
}
public:
void Garments()
{
GCode= ”NOT ALLOWED”;
GType= ”NOT ALLOWED”;
GFabric=”NOT ALLOWED”;
GSize=0;
GPrice=0;
}
void Input()
{
cout << “n Enter code:”;
cin >> GCode;
cout >> “n Enter type:”;
cin >> GType;
cout<< “n Enter fabric:”;
cin >> GFabric;
cout << “n Enter size:”;
cin >> GSize;
Assign();
}
void Display()
{
cout << “Code:” << GCode;
cout << “n Type:” << GType;
cout << “n Size:” << GSize;
cout << “n Fabric:” << GFabric;
cout << “n Price:”<< GPrice;
}
};

(d) (i) The example in the given question is an implementation of hierarchical


inheritance. In the given example, two derived classes SoftDolls and
ElectronicDolls inherit the same base class Dolls.

(ii) It will require 41 bytes.

(iii) The data members accessible from member functions of the class
SoftDolls are: Price, SDName and Weight.

(iv) The data member functions accessible by an object of the class


ElectronicDolls are: Dinput(), Dshow(), EDInput() and EDShow().

QUESTION PAPER 2007


a) Differentiate between Constructor and Destructor function in context of Classes and Objects
using C++. [2]

(b) Answer the questions (i) and (ii) after going through the following code:
[2]

class Maths
{
char Chapter [20];
int Marks;
public:
Maths ( ) //Member Function 1
{
strcpy (Chapter, “Geometry”);
Marks = 10;
cout << “Chapter Initialised”;
{
~Math ( ) //Member Function 2
}
cout << ”Chapter Over”;
}
};

(i) Name the specific features of class shown by Member Function 1 and
Member Function 2 in the above example.

(ii) How would Member Function 1 and Member Function 2 get executed?

(c) Define a class Tour in C++ with the description given below : [4]
Private Members :
Tcode of type string
NoofAdults of type integer
NoofKids of type integer
Kilometres of type integer
TotalFare of type float

Public Members:

• A constructor to assign initial values as follows :


TCode with the word “NULL”
NoofAdults as 0
NoofKids as 0
Kilometres as 0
TotalFare as 0

• A function AssignFare ( ) which calculates and assigns the value of


the data member TotalFare as follows:
For each Adult
Fare(Rs) For Kilometres
500 >=1000
300 <1000 &>=500
200 <500

For each Kid the above Fare will be 50% of the Fare mentioned in
the above table

For example :
If Kilometres is 850, NoofAdults = 2 and NoofKids = 3
Then TotalFare should be calculated as

NumofAdults * 300 + NoofKids * 150


i.e. 2*300 + 3*150=1050

• A function EnterTour ( ) to input the values of the data members


TCode, NoofAdults, NoofKids and Kilometres; and invoke the
Assign Fare( ) function.

• A function ShowTour( ) which displays the content of all the data


members for a Tour.

(d) Answer the questions (i) to (iv) based on the following code : [4]
class Trainer
{
char TNo [5], TName [20], Specialisation [10];
int Days;

protected :
float Remuneration;
void AssignRem (float);

public :

Trainer ( ) ;
void TEntry ( );
void TDisplay ( );
};
class Learner
{
char Regno [10], LName [20], Program [10];

protected :
int Attendance, Grade;

public:
Learner ( );
void LEntry ( );
void LDisplay ( );
};
class Institute: public Learner, public Trainer
{
char ICode[10], IName [20];

public:
Institute ( );
void IEntry ( );
void IDisplay ( );
};

(i) Which type of Inheritance is depicted by the above example?


(ii) Identify the member function(s) that cannot be called directly from the
objects of class Institute from the following
TEntry( )
LDisplay()
IEntry()

(iii) Write name of all the member(s) accessible from member functions of
class Institute.

(iv) If class Institute was derived privately from class Learner and privately
from class Trainer, then, name the member function(s) that could be
accessed through Objects of class Institute.
Marks:12
Answer: (a) A member function with the same name as its class is called constructor
and is used to initialize the objects of that class type with a legal initial value. A
destructor is used to destroy objects created by constructor. It takes no arguments, and
no return types can be specified for it. It is preceded by tlide(˜)

(b) (i) The specific features shown by member function 1 is: Constructor and by
function 2 is: Destructor.

(ii) Whenever object of the class is created, member function 1 will be called
automatically, since a constructor is called automatically when an object is created.
Whenever the scope of the function is over, the destructor, i.e., member function 2 will
be called.
(c)
Class Tour
{
private:
char Tcode[10];
int NoofAdults;
int NoofKids;
int Kilometres;
float TotalFare;

public:
Tour()
{
strcpy (Tcode, ”NULL”);
NoofAdults=0;
NoofKids=0;
Kilometres=0;
TotalFare=0;
}
void AssignFare()
{
int Fare=0;
if Kilometres(< 500)
Fare=200;
else if ( Kilometres >=500 && < 1000)
Fare=300;
else
Fare=500;
endif ;
endif ;
if (NoofKids > 0)
TotalFare= NoofKids * Fare/2;
TotalFare = TotalFare + NoofAdults *Fare;
}

void EnterTour()
{
cout << “nEnter the details:n”;
cout << “nEnter the Member Code:” ;
cin >> Tcode;
cout << ”nEnter NoofAdults:”;
cin >> NoofAdults;
cout << ”/nEnter NoofKids:”;
cin >> NoofKids;
cout << “/nEnter the total distance:”;
cin >> Kilometres;
AssignFare();
}
void ShowTour()

{
cout << “n Contents of all the data members for a Tour:n “;
cout << “n Members code: “ << Tcode ;
cout << “n Number of Adults:” << NoofAdults;
cout << “n Number of Kids: “ << NoofKids;
cout << “n Total Distance (in Kms ):” << Kilometres;
cout << “n Total Fare: “ << Total Fare;
}
};

(d)
(i) Multiple inheritance
(ii) TEntry(), LDisplay()
(iii) TEntry(), TDisplay(), LEntry(), LDisplay(), IEntry(), IDisplay()
(iv) IEntry(), IDisplay().
QUESTION PAPER 2006
(a) Define multilevel and multiple inheritance in context of object
oriented programming. Give suitable example to illustrate the same. [2]
(b) Answer the question (i) and (ii) after going through the following
class:
class Interview
{ int month;
public:
Interview(int y){month = y;} //Constructor 1
Interview(Interview&t); //Constructor 2
};
(i) Create an object, such that it invokes constructor 1. [1]
(ii) Write complete definition for constructor 2. [1]
(c) Define a class named ADMISSION in C++ with the following
descriptions: [4]
Private members:
AD_NO integer(ranges 10 – 2000)
NAME array of characters(String)
CLASS character
FEES float
Public Members:
 Function Read_data() to read an object of ADMISSION type
 Function Display() to display the details of an object
 Function Draw-Nos() to choose 2 students randomly.
And display the details. Use random function to generate admission
nos. to match with AD_NO
(d) Answer the questions (i) to (iii) based on the following code:
class stationary
{
char Type;
char Manufacturer[10];
public:
stationary();
void Read_sta_details();
void Disp_sta_details();
};
class office : public stationary
{
int no_of_types;
float cost_of_sta;
public:
void Read_off_details();
void Disp_off_details();
};
class printer: private office
{
int no_of_users;
char delivery_date[10];
public:
void Read_pri_details();
void Disp_pri_details();
};
void main()
{ printer MyPrinter; }
(i) Mention the member names which are accessed by myprinter
declared in main() function. [1]
(ii) What is the size of MyPrinter in bytes? [1]
(iii) Mention the names of function accessible from the member
function Read_pri_details() of class printer. [2]
Marks:12
Answer:
(a) Multiple inheritance:
When one class is derived from more than one base class, it is known
as multiple inheritance.
Example:
// first base class
class employee
{
};
//second base class
class customer
{

};
// multiple inheritance
class emp_cust: public employee, public customer
{

Multilevel inheritance:
When a derived class is derived from a class, which is already derived from another
class, then it is known as multilevel inheritance.
Example:
// first base class
class employee
{

};
//derived class of base class
class customer : public employee
{

};
// derived class of derived class
class emp_cust: private customer
{

(b) (i) objInterview(2002);


(ii) Interview(interview &t)
{ month = t.month;
}
(c) class ADMISSION
{
unsigned short AD_NO.;
char NAME[20];
char CLASS[3];
float FEES;
public:
void Read_Data()
{ cout << Ënter the admission No. : (strictly with in range 10-
2000)”;
lb:
cin >> AD_N0.;
if(AD_NO <10 ||AD_NO >2000)
cout<< "nRenter no:"" ;
goto lb;
cout << ”n Enter the name:”;
gets(NAME);
cout << ”n Enter the class : “;
gets(CLASS);
cout << ”n Enter the fees:”;
cin >> FEES;
}
void display()
{
cout << ”n Admission No. :” << AD_NO.
cout << ”n Name :” << puts(NAME);
cout << ”n Class :” << puts(CLASS);
cout << ”n Fees :” << FEES
}
void Draw_Nos(int ANo1, int ANo2)
{
int ANo1, ANo2;
randomize();
ANo1 = random(1091)+10;
ANo2 = random(1091)+10;
ANo1.display();
ANo2.display();
}
};

// We have used function random (U-L+1) +L to generate random numbers between L


(Lower range) and U(Upper range). Here L=10 and U= 2000. Function Read_Data will
read the number between 10 and 2000 and store corresponding details. Function
Draw_Nos will generate two random numbers and will output the details of those two
random numbers (Assumption is that we have already read and store the details of
those two numbers in Read_Data function)//
(d) (i) Read_pri_details(), Disp_pri_details()
(ii) 29 bytes
(iii) Read_sta_details(); Disp_sta_details();
Read_off_details(); Disp_off_details().

Вам также может понравиться