BCA 3.3 Non Linear Data Structure Using C++
BCA 3.3 Non Linear Data Structure Using C++
BCA 3.3 Non Linear Data Structure Using C++
Program BCA
Subject Non Linear Data structure using C++
Semester III
University Kuvempu university
Session 32
BCA 3.3 Non Linear Data structure using C++
Unit 03
Title Operators Overloading and Inheritance
Session Outcomes
Forms of Inheritance/ Inheritance types/ Types
of Inheritance
1)Single inheritance
2) Multilevel inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance
1)Single inheritance
Single inheritance is defined as the inheritance in which a derived
class is inherited from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.
#include <iostream>
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};
class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();
return 0;
}
Output:
Multiplication of a and b is : 20
In the above example, class A is privately inherited.
Therefore, the mul() function of class 'A' cannot be
accessed by the object of class B. It can only be
accessed by the member function of class B.
2) Multilevel inheritance
Multilevel inheritance is a process of deriving a class from
another derived class
Example:
Class A { ….. };
Class B : Public A { ….. };
Class C : Private B{ ….. };
3) Multiple inheritance
Multiple inheritance is the process of deriving a new
class that inherits the attributes from two or more
classes.
syntax:
class A
{ ….. };
class B : visibility A
{ …. };
class C : visibility A
{ …. };
5) Hybrid inheritance
When multi level and multiple inheritances are applied to an inheritance, then
it is called Hybrid inheritance.
Example :
Class student {……};
Class test : public student {……};
Class result : public test {…….};
Class result : public sports {…….};
Making a Private Member Inheritable
C++ provides a third visibility modifier, protected, which serve a
limited purpose in inheritance. A member declared as protected is
accessible by the member functions with in its class and any class
immediately derived from it. It cannot be accessed by the functions
outside these two classes.
Author: E Balaguruswamy
Title of the book: Object Oriented Programming with C++