OOP 3

You might also like

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

Object Oriented

Programming

Course Code: CS-322


Credit Hours: 4(3-3)
Instructor: Muhammad Bilal
Inheritance
• Inheritance is a form of Software reuse

• In Inheritance we create a class that absorbs an existing class’s capabilities, then


customizes or enhances them.
• Software reuse saves time during program development
• When creating a class, instead of writing completely new data members and member
functions, you can specify that the new class should inherit the members of an
existing class.
• This existing class is called the base class, and the new class is called the derived
class.
• Base class is also known as superclass and the derived class as the subclass.
2
Inheritance
• Every object of a derived class is also an object of base class.
• However, baseclass objects are not objects of their derived classes.

• For example, if we have Vehicle as a base class and Car as a derived class, then all
Cars are Vehicles, but not all Vehicles are Cars—for example, a Vehicle could also
be a Truck or a Boat.

• one base class can have many derived classes, the set of objects represented by a
base class typically is larger than the set of objects represented by any of its
derived classes. For example, the base class Vehicle represents all vehicles,
including cars, trucks, boats, airplanes, bicycles and so on.
• By contrast, derived class Car represents a smaller, more specific subset of all 3
vehicles.
Base classes and Derived classes
• Base classes tend to be more general and derived classes tend to be more specific.

4
Inheritance Hierarchy
• Inheritance relationships form class hierarchies.
• A base class exists in a hierarchical relationship with its derived classes. Although
classes can exist independently, once they’re employed in inheritance
relationships, they become affiliated with other classes.
• A class becomes either a base class—supplying members to other classes,
or a derived class—inheriting its members from other
classes, or both.

5
Inheritance Hierarchy

6
Shape Class Hierarchy

7
Shape Class Hierarchy

• This hierarchy begins with base class Shape.


• Classes TwoDimensionalShape and ThreeDimensionalShape derive from base class
Shape

• We can follow the arrows from the bottom of the diagram upwards to the topmost base
class in this hierarchy to identify several is-a relationships.
• For instance, a Triangle is a TwoDimensionalShape and is a Shape, while a Sphere is a
ThreeDimensionalShape and is a Shape.

8
Syntax of Inheritance in C++
class derived_class : access-specifier base_class
{

};

9
Example
#include <iostream>
using namespace std;

class Base
{
public:

int publicVar;

void display()
{
cout << "Value of publicVar: " << publicVar;
}
};

class Derived : public Base


{

};

int main()
{
Derived obj;

obj.publicVar=10;
10
obj.display();
#include<iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"animal eats";
}
void sleep()
{
cout<<"animal sleeps";
}
};

class Dog : public Animal


{
public:
void bark()
{
cout<<"dog barks";
}
};

class Cat : public Animal


{
public:
void meow()
{
cout<<"cat meow meow";
}
};
int main()
{
Cat obj;
}

11
Modes of Inheritance in C++
• Mode of inheritance controls the access level of the inherited members of the base
class in the derived class. In C++, there are three modes of inheritance:

• Public Mode
• Protected Mode
• Private Mode

12
Public Inheritance 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.

• Example:
class ABC : public XYZ
{
};

13
Protected Inheritance Mode
• If we derive a subclass from a Protected base class. Then
both public members and protected members of the base
class will become protected in the derived class.

• Example:
class ABC : protected XYZ
{
}

14
Private Inheritance Mode
• If we derive a subclass from a Private base class. Then
both public members and protected members of the base class will
become private in the derived class.

They can only be accessed by the member functions of the derived class.
• Private mode is the default mode that is applied when we don’t specify any mode.
• Example:
class ABC : private XYZ {}

15
• Note: The private members in the base class cannot be directly
accessed in the derived class, while protected and public members
can be directly accessed. To access or update the private members of
the base class in derived class, we have to use the
corresponding getter and setter functions of the base class or declare
the derived class as friend class.

16
• The below table summarizes the above three modes and shows the
access specifier of the members of the base class in the subclass when
derived in public, protected and private modes:

17
Example : Program to show different kinds of Inheritance Modes and their Member Access Levels

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
};

