Multiple Inheritance
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
using namespace std; class object With virtual functions in
class Base{
base classes.
int b;
public:
Base(int int main(){
b=0):b(b Base *basePtr;
void){}virtual show() {
from baseclass, b
cout <<"from baseclass, b = " <<b<<endl; }
};
Base base(10); = 10 from derived
Derived derived(20); class.. from
class Derived : public Base{ baseclass, b = 0
int d; basePtr = &base; in derived class, d
basePtr- = 20
public:
De
>show();
rive
void show() { basePtr = &derived;
d(intcout << "from derived class.."<<endl; basePtr->show();
d=0)Base::show(); }
:d(d)cout << "in derived class, d = "<<d<<endl;
} {}
};
Check
yourself!!
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 = " pb = & bb;
<<b<<endl; 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 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
class number { class decType : public number { int main(){
protected: public: decType d;
int val; void show() { hexType h;
public: cout << val << "\n"; octType o;
void setval(int i) { val = i; } }
// show() is a pure virtual }; d.setval(20);
function d.show(); // displays 20
virtual void show() = 0; class octType : public number h.setval(20);
}; { h.show(); // displays 14
class hexType : public number public: o.setval(20);
{ public: void show() { o.show(); // displays 24
void show() { cout << oct << val << "\n";
cout << hex << val << "\n";} } return 0;
}; }; }
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
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
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().
This is derived1's vfunc().
public: base *p, b; 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
class derived1 : public base { vfunc()
public:
void vfunc() { // point to derived1
cout << "This is derived1's vfunc().\n"; } p = &d1;
}; p->vfunc(); //
access
class derived2 : public base { derived1's
public: vfunc()
// vfunc() not overridden by derived2, base's is
used // point to
}; derived2
p = &d2;
p->vfunc(); //
use base's
vfunc()
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
#include <iostream> class Rectangle int main()
using namespace std; { {
int length; Square square(5);
class Square int breadth; Rectangle rectangle;
{ public: rectangle.shape(square);
friend class int getArea(){ cout << rectangle.getArea() ;
Rectangle; return length *
// declaring breadth; } return 0;
Rectangle as }
friend class void shape( Square a ){
int side; length = a.side; Output:
public: breadth = a.side;} 25
Square };
( int s){
side = s; }
};
Class Members as Friend
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;
}
Dynamic Memory Allocation: Example
1 // new, delete # of elements: 5 *ptr n Sum i
2 int main(){ Enter elements:
3 int n, i,*ptr, sum=0; 2 5 19
0
15
2
8
22 1
4
3
2
50
4 cout << "# of elements: "; 6
5 cin >> n; //input 5 7
6 ptr = new int[n]; 4 0 1 2 3 4
7 if(ptr==NULL){ //ptr==0 3
8 cout << "Error! not Sum = 22 2 6 7 4 3
9 allocated.";
10 return 1;
11 }
12 cout << "Enter elements:\n";
13 for(i=0;i<n;++i)
14 { //input 2 6 7 4 3
15 cin >> *(ptr+i); //ptr[i]
16 sum += *(ptr+i);
17 }
18 cout << "Sum = " << sum;
19 delete [] (ptr);
20 //memory de-allocated
}
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.