Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism: Anna University Part A Two Marks Important Questions and Answers
Data Structures using C ++:
Chapter 2: Inheritance and Polymorphism
Two Marks
Questions with Answers
1. What
is an abstract class ?
Answer:
The
abstract class is a base class in which at least one pure virtual function is used.
Abstract classes can be used to specify an interface that must be implemented
by all subclasses.
For
example
class area {
double dim1,dim2;
public:
void setarea(double d1,double d2)
{
dim=d1;
dim=d2;
}
void getdim(double &d1,double &d2)
{
d1=dim1;
d2=dim2;
}
virtual double getarea()=0;//pure virtual function
};
Now
we can derive another class using the area
as the base class. In this derived class the pure virtual function getarea() can be redefined for
calculating area of particular shape.
2. What
does 'this' pointer point to do?
OR
What is
role of 'this' pointer?
Answer:
The
this pointer is a hidden pointer inside every class member function that points
to the class object the member function is working with. The this pointer is
passed as a hidden parameter to the member function call and it is available in
the member function definition as a local variable.
For
example
class test
{
private:
int num;
public:
void get _val(int num)
{
this‒>num=num;
}
}
void main()
{
test obj;
int num=10;
obj.get_val(num);//here value 10 will be assigned to this‒>num
}
In
above program, the obj.get_val(num) will be interpreted as
get_val(&obj,num).
3. What
is a virtual function ?
Answer:
A
virtual function is a function that is declared within a base class and
redefined by derived class. The keyword virtual must be preceeded to the
virtual function.
4. When
do we make a virtual function "pure" ?
OR
Define
pure virtual function and mention its usage.
Answer:
The
pure virtual function is used in abstract class. When we want to override the
definition of some particular function in the derived class then we normally
declare that function as pure virtual function in the base class.
5. What
effects do the visibility labels private, protected and public have on the
members of the class ?
Answer:
If
the base class has the members possessing the visibility label as private then
those members are not accessible to derived class. If the members of the class
are labelled as protected then that means these members are public to derived
class but are private to rest of the program. If the members of the class are
labelled as public then that indicates the members can be accessible by any
class in the entire program.
6. What
is inheritance? What are its advantages ?
Answer:
Inheritance
is a process in which derived class takes some properties of the base class. At
the same time derived class can have its own properties.
Advantages
1.
Major advantage of inheritance is reusability
which means that we can add additional features into an existing class without
modifying it. For this purpose, a new class can be derived from the existing
one.
2.
Code extensibility is another
advantage of using inheritance. The modifications can be made in the base class
in order to extend the business logic.
3.
The inheritance offers the Maintainability.
That means if the inheritance is used then the hierarchy of base class and
derived class gets created and the task of error finding becomes easy.
7. What
does multiple inheritance mean?
Answer:
The
multiple inheritance is a type of inheritance in which the derived class is
derived from more than one class.
8. What
is the default access specifier of a class?
Answer:
The
default access specifier is private.
9. Specify
the situation in which protected member is used.
Answer:
When
the members of a class should be accessible by its immediate derived class
(child class) and not by the derived class of derived class(grand child class)
then in that situation the protected members are used.
10. What
is multilevel inheritance ?
Answer:
When
the derived class is derived from a base class which itself is derived class
then that type of inheritance is called multilevel inheritance.
11. How
can a private member be made inheritable?
Answer:
We
can not inherit the private members of a class, but we can use friend function
to access the private members.
12. What
is the difference between private, protected and public modes ?
Answer:
If
the base class have private members then those members are not accessible to
derived class. Protected members are public to derived classes but private to
reset of the program. And the public members are accessible to all.
13. What
is the difference between compile time and run time polymorphism?
Answer:
Compile time
polymorphism : The linking between method call and
method definition takes place at compile time. Compiler knows the exact memory
address of the function definition. The advantage of this approach is that the
execution of the code becomes fast.
For
example ‒ Function oveloading
Run time polymorphism :
The
linking between method call and method definition takes place at run time. The
advantage of this approach is that size of compiled code gets less.
For
example ‒ Virtual functions.
14. What
is function overriding ?
Answer:
The
function overriding means redefining of a function in a derived class .
15. What
is the need to declare base class as virtual ?
Answer:
When
a derived class is derived from two classes and these two classes are
themselves derived from one base class, then ambiguity may occur while using
the member functions of parent classes because two copies of the parent classes
are included in the derived class. To avoid this problem, the base class is
declared as virtual.
16. What
is the use of virtual function in C++ ?
Answer:
Virtual
function allows to have one interface, multiple functions performing different
tasks. Thus virtual function allows to have polymorphic feature of object
oriented programming.
17. Is it
possible to inherit the constructor of Its base class? Justify.
Answer:
No,
it is not possible to inherit the constructor of its base class.
18. What
is the purpose of composition ?
Answer:
Combining
or assembling the existing components together, instead of creating a new class
is called composition.
19. List
out various forms of inheritance.
Answer:
1.
Single inheritance
2.
Multilevel inheritance
3.
Multiple inheritance
4.
Hybrid inheritance
20. Write
a C++ code to display as area of square or rectangle using function overriding.
Answer:
#include<iostream>
using namespace std;
class Rectangle
{
private:
double length,bredth;
public:
void setlengthBredth(
double l,double b);
void computeArea();
};
void Rectangle::setlengthBredth(double 1,double b)
{
length=1;
bredth=b;
}
void Rectangle::computeArea()
{
double area;
area=length *bredth;
cout<<"\n
Area of Rectangle = "<<area;
}
class Square:public Rectangle
{
private:
double length;
public:
void setlength( double
1);
void computeArea();
};
void Square::setlength(double 1)
{
length=1;
}
void Square::computeArea()
{
double area;
area = length*length;
cout<<"\n
Area of Square = "<<area;
}
int main()
{
Rectangle obj1;
Square obj2;
obj1.setlengthBredth(10,20);
obj1.computeArea();
obj2.setlength(10);
obj2.computeArea();//function
overriding
return 0;
}
Output
Area of Rectangle =
200
Area of Square = 100
21. Write
a sample code to show the usage of this pointer in C++.
Answer:
#include<iostream>
#include<cstring>
using namespace std;
class Test
{
private:
char fname[30];
public:
void getData(char *str)
{
strcpy(fname, str);
}
void display(void)
{
cout<<"Name:"
<< this‒>fname << endl;
}
void
displaywithout_this(void)
{
cout <<
"Name:"<< fname << endl;
}
};
void main(void)
{
Test obj;
obj.getData("Sachin");
obj.display();
obj.displaywithout_this();
}
Output
Name:Sachin
Name:Sachin
22.
Differentiate between private and protected members of a class.
Answer:
•
If the base class have private members then those members are not accessible to
derived class.
•
On the other hand, protected members are public to derived class but private to
rest of the program.
23. What
is dynamic binding ?
Answer:
•
C++ provides the facility to specify that the compiler should match function
calls with the correct definition at the run time.
•
This mechanism is called dynamic binding. Dynamic binding is a mechanism due to
which the compiler has a provision to resolve the call to appropriate function
late until the run time.
•
Dynamic binding is achieved using virtual function.
24. Write
a C++ program to demonstrate the virtual function.
Answer:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()
{
cout <<
"\n In Base Class":
}
};
class Derived:public Base
{
public:
void display()
{
cout <<
"\n In Derived Class";
}
};
void main()
{
Base *ptr;
Derived d;
ptr = &d;
ptr‒>display();
}
Output
In Derived Class
25. State
about Cast Operator.
Answer:
Cast
operator is used to convert one data type element into another.
Various
cast operators are –
1)
const_cast<type><expression>
2)
dynamic_cast t<type><expression>
3)
static_cast<type>(expression)
4)
reinterprete_cast<type>(expression)
26. Give
the syntax of a pointer to a function which returns an integer and takes
arguments one of integer type and 2 of float type. What is the difference
between a class and a structure ?
Answer:
Syntax
int (*ptr)(int, float, float);
Difference
between class and structure:

Class
1.
It is mainly used for object oriented programming.
2.
In class, we have data, functions, constructors,destructors and inheritance.
3.
More secure because we can declare data as private
Structure
1.
It is mainly used for grouping data.
2.
Traditionally we store only data in structure.
3.
Less secure as all the members are public.
27. When
is protected access specifier used in a class?
Answer:
Protected
members are used when data or functions should be hidden from outside code but
still available to subclasses for inheritance.
28. State
the purpose of virtual base class.
Answer:
Virtual
base classes are used to avoid multiple copies of the same base class when
using multiple inheritance.
Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: : Data Structures using C++ Program - Inheritance and Polymorphism: Two Marks Important Questions and Answers
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation