Object Oriented Programming Interview Questions - Part - 6

1. Define inheritance.
The mechanism of deriving a new class from an old one is called inheritance. The old class is referred to as the base class and the new one is called the derived class or subclass. Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. It provides the idea of reusability. We can add additional features to an existing class without modifying it by deriving a new class from it.
2. What are the types in inheritance?
Single Inheritance, Multiple Inheritance, Multilevel Inheritance, Hierarchical inheritance, Hybrid Inheritance, Multipath inheritance
3. Explain single Inheritance
A derived class with only one base class is called single inheritance. If a single class is derived from a single base class is called single inheritance.
Eg:
Base class
Derived class
Here class A is the base class from which the class D is derived. Class D is the public derivation of class B hence it inherits all the public members of B. But D cannot access private members of B.
4. What is multiple inheritance?
A derived class with more than one base class is called single inheritance. If a class is derived from more than one base class, it is called multiple inheritance.
Eg: Base classes
Derived class
Here class C is derived from two base classes A & B.

5. Define Hierarchical Inheritance
One class may be inherited by more than one class. This process is known as hierarchical inheritance If a number of classes are derived from a single base class then it is called hierarchical inheritance.
Eg : Hierarchical classification of students in University
A
B
A
C
B

6. What is hybrid inheritance?
There could be situations where we need to apply two or more types of inheritance to
design a program this is called hybrid inheritance. It is the combination of one or more types of inheritance.
Multilevel
inheritance
Multiple
inheritance
The class result will have both the multilevel and multiple inheritances.

7. What is multilevel inheritance?
The mechanism of deriving a class from another derived class is known as multilevel inheritance. If a class is derived from a class, which in turn is derived from another class, is called multilevel inheritance. This process can be extended to any number of levels.
Eg:
Base class Grand father
Intermediate
Base class Father
Derived class Child
23. Define private base class?
A base class which allows it’s public and protected members to be inherited as “private” members of the private members of the derived class. Thus, the inherited members are accessible to the members and friends of the derived class, but they are not accessible to the users of the derived class.
8. What is the visibility of inherited members?
Base class visibility Derived class visibility
Public Private Protected
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected
9. How ambiguity resolution in inheritance?
We may face a problem in using the multiple inheritance, when a function with
the same inheritance appears in more than one base class.
Class M
{
public :
void disp(){}
};
Class N
{
public :
void disp(){}
};
Class O : public M,N
{
public :
void display()
{disp()} // ambiguity resolution
};
10. What is virtual base class?
The child has two direct base classes parent1 and parent2 which themselves have a common bas class grand parent. The child inherits the traits of grand parent via two separate paths. It can also inherit directly shown by the broken line. The grand parent is sometimes referred to as indirect base class. A base class that is qualified as virtual in the inheritance definition. In case of multiple inheritance, if the base class is not virtual the derived class will inherit more than one copy of members of the base class. For a virtual base class only one copy of members will be inherited regardless of number of inheritance paths between base class and derived class.
Eg: Processing of students’ results. Assume that class sports derive the roll number from class student. Class test is derived from class Student. Class result is derived from class Test and sports. As a virtual base class As a virtual base class

11. What is abstract class?
An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class. It is a design concept in program development and provides a base upon which other classes may be built. A class that serves only as a base class from which derived classes is derived. No objects of an abstract base class are created. A base class that contains pure virtual function is an abstract base class.

