PL2 Lecture 8.3-Multiple Inheritance
PL2 Lecture 8.3-Multiple Inheritance
programming
Course Code: CSC 2213 Course Title: Programming Language 2
In case of base class pointer object, if the base class function is declared as virtual
Then C++ can determine in runtime which function to call (from base class or from derived
class) based on the object pointed by the base class pointer.
• The function in the base class is declared as virtual by using the keyword virtual preceding its
normal declaration.
#include<iostream> Base class pointer pointing to derived class object
using namespace std; With virtual functions in base classes.
class Base{
int b;
public:
Base(int b=0):b(b){} int main(){
Base *basePtr;
void virtual show() { from baseclass,b = 10
cout <<"from baseclass, b = " <<b<<endl; }
};
Base base(10); from derived class..
Derived derived(20); from baseclass,b = 0
class Derived : public Base{ in derived class,d = 20
int d; basePtr = &base;
public: basePtr->show();
Derived(int d=0):d(d){}
class B
{
int b;
int main()
{
public: B *pb;
B(int b=0):b(b){} B bb(5);
virtual void show()
cout << "Pointer of B refers to the object of B" << endl;
{
cout <<"b = " <<b<<endl; pb = & bb;
}
pb->show();
}; cout << "Pointer of B refers to the object of D (Dynamic Binding) " << endl;
D dd(10);
pb = ⅆ
class D : public B pb->show();
{
int d; cout << "Pointer of D refers to the object of D" << endl;
D *pd;
public: pd = ⅆ
D(int d=0):d(d){} pd->show();
void show()
cout << "Pointer of B refers to the object of D after casting" << endl;
{ ((D *)pb)->show();
B::show();
return 0;
cout << "d = "<<d<<endl; }
}
};
Pointer of B refers to the object of B
b = 5
A virtual function is made ‘pure virtual’ by assigning zero(0) to the function name. Such a
function is also known as ‘do-nothing’ function.
A pure virtual function is a virtual function that has no definition within the
base class.
When a virtual function is made pure, any derived class must provide its own
definition. If the derived class fails to override the pure virtual function, a
compile- time error will result.
Abstract Classes
A class that contains at least one pure virtual function is said to be abstract.
Because an abstract class contains one or more functions for which there is no
definition (that is, a pure virtual function), no objects of an abstract class may be
created.
Although you cannot create objects of an abstract class, you can create pointers
and references to an abstract class. This allows abstract classes to support run-time
polymorphism.
Abstract Classes
int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
int main(){
Base *b;
Derived1 d1;
Derived2 d2;
b = &d1;
b->show();
b = &d2;
b->show();
}
ALL ABOUT VIRTUAL
- Virtual keyword is used with derived class to create only one instance of base class in
case of multiple inheritance.
- Virtual keyword is used with base class function name to implement Polymorphism /
dynamic linking /runtime binding. In polymorphism a pointer object of a base class can
resolve in runtime which function to call based on the actual object its pointing to!
The opposite of early binding is late binding. As it relates to C++, late binding
refers to function calls that are not resolved until run time.
Virtual functions are used to achieve late binding.
When access is via a base pointer or reference, the virtual function calling is
determined by the type of object pointed to by the pointer.
The main advantage to late binding is flexibility. Late binding allows to
create programs that can respond to events occurring while the program
executes.
Virtual Functions Are Hierarchical
Output:
class base { int main(){ This is base's vfunc().
public: base *p, b; This is derived1's vfunc().
This is base's vfunc().
derived1 d1;
virtual void vfunc() { derived2 d2;
cout << "This is base's vfunc().\n"; }
}; p = &b;
p->vfunc(); // access base's vfunc()
class derived1 : public base {
public: // point to derived1
void vfunc() { p = &d1;
cout << "This is derived1's vfunc().\n"; } p->vfunc(); // access derived1's vfunc()
};
// point to derived2
class derived2 : public base { p = &d2;
public: p->vfunc(); // use base's vfunc()
// vfunc() not overridden by derived2, base's is
used return 0;
}; }
Friend Function
Friend class—A class in which all functions can access the non-public members
of another class. The general format of friend class is –
class A
{
class B is declared as a friend of class A . So,
friend class B;
now all the member functions of class B
};
became friend functions of class A.
class B
{
};
Friend Class: Example
We do this in the same way as we make a function as a friend of a class. The only
difference is that we need to write class_name :: in the declaration before the
name of that function in the class whose friend it is being declared.
The friend function is only specified in the class and its entire body is declared
outside the class.
Class Members as Friend: Format
class A; // forward declaration of A
needed by B
class B
{
void display( A a ); //only specified. Body is not declared
};
class A
{
friend void B::display( A );
};
delete p_var;
Here, p_var is a pointer variable that receives a pointer to memory that is large
enough to hold an item of type type.
Dynamic Memory Allocation: Example
#include <iostream>
using namespace std;
int main()
{
int *p;
p = new int; // allocate memory for int
*p = 20; // assign that memory the value 20
cout << *p; // prove that it works by displaying value
delete p; // free the memory
return 0;
}
References
1. https://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htm
2. https://www.cplusplus.com/doc/tutorial/
3. http://en.cppreference.com/w/cpp/header
Books
Teach Yourself C++, 3rd Edition, Herbert Schildt.