2. • The capability of a class to derive
properties(the data members) and
functionality(the member functions)
from another class is
called Inheritance. Inheritance is one
of the most important key feature
of Object Oriented Programming.
3. What is parent class?
The class that is being inherited
inherited by other class is
class is known as parent class, super
class, super class or base class.
class.
What is child class?
A class that inherits another class is
class is known as child class, derived 3
Animal
Dog
4. In this type of inheritance a single
derived class may inherit from two or
more than two base classes. This means
that a single child class can have
multiple parent classes. Using comma
separation list
4
7. EXAMPLE:
#include <iostream>
using namespace std;
class bird {
public:
void type()
{
cout << " Aquatic flightless bird" << endl;
}
};
class adaptation {
public:
void life()
{
cout << "Adapted for life in the water" << endl;
}
};
class penguin: public bird, public adaptation{
};
int main()
{
penguin p1;
p1.type();
p1.life();
return 0;
}
7
Output :
8. CONSTRUCTORS CALLING :
• The constructors of Inherited classes
are called in the same order in which
they are inherited.
For example ,in the following programme
m mammal’s class constructor called before
c class winged animal’s constructor
8
9. EXAMPLE :
9
#include <iostream>
using namespace std;
class Mammal
{
public:
Mammal()
{
cout << "Mammals can give direct birth."
<< endl;
}
};
class WingedAnimal
{
public:
WingedAnimal()
{
cout << "Winged animal can flap." << endl;
}
};
class Bat: public Mammal, public
WingedAnimal {
};
int main()
{
Bat b1;
return 0;
}
Output :
10. PARAMETERISED AND DEFAULT CONSTRUCTORS :
#include <iostream>
using namespace std;
class A {
public:
A(){
cout<<" Default Constructor of A class"<<endl;}
A(int x){
cout<<" Parameterized Constructor of A class"<<endl;
}
};
class B {
public:
B(){
cout<<" Default Constructor of B class"<<endl;}
B(int g){
cout<<" Parameterized Constructor of B class"<<endl;
}
};
10
class C: public A, public B {
public:
C(){
cout<<" Default Constructor of C class"<<endl;
}
C(int z):A(z),B(z){
cout<<" Parameterzied Constructor of C class"<<endl;
}
};
int main() {
C obj(20);
return 0;
}
Output:
11. AMBIGUITY IN MULTIPLE INHERITANCE :
• What if same function is present in both the parent classes??
11
If we try to call the
function using the object
of the derived class,
compiler shows error.
It's because compiler
doesn't know which
function to call.
For example,
class base1
{ public:
void someFunction( ) { .... ... .... } };
class base2
{ public:
void someFunction( ) { .... ... .... } };
class derived : public base1, public base2 {
};
int main()
{ derived obj;
obj.someFunction() ; // Error!
return 0;
}
12. ONE POSSIBLE SOLUTION IS :
12
• The above issue can
be resolved by using
the Scope resolution
operator with the
function or we can say
explicitly calling
int main()
{
obj.base1::someFunction( );
// Function of base1 class is called
obj.base2::someFunction();
// Function of base2 class is called.
return0;
}
13. DIFFERENCE BETWEEN
MULTIPLE INHERITANCE AND MULTILEVEL INHERITANCE
13
Multiple Inheritance Multilevel Inheritance
“Multiple Inheritance” refers to the
concept of one class extending(or
inherits)
More than one base class
In this type of inheritance, a derived
class is created from another
derived class.
19. SlidesCarnival icons are editable shapes.
This means that you can:
⊚ Resize them without losing quality.
⊚ Change fill color and opacity.
⊚ Change line color, width and style.
Isn’t that nice? :)
Examples:
19
Find more icons at slidescarnival.com/extra-
free-resources-icons-and-maps
21. ✋👆👉👍👤👦👧👨👩👪💃🏃💑❤😂😉
😋😒😭👶😸🐟🍒🍔💣📌📖🔨🎃🎈🎨🏈
🏰🌏🔌🔑 and many more...
😉
You can also use any emoji as an icon!
And of course it resizes without losing quality.
How? Follow Google instructions
https://twitter.com/googledocs/status/730087240156643328
21