Lecture 6 - Inheritance
Lecture 6 - Inheritance
Mousumi Hasan,
Assistant Professor, Department of CSE, BAIUST
Example: Insect Taxonomy
2
C++ Inheritance
One of the most important concepts in object-oriented
programming is that of inheritance. Inheritance allows us
to define a class in terms of another class, which makes it
easier to create and maintain an application. This also
provides an opportunity to reuse the code functionality and
fast implementation time.
► The language mechanism by which one class acquires the properties (data and
operations) of another class
► Base Class (or superclass): the class being inherited from
► Derived Class (or subclass): the class that inherits
Advantages of inheritance
► When a class inherits from another class, there are three benefits:
► (1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new
methods
(3) You can modify the existing class by overloading its methods with
your own implementations
The "is a" Relationship
6
Inheritance – Terminology and Notation
in C++ Base class (or parent) – inherited from
►
► Derived class (or child) – inherits from the base class
► Notation:
class Student // base class
{
. . .
};
class UnderGrad : public student
{ // derived class
. . .
};
7
Back to the ‘is a’ Relationship
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
Types of Inheritance
3. Multiple Inheritances:
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
int main() {
Bat b1;
return 0;
}
Types of Inheritance
4. Hierarchical Inheritance:
Class A
Class A
Class B Class C
Class D
Virtual Functions
► A virtual function (also known as virtual methods) is a member function that is declared within a base
class and is re-defined (overridden) by a derived class. When you refer to a derived class object using
a pointer or a reference to the base class, you can call a virtual function for that object and execute
the derived class’s version of the method.
► Virtual functions ensure that the correct function is called for an object, regardless of the type of
reference (or pointer) used for the function call.
► They are mainly used to achieve Runtime polymorphism.
► Functions are declared with a virtual keyword in a base class.
► The resolving of a function call is done at runtime.
Rules for Virtual Functions
► The rules for the virtual functions in C++ are as follows:
► Virtual functions cannot be static.
► A virtual function can be a friend function of another class.
► Virtual functions should be accessed using a pointer or reference of base class type to
achieve runtime polymorphism.
► The prototype of virtual functions should be the same in the base as well as the derived
class.
► They are always defined in the base class and overridden in a derived class. It is not
mandatory for the derived class to override (or re-define the virtual function), in that
case, the base class version of the function is used.
► A class may have a virtual destructor but it cannot have a virtual constructor.
#include <iostream>
using namespace std;
class base {
public:
virtual void print() { cout << "print base class\n"; }
return 0;
}
class base {
public:
void fun_1() { cout << "base-1\n"; }
virtual void fun_2() { cout << "base-2\n"; }
virtual void fun_3() { cout << "base-3\n"; }
virtual void fun_4() { cout << "base-4\n"; }
};