18
Types Of Inheritance in C++
• The inheritance can be classified on the basis of the relationship
between the derived class and the base class. In C++, we have 5 types
of inheritances:
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4.Hierarchical inheritance
5.Hybrid inheritance

19
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.
• Syntax
class subclass_name : access_mode base_class
{
// body of subclass
};

20
1. Single Inheritance
// C++ program to demonstrate how to implement the 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 {
public:
Car() { cout << "This Vehicle is Car\n"; }
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}

21
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.
• Syntax
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
// body of subclass
};

22
2. Multiple Inheritance
• Here, the number of base classes will be separated by a comma (‘, ‘)
and the access mode for every base class must be specified and can be
different.
• Example:

23
2. Multiple Inheritance
// C++ program to illustrate the 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\n"; }
};

// sub class derived from two base classes


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

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj; 24
return 0;
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.
• Syntax
class derived_class1: access_specifier base_class
{
... .. ...
}
class derived_class2: access_specifier derived_class1
{
... .. ...
}
.....

25
3. Multilevel Inheritance
// 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 << "4 Wheeler Vehicles\n"; }
};

// sub class derived from the derived base class fourWheeler


class Car : public fourWheeler {
public:
Car() { cout << "This 4 Wheeler Vehical is a Car\n"; }
};

// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
26
Polymorphism in Inheritance
• Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

class Animal
{
public:
void animalSound()
{
cout << "The animal makes a sound \n";
}
};

class Cat : public Animal


{
public:
void animalSound()
{
cout << "The cat says: meow meow \n";
}
};

// Derived class
class Dog : public Animal
{
public:
void animalSound()
{
cout << "The dog says: bow wow \n";
}
};

int main()
{
Animal myAnimal;

Dog myDog;
myAnimal.animalSound(); 27
myDog.animalSound();
Constructors in Inheritance
#include<iostream>
using namespace std;
class A1
{
public:
A1()
{
cout << "Constructor of class A1 \n";
}
};

class A2: public A1


{
public:
A2()
{
cout << "Constructor of class A2 \n";
}
};

int main()
{
A2 obj;
return 0;
}
28
Destructors in Inheritance

29
#include <iostream>
using namespace std;
class parent //parent class
{
public:
Destructors in Inheritance
parent() //constructor
{
cout << "Parent class Constructor\n";
}
~parent()//destructor
{
cout << "Parent class Destructor\n";
}
};

class child : public parent//child class


{
public:
child() //constructor
{
cout << "Child class Constructor\n";
}
~ child() //destructor
{
cout << "Child class Destructor\n";
}
};
int main()
{
child c; 30
}
Encapsulation
• Encapsulation is a state of binding/wrapping related data or code in
one place.
• You wrap and bind all related data together in a single form.
• Example:
• In a Car the engines, cooling, battery, breaking, clutching system are
hosted together in the front of the car.
• Similarly, for a coder to give code more understandable and readable
format he groups and encapsulates all the related data together
inside a single unit.
• Note: Data abstraction and Encapsulation are different
31
Encapsulation
#include <iostream>
using namespace std;

class Rectangle
{
public:
int len;
int width;

int calculateArea()
{
return len * width;
}
};

int main()
{
Rectangle obj;
obj.len = 10;
obj.width = 20;
cout << "Area: " << obj.calculateArea();
}
32
Abstraction

33
Abstraction
• Types of Abstraction
• Header Files:
• We all use header files, we import #include to use the power function. We
directly use the pow() function as pow(2, 3) to get results.
• But, the implementation details are hidden from us, we just get the desired
output without knowing what happened in the background.
• Classes :
• In C++, we can define which data members & member function
implementation we want to show to the outside world and which ones we
want to hide.
• This is done using access specifiers and getters/setters.
34
Abstraction
#include <iostream>
using namespace std;
class myClass {
// data members and functions declared public
private:
int x, y;
public:
void setX(int x1){
x = x1;
}
void setY(int y1){
y = y1;
}
int getX(){
return x;
}
int getY(){
return y;
}
};
int main(){
myClass obj;
obj.setX(20);
obj.setY(40);
// x = 100; error
// y = 200; error
cout << "x: " << obj.getX() << " y: " << obj.getY() << endl; // this is how you read with getter interface
}
35

You might also like