You are on page 1of 14

OBJECT ORIENTED PROGRAMMING

UT2- QUESTION BANK


PART-A
1. What is need for virtual base class?
Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that
use multiple inheritance.
When a base class is specified as a virtual base, it can act as an indirect base more than once
without duplication of its data members. A single copy of its data members is shared by all the
base classes that use it as a virtual base.
2. Differentiate multiple and multilevel inheritance?
When a class is derived from more than one base class, then it is known as multiple
inheritance.
When a class is derived from another derived class, then it is known as multilevel
inheritance.
3. How is virtual function declared in C++?
A virtual function is a member function that you expect to be redefined in derived classes.
Virtual functions ensure that the correct function is called for an object, regardless of the
expression used to make the function call.
Syntax:
virtual return_type function_name()
{
}
4. What is the difference between function template and class template?
A function template or generic function defines a general set of operations that
will be applied to various types of data. Using this mechanism, the same general procedure can be
applied to a wide range of data
template <class T>
returntype functionname(parameter list)
{
// body of function
}
A class template definition looks like a regular class definition, except it is prefixed by
the keyword template which supports usage of different data types in the entire class.
template <class T>
class classname
{
//member variable declarations of type T and other types
//member function declarations
};
5. What are the properties of pure virtual function?
A pure virtual function has no implementation in the base class. Hence the class cannot
be instantiated. No object is created for the base class.
The function can be redefined in the derived class.


6. What are the rules for virtual functions?
Virtual function must be a member of some class.
They are accessed by using object pointers.
The prototype of the base class version of a virtual function and all the derived class
versions must be identical.
The base pointer can point to any type of the derived object.
A virtual function can be a friend of another class
7. What is Inheritance? List the type of Inheritance.
The mechanism of deriving a new class from an old one is called inheritance. The old class is
referred as base class and the new one as derived class.

Types: Single Inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance,
multipath inheritance
8. Define multipath inheritance?
The process of deriving a class from other derived classes, which are derived from same base
class is known as multipath inheritance.

9. How will you overcome the ambiguity in calling the member function with same name in
base and derived class also give syntax?
Virtual functions ensure that the correct function is called for an object, regardless of the
expression used to make the function call.
Syntax:
virtual return_type function_name()
{
}

10. Write syntax of stream operator overloading?
The stream operators can be overloaded using a friend function as follows,
friend istream &operator>>(istream &in, classname &objectname);
friend ostream &operator<<(ostream &out, classname &objectname);


11. What is the difference between friend function and member function?

Member Function Friend Function
i. It can both use any member of the class
public or private
ii. It can access the data members directly by
specifying the name.
iii. It will be called using the object and dot
operator.
iv. It can access the data members of the class
in which it is a member.
It can both use any member of the class
public or private
It can access the data members by using the
objects of that class
This function will be invoked by specifying
only the function name.
It can access the data members of all the
classes in which it is declared as a friend.

12. List the operator which cannot be overloaded using friend function?
The operators that cannot be overloaded using friend function are =, (), [], -> .
13. What are the basic 3 keyword of exception handling mechanism?
try:
The program statements that we suspect that they may cause runtime errors are placed in the try
block.
throw:
If the exception occurs in the try block it is thrown to the catch block.
catch:
The catch block catches the thrown exception and handles it appropriately.
14. What are the various visibility modes used in inheritance?
Public
Private
Protected
15. Write an example for hybrid inheritance.

16. What is the prototype of a typical pure virtual function?
A pure virtual function is a function declared in a base class that has no definition relative to
the base class.
virtual return_type function_name() =0;
In such cases, the compiler requires each derived class to either define the function or
redeclare it as a pure virtual function. A class with pure virtual function is known as abstract
class. A class containing pure virtual function cannot be used to declare any objects of its
known.
17. Define exception give example.
Exceptions are run-time anomalies or unusual conditions that a program may encounter while
executing. Anomalies might include conditions such as division by zero, access to an array outside
its bounds, or running out of memory or disk.

Using Exception handling mechanism, we can detect (try) and report (throw) an error so that an
appropriate action can be taken.
18. Differentiate binary and Unary operator overloading.
Operator overloading refers to giving a special meaning to the operator using a function.
Unary operator overloading will have only one operand along with the operator.
operand operator; (or) operator operand;
Ex:
C--; ++c;
Binary operator overloading will have only two operands along with the operator.
operand1 operator operand2;
Ex:
C - B;
19. Write the syntax for calling Binary operator overloading if the function is defined as
friend.
Declaration:
friend returntype operator operatorsymbol(arg1, arg2);
Definition outside the class:
returntype operator operatorsymbol(arg1, arg2)
{
}
Function Call:
object1 operator object2;

