Polymorphism and Inheritance: C plus plus Class Notes

Updated: Aug 18

Mobiprep has created last-minute notes for all topics of C++ to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on C++.

  1. Basics of C plus plus

  2. Basics Of OOPs

  3. Classes and Objects

  4. Polymorphism and Inheritence

  5. Exception Handling

  6. File Handling and templates

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.


  1. Explain different types of polymorphism.

  2. Explain Pure Virtual Functions.

  3. What is the need of overloading operators and functions?

  4. Write down the example to overload unary and binary operators in C++.

  5. Write down a C++ program to implement function overloading.

  6. What is the need of inheritance?

  7. Draw a diagram to represent the forms of inheritance.

  8. Discuss the role of access specifiers in inheritance and show their visibility when they are inherited as public, private and protected.

  9. Discuss the concept of generalization and aggregation.

  10. How overriding is different from the overloading?

Polymorphism and Inheritance


Question 1) Explain different types of polymorphism.

Answer) There are two types of Polymorphism-

1. Run time Polymorphism-

If the appropriate member function could be selected while the program is running. This is called run time polymorphism.

2. Compile time Polymorphism-

The compiler uses the information from functions and operators at compile time, and, hence, is able to select the appropriate function for a particular call at compile time. This is called early or static binding or static linking or compile time polymorphism.


 

Question 2) Explain Pure Virtual Functions.

Answer) A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Base class having pure virtual function becomes abstract i.e. it cannot be instantiated.

Syntax:

virtual return_type function_name() = 0;

Example:


class Base
{
public:
virtual void show() = 0;
};
class Derived:public Base
{
void show()
{cout<<I love TV shows”);}
};
int main()
{
Derived d;
d.show();
return 0;
}

Output:

I love TV shows

 

Question 3) What is the need of overloading operators and functions?

Answer) Overloading is needed as it increases the readability of the program as you don’t need to use different names for the same action.

Note that All except three operators: ‘.’, ’?:’ and ‘sizeof” can be overloaded in C.


 

Question 4) Write down the example to overload unary and binary operators in C++.

Answer) Unary- (++)


#include <iostream>
using namespace std;
class Count
{
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ ()
{
++value;
}
void display() {
cout <<"Count" << value << endl;
}
};
int main()
{
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}

Output

count: 6
count: 7

Binary-


#include <iostream>
using namespace std;
class Count {
private:
int value;
public Count() : value(5) {}
Count operator ++ () {
Count temp;
temp.value = ++value;
return temp; }
Count operator ++ (int) {
Count temp;
temp.value = ++value;
return temp; }
void display() {
cout<<"Count:"<<value <<endl; }
};
int main()
{
Count count1, result;
result = ++count1;
result.display();
result = count1++;
result.display();
return 0;
}

Output-

count: 6
count: 7

 

Question 5) Write down a C++ program to implement function overloading.

Answer)


#include <iostream>
using namespace std;
/ / function with float type parameter
float absolute(float var)
{
if (var &< 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var)
{
if (var < 0)
var = -var;
return var;
}
int main()
{
cout <<"Absolute value of -5 = "<< absolute(-5) <<endl;
cout << "Absolute value of 5.5 ="<<absolute(5.5f) << endl;
return 0;
}

Output-

Absolute value of -5 = 5
Absolute value of 5.5 = 5.5


 

Question 6) What is the need of inheritance?

Answer) Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. most knowledge is made manageable by hierarchical (that is, top-down) classifications.


 

Question 7) Draw a diagram to represent the forms of inheritance.

Answer)

Forms of inheritance in c++ class notes


 

Question 8) Discuss the role of access specifiers in inheritance and show their visibility when they are inherited as public, private and protected.

Answer) In inheritance, it is important to know when a member function in the base class can be used by the objects of the derived class.

A public member is accessible from anywhere outside the class but within a program.

A private member variable or function cannot be accessed, or even viewed from outside the class.

A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.


 

Question 9) Discuss the concept of generalization and aggregation.

Answer) Generalization is the process of extracting common properties from a set of entities and creating a generalized entity from it. In specialization, an entity is divided into sub-entities based on their characteristics. It is a top-down approach where a higher level entity is specialized into two or more lower level entities. A relationship with its corresponding entities is aggregated into a higher level entity.


 

Question 10) How overriding is different from the overloading?

Answer) Overriding happens at runtime. Binding of overridden method call to its definition happens at runtime.





8 views0 comments

Recent Posts

See All