Inheritance
Inheritance
void display();
};
int main()
{
A a1(5);
A a2(4);
//a1+a2;
a1.operator+(a2);
return 0;
}
C++ Function Overriding
• If derived class defines same function as defined
in its base class, it is known as function
overriding in C++.
• It is used to achieve runtime polymorphism.
• It enables you to provide specific implementation
of the function which is already provided by its
base class.
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{
cout<<"Eating bread...";
}
};
• int main()
• {
• Dog d = Dog();
• d.eat();
• return 0;
•}
C++ virtual function
• A C++ virtual function is a member function in the base class that
you redefine in a derived class. It is declared using the virtual
keyword.
• It is used to tell the compiler to perform dynamic linkage or late
binding on the function.
• There is a necessity to use the single pointer to refer to all the
objects of the different classes. So, we create the pointer to the
base class that refers to all the derived objects.
• But, when base class pointer contains the address of the derived
class object, always executes the base class function. This issue can
only be resolved by using the 'virtual' function.
• A 'virtual' is a keyword preceding the normal declaration of a
function.
• When the function is made virtual, C++ determines which function
is to be invoked at the runtime based on the type of the object
pointed by the base class pointer.
Late binding or Dynamic linkage
In late binding function call is resolved during runtime.
Therefore compiler determines the type of object at
runtime, and then binds the function call.
Rules of Virtual Function
• Virtual functions must be members of some class.
• Virtual functions cannot be static members.
• They are accessed through object pointers.
• A virtual function must be defined in the base class,
even though it is not used.
• The prototypes of a virtual function of the base class
and all the derived classes must be identical.
• If the two functions with the same name but different
prototypes, C++ will consider them as the overloaded
functions.
Pure Virtual Function
• A virtual function is not used for performing any task. It only
serves as a placeholder.
• When the function has no definition, such function is known
as "do-nothing" function.
• The "do-nothing" function is known as a pure virtual
function. A pure virtual function is a function declared in the
base class that has no definition relative to the base class.
• A class containing the pure virtual function cannot be used
to declare the objects of its own, such classes are known as
abstract base classes.
• The main objective of the base class is to provide the traits
to the derived classes and to create the base pointer used
for achieving the runtime polymorphism.
virtual void display() = 0;
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class.";
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
In the above example, the base class contains the pure virtual function.
Therefore, the base class is an abstract base class. We cannot create
the object of the base class.