Unit 3
Unit 3
Introduction to Inheritance
Introduction to Inheritance
Introduction to Inheritance
Need of Inheritance
➢ Avoid Duplication of code
➢ Increases the execution time
➢ Reduces Chances of error and data redundancy
➢ Capability of inheriting features
➢ Reusability of code
Types of Inheritance
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
Single Inheritance
the inheritance in which a class A // base class
derived class is inherited from {
the only one base class
..........
};
class B : acess_specifier A //
derived class
{
...........
};
Multiple Inheritance
class A
a class can inherit more than one class
{
..........
};
class B
{
...........
};
class C : acess_specifier A,
access_specifier B
{
...........
};
Hierarchical Inheritance
class A // base class
{ .............. };
class B : access_specifier A // derived from
A
{ ........... } ;
class C : access_specifier A // derived A
{ ........... } ;
class D : access_specifier A // derived
from A
{ ........... } ;
Multilevel Inheritance
class A // base class
one class inherits another child class {
...........
};
class B : access_specifier A // derived class
{
...........
};
class C : access_specifier B // derived class
B
{
...........
};
Hybrid Inheritance
➢A combination of more than one type of
inheritance.
➢For example, A child and parent class relationship
that follows multiple and hierarchical inheritance
both can be called hybrid inheritance.
Syntax Hybrid Inheritance
class A {
.........
};
class B : public A{
..........
};
class C {
...........
};
class D : public B, public C
{
...........
};
MemberAccess Control
Public member is accessible to all the
functions of the program.
Private member is accessible within the
class only.
Protected member is accessible within its
own class as well as the class immediately
derived from it.
Base and Derived Class
Derive a new class (subclass) from an
existing class (base class or
superclass).
Duplication is avoided regardless of the number of paths that exist to the child
class.
Pointers
• Pointers are symbolic representations of addresses. They enable programs to simulate call-
by-reference as well as to create and manipulate dynamic data structures. Iterating over
elements in arrays or other data structures is one of the main use of pointers.
• The address of the variable you’re working with is assigned to the pointer variable that
points to the same data type (such as an int or string).
datatype *var_name; int *ptr; // ptr can point to an address which holds int data
Pointers
Program
// C++ program to illustrate Pointers
#include <iostream.h>
using namespace std;
void geeks()
{
int var = 20;
// Driver program
int main() { arith(); }
Arrays of Pointers
An array of pointers is an array that consists of variables of pointer type, which means that the variable is a
pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers;
then its declaration would look like:
• In the above code, we are assigning the address of 'a' variable to the third element of an array 'ptr'.
• We can also retrieve the value of 'a' be dereferencing the pointer.
Array of Pointers
#include <iostream>
using namespace std;
int main()
{ int ptr1[5]; // integer array declaration
int *ptr2[5]; // integer array of pointer declaration
cout << "Enter five numbers :" <<endl;
for(int i=0;i<5;i++)
{ cin >> ptr1[i]; }
for(int i=0;i<5;i++)
{ ptr2[i]=&ptr1[i]; }
// printing the values of ptr1 array
cout << "The values are" << endl;
for(int i=0;i<5;i++)
{ cout << *ptr2[i] << endl; } }
Pointers to Functions
• After storing the address in the pointer to the object, the member function can be called using the
pointer to the object with the help of an arrow operator.
‘This’ Pointer
• In C++ programming, this is a keyword that refers to the current instance of the
class. There can be 3 main usage of this keyword in C++.