12. What is Nesting of classes?
A class can contain objects of other classes as its members it is called nesting of classes.
Class A{…};
Class B
{
A a;
};
13. What is Polymorphism?
Polymorphism is one of the crucial features of OOP. It simply means ‘one name, multiple forms’. Polymorphism is the feature that allows one interface to be used for a general class of actions.(ie) “one interface multiple methods”. This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action.  It contains runtime polymorphism and compiles time polymorphism
14. What are the types of polymorphism?
1 Run time polymorphism
2 Compile time polymorphism
15. What is compile time polymorphism?
The overloaded member functions are selected for invoking by matching arguments, both type and number. This information is known to the compiler at the compile time and, therefore, compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding or static binding or static linking. Also known as compile time polymorphism, early binding simply means that an object is bound to its function call at compile time.
16. What is runtime polymorphism?
Member function could be selected while the program is running. This is known as run time polymorphism. At run time, when it is known what class objects are under consideration, the appropriate version of the function is invoked. Since the function is linked with a particular class much later after the compilation, this process is termed as late binding. It is also known as dynamic binding because the selection of the appropriate
function is done dynamically at runtime.
17. What the types in compile time polymorphism?
1 Function overloading
2 Operator overloading
18. What is pointer to objects?
To use pointers to access the class members. As stated earlier, a pointer can point
to an object created by a class.
Class item
{
public :
void get(){..};
};
void main()
{
item *i; // i is pointer object
i->get(); // -> arrow operator to access pointer object members.
}
19. What is this pointer?
A unique keyword called this to represent an object hat invokes a member function. This is a pointer that points to the object for which this function was called. This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member function.
20. Explain Pointer to derived classes?
We can use pointers not only to the base objects but also to the objects of derived classes. Pointers to objects of a base class are type compatible with pointers to objects of a derived class.
B * cptr; // Pointer to class B type variable
B b; // base object
D d; // derived object
Cptr=&b; // cptr points to object b
Cptr = &d; // cptr points to object d
21. What is Virtual function?
The same function name in the base and derived classes, the function in base class is declared as virtual using the keyword virtual preceding its normal declaration. When a function is made virtual, c++ determines which function to use at runtime based on the type of object pointed to by the base pointer, rather than the type of the pointer. Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function.
22. What are the rules for virtual function?
_ the virtual functions must be members of some class.
_ they cannot be static members
_ they are access by using object pointes
_ A virtual function can be a friend of another class.
_ A virtual function is a base class must be defined, even though it may not be used.
_ We cannot have virtual constructors, but we can have virtual destructors.
23. What is pure virtual functions?
A pure virtual function is a function declared in a base class that has no definition
relative to the base class.
Virtual void display()=0;


24. How the constructor executed in derived classes?
In case of multiple inheritances, the base classes are constructed in the order in which they appear in the declaration of the derived class. Similarly in a multilevel inheritance, the constructors will be executed in the order of inheritance.
25. Write a program for single inheritance?
class base
{
public:
void printbase(){cout<<”From base”;}
};
class derived: base
{
public:
void printderived(){cout<<”From Derived”;}
};
void main()
{
derived d;
d.printderived();
d.printbase();
}
Define virtual function?
A function qualified by the virtual keyword. When a virtual function is called via a pointer, the class of the objected pointed to determine which function definition will be used. Virtual functions implement polymorphism, whereby objects belonging to different classes can respond to the same message in different ways.
66)Write some of the basic rules for virtual functions
_ Virtual functions must be member of some class.
_ They cannot be static members and they are accessed by using object pointers
_ Virtual function in a base class must be defined.
_ Prototypes of base class version of a virtual function and all the derived class
versions must be identical.
_ If a virtual function is defined in the base class, it need not be redefined in the
derived class.


67) What are pure virtual functions? Write the syntax.
A pure virtual function is a function declared in a base class that has no definition
relative to the base class. In such cases, the compiler requires each derived class to either
define the function or redeclare it as a pure virtual function. A class containing pure
virtual functions cannot be used to declare any object of its own. It is also known as “donothing”
function.
The “do-nothing” function is defined as follows:
virtual void display ( ) =0;

Part B
Explain inheritance?
Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. It provides the idea of reusability. We can add additional features to an existing class without modifying it by deriving a new class from it.
Types:
Single Inheritance, Multiple Inheritance, Multilevel Inheritance, Hierarchical
Inheritance, Hybrid Inheritance, Multipath inheritance
i. single inheritance
If a single class is derived from a single base class is called single inheritance.
Eg:
Base class
Derived class
Here class A is the base class from which the class D is derived. Class D is the public derivation of class B hence it inherits all the public members of B. But D cannot access private members of B.
ii. multiple inheritance
If a class is derived from more than one base class, it is called multiple inheritance.
Eg: Base classes
Derived class
Here class C is derived from two base classes A & B.
iii. Hierarchical inheritance
If a number of classes are derived from a single base class then it is called hierarchical inheritance.
Eg : Hierarchical classification of students in University
A
B
A
C
B
Student
Arts Engineering M e d i c a l
CSE ECE Civil
iv. Multilevel inheritance
If a class is derived from a class, which in turn is derived from another class, is called multilevel inheritance. This process can be extended to any number of levels.
Eg:
Base class Grand father
Intermediate
Base class Father
Derived class Child
v. Hybrid inheritance
It is the combination of one or more types of inheritance. The class result will
have both the multilevel and multiple inheritances.
Multilevel
inheritance
Multiple
Inheritance

