0% found this document useful (0 votes)
19 views35 pages

unit 6 oops

Inheritance is a key feature of object-oriented programming that allows subclasses to inherit attributes and methods from a parent class, promoting code reuse. The document outlines different types of inheritance in C++, including single, multilevel, multiple, hierarchical, and hybrid inheritance, as well as concepts of aggregation and composition. It also introduces abstract classes and pure virtual functions, emphasizing their role in defining interfaces for derived classes.

Uploaded by

priyanshu110101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views35 pages

unit 6 oops

Inheritance is a key feature of object-oriented programming that allows subclasses to inherit attributes and methods from a parent class, promoting code reuse. The document outlines different types of inheritance in C++, including single, multilevel, multiple, hierarchical, and hybrid inheritance, as well as concepts of aggregation and composition. It also introduces abstract classes and pure virtual functions, emphasizing their role in defining interfaces for derived classes.

Uploaded by

priyanshu110101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Inheritance

UNIT 6
Inheritance is one of the core features of object-oriented programming. It’s a programming
procedure that allows you to reuse code by referencing the behaviors and data of an object. In
other words, a class that inherits from another class shares all the attributes and methods of the
referenced class.

An inherited class is called a subclass or child class of the class it inherits from. And the class
being inherited is called either a parent class, superclass, or base class.
The derived class inherits the features from the base class and can have

additional features of its own. For example,

class Animal {

// eat() function

// sleep() function};

class Dog : public Animal {

// bark() function };

Here, the Dog class is derived from the Animal class. Since Dog is derived
from Animal, members of Animal are accessible to Dog.
Notice the use of the keyword public while inheriting Dog from Animal.

class Dog : public Animal {...};

We can also use the keywords private and protected instead of public.
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
Order of
constructor
and
Destructor
call for a
given order
of
Inheritance.
Aggregation
Aggregation is a relationship that comes under object-oriented programming,
classifying an instance of a class as “has a.” It’s a form of association with weaker
relationship strength, whereby the lifetime of the contained object (part) is not
controlled based on the lifetime of the container object (whole). Concepts of
aggregation are quite important for developing modular and reusable software
components.
Aggregation is a type of association that represents a relationship where one class
is a collection or container of another class. It depicts a “has-a” relationship, where
the container object can exist independently of its contents, and the contained
objects can exist independently of the container.
● It represents Has-A’s relationship.
● It is a unidirectional association i.e. a one-way relationship. For example, a department
can have students but vice versa is not possible and thus unidirectional in nature.
● In Aggregation, both entries can survive individually which means ending one entity
will not affect the other entity.
It represents a Has-A relationship. In the above example: Student Has-A name.
Student Has-A ID. Department Has-A Students as depicted from the below
media
Composition
Composition is a core concept in object-oriented programming that refers to the
relationship “part-of” between classes. It is a stronger form of association in which the
contained objects’ lifecycle is strongly associated with the container object’s lifecycle.
The understanding of composition is crucial in the design of complex systems where
objects of the system are composed of other objects.
Composition is a type of association meaning one class “contains” another. This
association can be said to be a “part-of” relationship and would denote that the
contained object is strongly connected with the containing object, the whole. The parts
cannot be without the whole.
Composition is a restricted form of Aggregation in which two entities are highly
dependent on each other.
● It represents part-of relationship.
● In composition, both entities are dependent on each other.
● When there is a composition between two entities, the composed object cannot
exist without the other entity.
When considering the above classroom and student scenario, the student object does
not depend on the classroom object. Moreover, destroying the classroom does not
affect or destroy the student object. Therefore, the association between classroom and
student is an aggregation.
the School object contains classroom objects. As the school object contains classroom
objects, it is an aggregation, but it is a special type of aggregation. If we destroy the
school object, the classroom object is also destroyed. Therefore, it is a composition,
and it implies ownership.
C++ Hierarchical Inheritance

Inheritance is a feature of Object-Oriented-programming in which a derived class (child class)


inherits the property (data member and member functions) of the Base class (parent class). For
example, a child inherits the traits of their parents.
In Hierarchical inheritance, more than one sub-class inherits the property of a single base
class. There is one base class and multiple derived classes. Several other classes inherit the
derived classes as well. Hierarchical structures thus form a tree-like structure. It is similar to
that, mango and apple both are fruits; both inherit the property of fruit. Fruit will be the Base
class, and mango and apple are sub-classes.
The below diagram shows, Class A is a Base class, B is a subclass inherited from
class A, and C is a subclass it also inherits from class A.
Similarly, if another
subclass inherits
property from B class
and so on then there
will be a hierarchy, and
a tree-like structure is
formed, below is the
diagram.
Syntax:
Class A

............

};

Class B: access_specifier A

.........

};

Class C: access_specifier A

.............

};
class A is the base class

class B inherits from A using public inheritance.

class C also inherits from A.

Object b of class B is created.

b.show_B(); → Calls function from class B.

b.show_A(); → Calls inherited function from class A.

Object c of class C is created.

c.show_C(); → Calls function from class C.

c.show_A(); → Calls inherited function from class A.


ABSTRACT CLASS
An abstract class is a class that is designed to be specifically used as a base class.

Contains at least one pure virtual function

Used as a blueprint for derived classes

Sometimes implementation of all functions cannot be provided in a base class because we


don’t know the implementation. Such a class is called an abstract class.
For example, let Shape be a base class. We cannot provide
implementation of function draw() in Shape, but we know every
implementation of function draw() in Shape, but we know every
derived class must have implementation of draw().
Similarly an Animal class doesn’t have implementation of move()
(assuming that all animals move), but all animals must know how to
move. We cannot create objects of abstract classes.
C++ Pure Virtual Functions
Pure virtual functions are used

● if a function doesn't have any use in the base class


● but the function must be implemented by all its derived classes

Let's take an example,

Suppose, we have derived Triangle, Square and Circle classes from the Shape class, and we want to calculate the area
of all these shapes.

In this case, we can create a pure virtual function named calculateArea() in the Shape. Since it's a pure virtual function,
all derived classes Triangle, Square and Circle must include the calculateArea() function with implementation.

A pure virtual function doesn't have the function body and it must end with = 0.
A pure virtual function doesn't have the function body and it must end with = 0. For
example,

class Shape {

public:

// creating a pure virtual function

virtual void calculateArea() = 0;

};

Note: The = 0 syntax doesn't mean we are assigning 0 to the function. It's just
the way we define pure virtual functions.
A class that contains a pure virtual function is known as an abstract class. In the above example, the class
Shape is an abstract class.

We cannot create objects of an abstract class. However, we can derive classes from them, and use their data
members and member functions (except pure virtual functions).
Output

Enter the length of the square: 4

Area of square: 16

Enter radius of the circle: 5

Area of circle: 78.5


Abstract Class (Shape)

● Contains a protected data member dimension (accessible by derived classes).


● A method getDimension() to input the dimension value.
● A pure virtual function calculateArea() that must be overridden in derived classes.

Derived Classes (Square & Circle)

● Both classes override calculateArea() to compute the area based on their specific formula.

Main Function (main())

● Creates objects of Square and Circle.


● Asks the user for input and calculates the respective areas.

You might also like