Encapsulation and 3 types || inheritance In C++ And Types

 Encapsulation

Encapsulation is defined as the wrapping up of data and information in a single unit.

In Object Oriented Programming, encapsulation is defined as binding together the data and the functions that manipulate them.

Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc.

Methods | Variables

Class


Encapsulation (C++) :-

Encapsulation facilitates data hiding in C++ by using private, protected and public access specifiers for restricting access to the class member functions and data members.

There are 3 types of access modifiers available in C++:

  1. Public
  2. Private (default)
  3. Protected

Public Specifier :-

All the class members declared under the public specifier will be available to everyone.
The data members and member functions declared as public can be accessed by other classes and functions too.
The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.

Private Specifier:-

The class members declared as private can be accessed only by the member functions inside the class.
They are not allowed to be accessed directly by any object or function outside the class.

Protected Specifier
The protected access modifier is similar to the private access modifier in the sense that it can’t be accessed outside of its class unless with the help of a friend class.
The difference is that the class members declared as Protected can be accessed by any subclass (derived class) of that class as well.


Inheritance in C++
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 in C++.
In this article, we will learn about inheritance in C++, its modes and types along with the information about how it affects different properties of the class.

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

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.
Example:
In the given image, Mufasa is the base class and Simba (son of Mufasa) inherits the attributes and behavior of Mufasa.
(Base Class → Derived Class)

Multilevel Inheritance
Multilevel Inheritance in C++ is the process of deriving a class from another derived class.
When one class inherits another class, it is further inherited by another class. It is known as multilevel inheritance.
Example:
  • Grandmother → Mother → Child
  • Grandmother = Base class
  • Mother = Derived class (from Grandmother)
  • Child = Derived class (from Mother)
  • Child inherits all features of Mother.

Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class.
The constructors of inherited classes are called in the same order in which they are inherited.


And Learn more for Follow Channel 

Comments

Popular posts from this blog

DBMS Architecture & Models || DBMS Tier Architecture

Database Design And ER Diagram || Entity And Attributes

System Analysis and Design Notes | Easy Explanation for Students