2. Explain virtual functions
A function qualified by the ‘virtual’ keyword is called virtual function. When a virtual function is called through a pointer, class of the object pointed to determine which function definition will be used.
The rules for virtual functions are
_ Virtual functions must be member of some class.
_ They cannot be static members and they are accessed by using object pointers
_ Virtual function in a base class must be defined.
_ Prototypes of base class version of a virtual function and all the derived class
versions must be identical.
_ If a virtual function is defined in the base class, it need not be redefined in the
derived class.
Pure virtual functions
A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases, the compiler requires each derived class to either define the function or redeclare it as a pure virtual function. A class containing pure virtual functions cannot be used to declare any object of its own. It is also known as “donothing”
function.
The “do-nothing” function is defined as follows:
virtual void display ( ) =0;

1. What is Inheritance?
Inheritance is the process of creating new classes called derived classes from the
existing or base classes. The derived class inherits all the capabilities of the base class.
The main advantages of the inheritance are
• Reusability of code.
• To increase the reliability of the code.
• To add some enhancements to the base class.
Defining Derived Classes:
A derived class can be defined by specifying its relationship with the base class in
addition to its own details. The general form of defining a derived class is
Class derived_class_name : visibility_mode base_class_name
{
………………
……………… //member of derived class
………………
};
The colon indicates that the derived class name is derived from the base class
name. The visibility mode is optional and if present may be either private or
public . The default visibility mode is private. Visibility mode specifies
whether the features of the base class are privately derived or publicly derived.
Example:
1)class abc: private xyz //private derivation
{
member of abc;
};
2)class abc:public xyz //public derivation
{
member of abc;
};
3)class abc:xyz //private derivation by default
{
members of abc;
};
When a base class is privately inherited by a derived class, ’public members’ of
the base class become ‘private members’ of the derived class. The public members of the
base class can only be accessed by the member functions of the derived class.
When the base class is publicly inherited, public members of the base class
become public members of the derived class.
In inheritance some of the base class data elements and member functions are
inherited into the derived. We can add our own data and member functions to the base
class.
2. Explain the Single inheritance
A derived class with only one base class is called single inheritance.
Let us consider a example to illustrate inheritance. The following program shows
a base class B and a derived class D. The class B contains one private data member, one
public data member and three public member functions.
class b
{
int a;
public:
int b;
void get_ab()
{
a=5;
b=10;
}
int get_a()
{
return a;
}
void show_a()
{
cout<<”a:”<<a<<endl;
}
};
class d:public b
{
int c;
public:
void mul()
{
c=b*get_a();
}
void display()
{
cout<<”a:”<<get_a()<<endl;
cout<<”b”<<b<<endl;
cout<<”c:”<<c<<endl;
}
};
Void main()
{
D d1;
d1.get_ab();
d1.mul();
d1.show_a();
d1.display();
}
The class D is a public derivation of the base class B. Therefore, D inherits all the
public members of B. thus a public member of the bas class B is also a public member of
derived class D.
The private members of B cannot be inherited by D. The class D, in effect, will
have more members than what is contains at the time of declaration as shown in
following figure.
3. Explain the Multilevel inheritance
The mechanism of deriving a class from another ‘derived class’ is known as
multilevel inheritance. The form of multilevel inheritance is
Class D
Private Section
Public Section
c
b
Get ()
Show()
Mul()
Display()
B
A
B
C
Base Class
Intermediate Base Class
Derived Class
Grand Father
Father
child
The class A serves as a base class for the derived class B which in turn serves as
a base a class for the derived class C. The class B is known as intermediate base class
since it provides a link for ht inheritance between A and C. the chain ABC is known as
inheritance path.
A derived class with multilevel inheritance declared as follows.
Class A
{
……….
};
class B : public A
{
………..
};
Class C : public B
{
………
};
This process can be extended to nay number of levels.
Example
# include <iostream.h>
class student
{
protected:
int rollno;
public:
void get_number()
{
cout << “Enter Roll No”;
cin>>rollno;
}
void put_number()
{
cout << “Roll Number =“<< rollno;
}
};
class test : public student
{
protected :
float s1,s2;
public :
void get_marks()
{
cout << “Enter Subject1, subject 2 Marks”;
cin>>s1>>s2;
}
void put_marks()
{
cout << “Subject 1 = “<< s1 <<endl;
cout << “Subject 2 = “<< s2 <<endl;
}
};
class result : public test
{
private :
float total;
public :
void display()
{
total =s1+s2;
put_number();
put_marks();
cout << “Total = “ <<total;
}
};
void main()
{
result stud1;
stud1.get_number();
stud1.get_marks();
stud1.display();
}
output
Enter RollNo
1
Enter subject1, subject2 Marks
90
80
Roll Number = 1
Subject1 = 90
Subject 2 = 80
Total = 170
In this example class student stores the rollno, class test stores the marks obtained
in two subjects and class result contains the total marks obtained in the test. The class
result can inherit the details of the marks obtained in the test and the rollno of students
through multilevel inheritance.
4. Explain the Multiple inheritances
A class can be derived from more than one bas class. This is called multiple
inheritances. Multiple inheritance allows us to combine the features of several existing
classes. The form of multiple inheritance is
Here the class C is derived from bas classes A and B.
The syntax of a derived class with multiple base classes is as follows
Class D: visibility B1, visibility B2,…
{
………
};
Where, visibility may be either public or private. The base classes are separated
by commas. The declaration of multiple inheritance is
Class A
{
………….
};
class B
{
………..
};
class C: public A, public B
{
………….
};
Example
# include <iostream.h>
class M
{
protected:
int m;
public :
void get_m()
{
cout<<"Enter m\n";
cin>>m;
}
};
class N
{
protected:
int n;
public :
void get_n()
A B
C
{
cout<<"Enter n\n";
cin>>n;
}
}
class P : public M, public N
{
public:
void display()
{
cout<<"m="<<m;
cout<<"n="<<n;
}
};
void main()
{
P p1;
p1.get_m();
p1.get_n();
p1.display();
}
Output
Enter m
1
Enter n
2
m=1
n=2
5. What is Ambiguity resolution in inheritance?
Two bas classes have functions with the same name, while a class derived from
both base classes. This is known as a ambiguity in multiple inheritance. The problem is
how do objects of the derived class access the correct base class function the name of the
function alone is in sufficient, since the compiler can’t figure out which of the two
functions is meant.
To avoid ambiguity between the derived class and one of the base classes or
between the base class themselves, it is better to use the scope resolution operations ::
alone with the data members and methods.
6. What is Virtual Base Classes?
Consider the situation shown in following figure.
Grandparent
Here the ‘child’ has two direct base classes ‘parent1’ and ‘parent2’ which
themselves have a common base class ‘grandparent’. The ‘child’ inherits the traits of
‘grandparent’ via two separate paths. The ‘grandparent’ is sometimes referred to as
indirect base class. All the public and protected members of ‘grandparent’ are inherited
into ‘child’ twice, first via ‘parent1’ and again via ‘parent2’. This means ‘child’ would
have duplicate sets of the members inherited from ‘grandparent’. This introduces
ambiguity and should be avoided.
The duplication of inherited members due to multiple paths can be avoided by
making the common base class as virtual base class.
Example
Class grandparent
{
protected:
int basedata;
};
class parent1 : virtual public grandparent
{ ………….};
class parent2 : virtual public grandparent
{ …………..};
class child : public parent1, public parent2
{
public:
int get_data()
{ return basedata;}
};
7. Pointers to Objects :
Pointers can point to objects as well as to simple data types . For example , the
following declaration of creating an object is valid in c++ .
Class sample
{
private :
int x ;
float y ;
char s ;
public :
void get data();
Parent1 Parent2
Child
void display();
};
void main()
{
sample *per;
....................
}
Where per is a pointer variable that holds the address of the class object sample
and consists of the three members such as int x, float y and char s and also holds member
functions such as get data() and display().
The pointer to an object of class variable will be accessed and processed in one of
the following ways .
1. (*object name) . member _name = variable ;
The parentheses are essential since the member of class period (.) has a higher
precedence over the indirection operator (*) .
2. object name->member name = variable ;
The pointer to the member of a class can be expressed using dash (-) followed by
the greater then sign (>) .
Case 1 :
A member of class object can be accessed by the indirection operator which has
been shown in the following program segment .
Class student
{
private :
.............
public:
............
};
void main()
{
student *per ;
(*per) . data member ;
(*per) . member function();
}
case 2 :
A member of class object can be accessed by the pointer of a class operator which
has been shown in the following program segment .
Class student
{
private :
.............
public :
.............
};
void main()
{
student *per ;
per -> data member ;
per -> member function();
}
Example:
#include<iostream.h>
class add
{
private :
int a,b,c ;
public :
void read ()
{ can>>a>>b; }
void sum ()
{ c=a+b; }
void print ()
{ cout<<”The sum is “<<c ; }
};
void main()
{
add *a ;
a -> read();
a -> sum();
a -> print ();
}
public :
virtual void show()=0;
};
class derv1:public base
{
public :
void show()
{ cout<<”\n Derv1 “; }
};
class derv2:public base
{
public :
void show()
{ cout<<”\nDerv2”; }
};
void main()
{
base *list ;
derv1 dv1 ;
derv2 dv2;
list=&dv1;
list->show();
list=&dv2;
list->show();
}