unit 6 oops
unit 6 oops
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
class Animal {
// eat() function
// sleep() function};
// 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.
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
............
};
Class B: access_specifier A
.........
};
Class C: access_specifier A
.............
};
class A is the base class
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:
};
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
Area of square: 16
● Both classes override calculateArea() to compute the area based on their specific formula.