KEMBAR78
Inheritance in C++ | PPTX
Presentation Group Members :-
08. Ankita
14. Ashutosh
18. Samiksha
19. Laxman
S.Y.BSc.(Computer Science)
I
Types of Inheritance :-
Demo !!!!
Doubt & Question Session ????
Single
Inheritance
Multilevel
Inheritance
Multiple
Inheritance
Hierarchal
Inheritance
Hybrid
Inheritance
class base_class_name
{
access specifier:
:
};
Class derived_class_name : access specifier base_class_name
{
access specifier:
:
};
void A :: getdata ()
{
cout<<"nn Enter value
a:";
cin>>a;
cout<<"Enter value b:";
cin>>b;
}
void B :: calculate ()
{
c = (a + b);
}
void B :: display ()
{
cout<<"n"<<a<<"+"<<b<
<"="<<c;
}
Access Public Protected Private
Same class/
Base class
yes yes yes
Derived
classes
yes yes no
Outside
classes
yes no no
Public inheritance
Base access specifier
Derived access
specifier
Derived class access? Public access?
Public Public Yes Yes
Private Private No No
Protected Protected Yes No
Private inheritance
Base access specifier
Derived access
specifier
Derived class access? Public access?
Public Private Yes No
Private Private No No
Protected Private Yes No
Protected inheritance
Base access specifier
Derived access
specifier
Derived class access? Public access?
Public Protected Yes No
Private Private No No
Protected Protected Yes No
class derived class_name:<access_specifier>
class1_name,<access_specifier>
class2_name,………………………………………..,
<access_specifier> classn_name
{
:
: //members of derived class
:
}
SYNTAX:
class sub : public superA, private
superB
{
:
: //members of class sub
:
}
FOR INSTANCE:
Multilevel Inheritance :-
Definition:
“It is the inheritance hierarchy wherein subclass acts as a
base class for other classes”
It is implemented by defining at least three classes.
In multilevel inheritance, there is one base class and the
remaining two is derived class.
Declaration :-
class A
{………..
……….}; // Base class
class B : public A
{………..
……….}; // B derived from A
class C : public B
{……….
………}; // C derived from B
Fig. Multilevel Inheritance :-
Student
Test
Records
Result
Base Class
Intermediate
base class
Hybrid inheritance :-
Definition:
“Inheritance hierarchy that reflects any legal combination of
other four types of inheritance.”
Simple word:-
“The method of combining any two or more forms of
inheritance in single form is called hybrid inheritance.”
Fig. Hybrid Inheritance :-
Student
Test
Records
Result
Sports
Base
class
Intermediate
base class
Derived
class
Base
class
class A
{……….
………};
class B:public A
{……….
……….};
class C
{……….
………};
class D: public b, public c
{……….
……….};
Constructors in Inheritance :-
• Base class constructors are automatically called for you if
they have no argument.
• If you want to call a superclass constructor with an argument,
you must use the subclass's constructor initialization list.
• C++ supports multiple inheritance (for better or worse), so the
base class must be referred to by name, rather than "super()".
Constructor Example :-
class SuperClass
{
public:
SuperClass(int foo)
{ // do something with foo }
};
class SubClass : public SuperClass
{
public: SubClass(int foo, int bar) : SuperClass(foo)
{ // do something with bar }
};
Virtual keyword determines if a member function of a class can
be over-ridden in its derived classes.
The non-virtual member functions are resolved at compiling
time and it’s called static binding. However, the c++ virtual
member functions are resolved during runtime and it’s called
as dynamic binding
Virtual Class :-
For your Attention !!!!

Inheritance in C++