Overloading+ Inheritance
Overloading+ Inheritance
#include <iostream>
using namespace std;
INHERITANCE
Inheritance is one of the most important features of Object Oriented Programming
in C++. The capability of a class to derive properties and characteristics from
another class is called Inheritance.
Syntax-
class derived_class_name : access-specifier base_class_name
{
// body ....
};
Example-
// C++ program to demonstrate inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
} };
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
} };
int main() {
// Create object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();
// Calling member of the derived class
dog1.bark();
return 0;
}
TYPES OF INHERITANCE
1. Single inheritance- In single inheritance, a class is allowed to inherit from only one class. i.e. one
base class is inherited by one derived class only.
class A
{ ... .. ... };
class B: public A
{ ... .. ... };
2. Multiple inheritance- Multiple Inheritance is a feature of C++ where a class can inherit from more
than one class. i.e one subclass is inherited from more than one base class.
class A
{ ... .. ... };
class B
{ ... .. ... };
class C: public A, public B
{ ... ... ... };
3. Multilevel Inheritance- In this type of inheritance, a derived class is created from another derived
class and that derived class can be derived from a base class or any other derived class. There can be
any number of levels.
class C
{ ... .. ... };
class B : public C
{ ... .. ... };
class A: public B
{ ... ... ... };
4.Hierarchical Inheritance- In hierarical type of inheritance, more than one subclass is inherited
from a single base class. i.e. more than one derived class is created from a single base class .
class A
{ // body of the class A. }
class B : public A
{ // body of class B. }
class C : public A
{ // body of class C. }
class D : public A
{ // body of class D. }
5. Hybrid Inheritance- It is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance
in C++.
class F
{ ... .. ... }
class G
{ ... .. ... }
class B : public F
{ ... .. ... }
class E : public F, public G
{ ... .. ... }
class A : public B
{ ... .. ... }
class C : public B
{ ... .. ... }
VISIBILITY
Public Visibility mode: If we derive a subclass from a public base class. Then the public
member of the base class will become public in the derived class and protected members of the
base class will become protected in the derived class.
Protected Visibility mode: If we derive a subclass from a Protected base class. Then both
public member and protected members of the base class will become protected in the derived
class.
Private Visibility mode: If we derive a subclass from a Private base class. Then both public
Cmember and protected members of the base class will become Private in the derived class.