20. Write syntax for Multiple Inheritance. Give example.
When a class is derived from more than one base class, then it is known as multiple
inheritance.

21. What is Generic program template? List its type with example.
Generic programming is an approach where generic types are used as parameters in
algorithms so that they work for a variety of suitable data types and data structures.

Types:
Function template
Class template
We can define a template for a function multiply(), that would help us create various versions
of multiply() for multiplying int, float, double type values.
22. What is virtual base class? Give syntax.
In multipath inheritance, duplication of inherited members will occur in the final derives
class which will cause ambiguity error.
When a base class is specified as a virtual base, it can act as an indirect base more than once
without duplication of its data members. A single copy of its data members is shared by all the
base classes that use it as a virtual base.
Syntax:
classs derivedclassname : virtual visibilitymode baseclassname
{
}
PART-B
1. Explain type conversion, its types with example.
If the source and destination data items are of different data types, data conversion must be done.
This includes conversions between basic (int, float, double) and user-defined types (class) or between
the user-defined data items of different types.
Conversions from one type to another type are either achieved by the use of constructors or type
conversion operator.

There are 3 different ways to handle when the data types are mismatched:
Basic type to class type
Class type to basic type
Class type to class type
i. Basic type to class type
To convert data from basic type to class type, the conversion function should be defined in the user-
defined objects class in the form of the constructor.
Syntax:
classname(basictype)
{
// steps for conversion
}
Example:
class time
{
private:
int h, m;
public:
time()
{
}
time(int t) // constructor for conversion
{
h=t/60;
m=t/%60;
}
void showdata()
{
cout<<h<<hours<<m<<minute;
}
};
void main()
{
time t;
int d=120;
cout<<\n given type: << d<<minute;
t=d; // integer d is converted to class type t//
cout<<\n converted time;
t.showdata ();
}
ii. Class to class type conversion:
Conversion from one class type to another class type can be achieved by using both constructor
and type conversion operator.
If the conversion function is defined in the source objects class, then the operator
function is used.
If the conversion function is defined in the destination objects class, then constructor is
used as the conversion function.
Syntax for operator function:
operator destinationtype()
{
// steps for conversion
}
Example:
#include <iostream.h>
#include<conio.h>
class Kilometers
{
private:
double kilometers;
public:
Kilometers()
{
}
Kilometers(double km) // parameterized constructor
{
kilometers=km;
}
void display()
{
cout << kilometers << " kilometeres";
}
double getValue()
{
return kilometers;
}
};
class Miles
{
private:
double miles;
public:
Miles()
{
}
Miles(double m)
{
miles=m;
}
void display()
{
cout << miles << " miles";
}
operator Kilometers() // operator function for conversion
{
return Kilometers(miles*1.609344);
}
Miles(Kilometers k) // constructor for conversion
{
miles = k.getValue()/1.609344;
}
};
void main(void)
{
// Converting using the conversion function
Miles m1(100);
Kilometers k1;
k1=m1; // call to operator function for conversion
m1.display();
cout << " = ";
k1.display();
cout << endl;

// Converting using the constructor
Kilometers k2 (100);
Miles m2;
m2 = k2; // call to constructor for conversion
k2.display();
cout << " = ";
m2.display();
cout << endl;
getch();
}

2. Explain run time polymorphism with example program in C++?

VIRTUAL FUNCTION
It is a function which has ability to take more than one form during run-time. It uses the
keyword virtual. It is invoked using pointers.

Syntax:
virtual return_type function_name(args if any)
{
}
Properties of virtual function:
Virtual function must be a member of some class.
They are accessed by using object pointers.
The prototype of the base class version of a virtual function and all the derived class
versions must be identical.
The base pointer can point to any type of the derived object.
Example:

#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"Display Base";
}
virtual void show()
{
cout<<"\nShow Base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\nDisplay derived";
}
void show()
{
cout<<"\nShow derived";
}
};
void main()
{
clrscr();
cout<<"VIRTUAL FUNCTION\n";
base B;
derived D;
base *bptr;
cout<<"\nbptr points to Base\n";
bptr=&B; // base class object address is assigned
bptr->display(); // call to base class function
bptr->show();
cout<<"\nbptr points to Derived\n";
bptr=&D; // derived class object address is assigned
bptr->display(); // call to derived class function
bptr->show();
getch();
}


