Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BCA 3.3 Non Linear Data Structure Using C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

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

Sub – title Inheritance types-single inheritance


making a private member inheritable
Recap of previous class

 Understanding the Inheritance definition, defining


derived classes
Learning objectives

To understand the Inheritance types-single inheritance making a


private member inheritable

 
Session Outcomes

The Student will be able to understand Inheritance


types-single inheritance making a private member
inheritable

 
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.

Class D : visibilityB1, visibility.B2, visibility B3


{ (Body of D) };
* visibility may be public or private.
4) Hierarchical inheritance
It the inheritance follows the hierarchical design of a
program, then it is called hierarchical inheritance.

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.

 When a protected member is inherited in public mode, it becomes


protected in the derived class too and therefore is accessible by
the member functions of the derived class. It is also ready for
further inheritance. A protected member, inherited in the private
mode derivation, becomes private in the derived class. Although it
is available to the member functions of the derived class, it is not
available for further inheritance (since private members cannot be
inherited).
Ex:
#include<iostream.h>
#include<conio.h>
class parent
{
protected:
int a;
public:
int b;
void get(void)
{
a=2;
b=4;
}
void show(void)
{
cout<<a<<b;
}
};
class child : public parent
{
int c;
public:
void getc()
{
c=6;
}
void add()
{
int z=a+b+c; // a is protected member
cout<<" "<<z;
}
};
void main(){
clrscr();
child c;
c.get();
c.show();
c.getc();
c.add();
getch();}
 It is also possible to inherit a base class in protected mode
(known as protected derivation). In protected derivation, both
the public and protected members of the base class become
protected members of the derived class.
 While the friend functions and the member functions of a
friend class can have direct access to both the private and
protected data, the member functions of a derived class can
directly access only the protected data. However, they can
access the private data through the member functions of the
base class
MCQs
1.A member declared as ………….. is accessible by
the member functions within its class and any
class immediately derived from it.
A) protected
B) private
C) public
D) friend
Ans: A)
2. In ……………….. inheritance, the base classes are
constructed in the order in which they appear in
the deceleration of the derived class.
A) multipath
B) multiple
C) multilevel
D) hierarchical
Ans:B)
3. A class can be derived from another derived
class which is known as ………. inheritance.
A) single
B) multiple
C) multilevel
D) hierarchical
Ans: C)
4. When the properties of one class are inherited
by more than one class, which is called ………
inheritance.
A) single
B) multiple
C) multilevel
D) Hierarchical
Ans: D)
5. A derived class with only one base class is
called …………… inheritance.
A) single
B) multiple
C) multilevel
D) hierarchical
Ans:A)
Session Summary

The Student can be able to understand Inheritance types-


single inheritance making a private member inheritable
Reference

Author: E Balaguruswamy
Title of the book: Object Oriented Programming with C++  

You might also like