Inheritance Unit3

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

Inheritance

Unit 3
Inheritance

• The capability of a class to derive properties and characteristics


from another class is called Inheritance.

• Inheritance is one of the most important features of Object-


Oriented Programming.

• Inheritance is a feature or a process in which, new classes are


created from the existing classes.

• The new class created is called “derived class” or “child class”


and the existing class is known as the “base class” or “parent
class”. The derived class now is said to be inherited from the
base class.
Inheritance

• When we say derived class inherits the base class, it means,


the derived class inherits all the properties of the base class,
without changing the properties of base class and may add
new features to its own. These new features in the derived
class will not affect the base class.
• The derived class is the specialized class for the base class.

• Sub Class: The class that inherits properties from another


class is called Subclass or Derived Class.

• Super Class: The class whose properties are inherited by a


subclass is called Base Class or Superclass.
Why and when to use inheritance?

• Consider a group of vehicles. You need to create classes for


Bus, Car, and Truck. The methods fuelAmount(), capacity(),
applyBrakes() will be the same for all three classes.
• If we create these classes avoiding inheritance then we have to
write all of these functions in each of the three classes as
shown below figure:
• You can clearly see that the above process results in duplication of
the same code 3 times. This increases the chances of error and data
redundancy. To avoid this type of situation, inheritance is used.
• If we create a class Vehicle and write these three functions in it and
inherit the rest of the classes from the vehicle class, then we can
simply avoid the duplication of data and increase re-usability.
Look at the below diagram in which the three classes are inherited
from vehicle class:
Implementing inheritance in C++
• For creating a sub-class that is inherited from the base class we have to follow
the below syntax.
• Derived Classes: A Derived class is defined as the class derived from the
base class.
• Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}
• Where, class is keyword to create a new class
derived_class_name — name of the new class, which will inherit the base
class
access-specifier — either of private, public or protected. If neither is
specified, PRIVATE is taken as default
base-class-name — name of the base class
Example
• class ABC : private XYZ //private derivation
{ }
• class ABC : public XYZ //public derivation
{ }
• class ABC : protected XYZ //protected derivation
{ }
• class ABC: XYZ
//private derivation by default
{ }
• When a base class is privately inherited by the derived class,
public members of the base class becomes the private
members of the derived class and therefore, the public
members of the base class can only be accessed by the
member functions of the derived class. They are inaccessible
to the objects of the derived class.

• On the other hand, when the base class is publicly inherited by


the derived class, public members of the base class also
become the public members of the derived class. Therefore,
the public members of the base class are accessible by the
objects of the derived class as well as by the member functions
of the derived class.
C++ program to demonstrate implementation of Inheritance

using namespace std;

// Base class
class Parent
{
public:
int id_p;
};

// Sub class inheriting from Base Class(Parent)


class Child : public Parent
{
public:
int id_c;
};
int main()
{
Child obj1;

// An object of class child has all data members


// and member functions of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is: " << obj1.id_c << '\n';
cout << "Parent id is: " << obj1.id_p << '\n';

return 0;
}
class A {
public: int x;

protected: int y;

private: int z;
};
class B : public A {
// x is public
// y is protected
// z is not accessible from B
};

class C : protected A {
// x is protected
// y is protected
// z is not accessible from C
};

class D : private A // 'private' is default for classes


{
// x is private
// y is private
// z is not accessible from D
};
Example: define member function without argument within the class
#include <iostream>
using namespace std;

class Person {
int id;
char name[100];

public:
void set_p()
{
cout << "Enter the Id:";
cin >> id;
cout << "Enter the Name:";
cin >> name;
}
void display_p()
{
cout << endl <<"Id: "<< id << "\nName: " << name <<endl;
}
class Student : private Person {
char course[50];
int fee;
public:
void set_s()
{
set_p();
cout << "Enter the Course Name:";
cin >> course;
cout << "Enter the Course Fee:";
cin >> fee;
}
void display_s()
{
display_p();
cout <<"Course: "<< course << "\nFee: " << fee << endl;
}
};
int main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}
Output

• Enter the Id: 101


• Enter the Name: Dev
• Enter the Course Name: GCS
• Enter the Course Fee:70000
• Id: 101
• Name: Dev
• Course: GCS
• Fee: 70000
Example: define member function with argument outside the class

#include<iostream>
#include<string.h>
using namespace std;

class Person
{
int id;
char name[100];

public:
void set_p(int,char[]);
void display_p();
};

void Person::set_p(int id,char n[])


{
this->id=id;
strcpy(this->name,n);
}

void Person::display_p()
{
cout<<endl<<id<<"\t"<<name;
}
class Student: private Person
{
char course[50];
int fee;
public:
void set_s(int,char[],char[],int);
void display_s();
};

void Student::set_s(int id,char n[],char c[],int f)


{
set_p(id,n);
strcpy(course,c);
fee=f;
}

void Student::display_s()
{
display_p();
cout<<"t"<<course<<"\t"<<fee;
}
main()
{
Student s;
s.set_s(1001,"Ram","B.Tech",2000);
s.display_s();
return 0;
}
C++ program to explain single inheritance

#include<iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

// sub class derived from a single base classes


class Car : public Vehicle {

};

// main function
int main()
{
// Creating object of sub class will invoke the constructor of base classes
Car obj;
return 0;
}
C++ program to implement Multilevel Inheritance

#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

// first sub_class derived from class vehicle


class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheels\n"; }
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
• This is a Vehicle
• Objects with 4 wheels are vehicles
• Car has 4 Wheels
C++ program to explain multiple inheritance

#include <iostream>
using namespace std;

// first base class


class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

// second base class


class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
class Car : public Vehicle, public FourWheeler
{
};

int main()
{
// Creating object of sub class will invoke the
//constructor of base classes.

Car obj;
return 0;
}
Output
• This is a Vehicle
• This is a 4 wheeler Vehicle
C++ program to implement Hierarchical Inheritance

#include <iostream>
using namespace std;

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

class Car : public Vehicle {


};

class Bus : public Vehicle {


};

int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;

You might also like