3. Write a C++ program to add two string using Binary operator overloading.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bstring
{
char name[30];
public:
void getdata()
{
cout<<"enter the name ";
cin>>name;
}
void display()
{
cout<<name;
}
bstring operator+(bstring c2)
{
bstring t;
strcpy(t.name,name); // copies the value of name and store it in t.name
strcat(t.name,c2.name); // add the value of name and t.name and store it in t.name
return t;

}
};
int main()
{
bstring c1,c2,c3;
clrscr();
c1.getdata();
c2.getdata();
c3=c1+c2;
cout<<"first name = ";
c1.display();
cout<<endl;
cout<<"last name= ";
c2.display();
cout<<endl;
cout<<"name= ";
c3.display();
getch();
return 0;
}

4. Write a C++ program to create a single base class as student and single derived class
as mark with necessary data member and member function(calculate total and average in
derived class )

#include<iostream.h>
#include<conio.h>
class student
{
public:
int roll;
char a[20];
void getdata()
{
cout<<"\nEnter rollno::";
cin>>roll;
cout<<"\nEnter Name::";
cin>>a;
}
void show()
{
cout<<"\nRoll No::"<<roll;
cout<<"\nName::"<<a;
}
};
class mark
{
public:
int m1,m2,m3;
double tot,avg;
void getdata()
{
student::getdata( ); // call getdata function defined in student class
cout<<"\nEnter m1::";
cin>>m1;
cout<<"\nEnter m2::";
cin>>m2;
cout<<"\nEnter m3::";
cin>>m3;
}
void show()
{
student::show(); // call show function defined in student class
tot=m1+m2+m3;
avg=tot/3;
cout<<"\nM1::"<<m1;
cout<<"\nM2::"<<m2;
cout<<"\nM3::"<<m3;
cout<<"\nTotal::"<<tot;
cout<<"\nAverage::"<<avg;

}

};

void main()
{
clrscr();
cout<<"STUDENT DETAILS \n";
mark i;
i.getdata();
i.show();
getch();
}

5. Write a function template for sorting n numbers.
include<iostream.h>
#include<conio.h>
template<class T>
void bubble(T a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
T temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int main()
{
int x[5]={10,45,23,56,70};
float y[5]={6.7,2.4,4.5,6.8,2.1};
clrscr();
bubble(x,5); // passes array name(x) and size
bubble(y,5); // passes array name(y) and size
cout<<"sorrting x value ";
for(int i=0;i<5;i++)
{
cout<<x[i]<<" ";
}
cout<<endl;
cout<<"sorting y value ";
for(int j=0;j<5;j++)
{
cout<<y[j]<<" ";
}
getch();
return 0;
}

6. Explain with example, the types of Inheritance in C++?
Refer class notes

7. Write a C++ program to add two complex numbers using Binary operator overloading, define
the operator function as a friend function.
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
void getdata()
{
cout<<"Enter any two complex numbers::";
cin>>x>>y;
}
friend complex operator+(complex,complex);
void display();
};
complex operator+(complex c1,complex c2)
{
complex t;
t.x=c1.x+c2.x;
t.y=c1.y+c2.y;
return t;
}
void complex::display()
{
cout<<"Addition is "<<x<<"+i"<<y;
}
void main()
{
clrscr();
cout<<"ADDITION OF COMPLEX NOS. USING BINARY OPERATOR
OVERLOADING\n";
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3=c1+c2;
c3.display();
getch();
}
8. Write a program to swap two number using class template.

#include< iostream.h>
#include<conio.h>
template<class T>
class swap
{
T a;
T b;
public:
swap(T x,T y)
{
a=x;
b=y;
cout<<"\nBEFORE SWAPPING....";
cout<<"\na= "<<a;
cout<<"\nb= "<<b;
}
void display();
};
template<class T>
void swap<T>::display()
{
T m;
m=a;
a=b;
b=m;
cout<<"\nAFTER SWAPPING....";
cout<<"\na= "<<a;
cout<<"\nb= "<<b;
}
void main()
{
int o,p;
clrscr();
cout<<"SWAPPING OF 2 NOS USING CLASS TEMPLATE\n";
cout<<"\nEnter any two numbers::";
cin>>o>>p;
swap<int>s(o,p);
s.display();
getch();
}


1. What is base to class type conversion? How it is performed?
2.Write the syntax for inheriting a class.
3. Define Hierarchical inheritance.
4. What are the different visibility modes of inheritance?
5.What is the need for virtual base class?

10 Marks:
1. Explain in detail about class template.

You might also like