OOPs Concepts in C++

What is OOPs in C++?

C++ fully supports object-oriented programming including the six pillars of object-oriented development. They are:

1. Class and Object

2. Encapsulation

3. Data Hiding

4. Inheritance

5. Polymorphism

6. Message Passing

    Class and Object in C++

    1. The concepts of objects and classes are linked with each other and form the foundation of the object-oriented paradigm.

    2. An object is described as an instance of the class.

    3. An object is a real-world component in object-oriented circumstances that may have a physical or conceptual presence.

    4. Objects can be modeled according to the need of the application. An object can be a customer, a car, etc or an intangible conceptual existence like a project, a process, etc.

    What are Classes in C++?

    A class represents a collection of objects having the same characteristics properties that exhibit common behavior. It provides the design or description of the objects that can be formed from it.

    The production of an object as a member of a class is called instantiation. Thus, Object is an instance of a class. Since the class provides blueprints for the objects so an object is created from a class.

    The programmers or users can create as many objects as they want for a single class. All the objects have the same property belonging to the same class.

    Example: Let us suppose a simple class, named circle, that describes the regular geometrical figure. Circle is a regular geometrical figure that can be drawn in a 2-dimensional (2d) space. The attributes of the class circle can be identified as follows.

    1. x co-ordinate, which denotes the x co-ordinate of the center.

    2. y co-ordinate which denotes the y co-ordinate of the center.

    3. a which denotes the radius of the circle.

    Here, x represents the 'x' co-ordinate of the circle class and 'y' represents the y co-ordinate of circle class and 'a' represents the radius of the circle class. These are the properties of the class circle.

    What is Encapsulation and Data Hiding?

    Encapsulation is the process of limiting attributes and methods collectively within a class so that no other outside source can intervene and misuse it. The internal details of a class can be hidden from outside of the class with the help of encapsulation.

    It allows the elements of the class to be accessed from outside only by the interface given by the class. The data and member functions are wrapped into a single unit.

    Data hiding is the process of insulating the data of an object. It is also called information hiding. Generally, a class is created such that its data (or attributes) can only be accessed by its class methods and protected from direct outside access. It provides security to the data.

    Abstraction is the process of showing only information or features of any object to the outside world which is required and hiding all other unnecessary information about the entity or object.

    For example, If you want to read content on website 'Learning Mania' you only need to search for it on a web browser in search engines like Google but not thinking about the process behind the scene that how does it goes to website server and fetch data from it and how does it show the results to your web browser.

    What is Inheritance?

    Inheritance is the mechanism that permits new classes to be created out from the existing classes by extending and refining its capabilities.

    In simple words, Inheritance is the ability to create a new class by making use of the existing class. It helps in reducing the development time.

    Inheritance defines an "is-a" relationship among classes.

    Types of Inheritance

    1. Single Inheritance

    2. Multiple Inheritance

    3. Multilevel Inheritance

    4. Hierarchical Inheritance

    5. Hybrid Inheritance

    When a subclass is derived from a single-parent class is called single inheritance.

    When a subclass is derived from more than one base class is called multiple inheritance. The relation of one child with two parents is shown in multiple inheritance.

    When a subclass is derived from a superclass and superclass is derived from another superclass and so on is called multi-level inheritance.

    It shows the relation of Grand Parent to Parent and the relation of Parent to Child. Basically, the relation of Grandparents to parent to child is shown in multi-level inheritance.

    The tree structure like inheritance is called hierarchical inheritance. The relation of multiple children with one parent is shown in hierarchical inheritance.

    The combination of multiple and multi-level inheritance is called hybrid inheritance.

    What is Polymorphism?

    Polymorphism is derived from the Greek words 'poly' which means 'many' and 'morphs' which means 'form'. Polymorphism means 'many forms'. It is the ability to take multiple forms.

    In an object-oriented paradigm, polymorphism can be achieved using operations in different styles depending upon the instance they are working upon. Polymorphism enables objects with various internal structures to have the same external interface.

    Example: Let us suppose two classes, namely circle and square. Both classes have a common method named WhatIsArea().

    Here, the method name and its purpose in the classes are the same but internal implementation i.e., the method of calculating area is different. This is called polymorphism.

    In C++, Polymorphism can be categorized as two types namely compile-time polymorphism and run-time polymorphism.

    Compile-time polymorphism can be achieved by method overloading (function overloading and operator overloading). Run-time polymorphism can be achieved by using virtual function.

    What is Message Passing?

    Any application needs a number of objects communicating in a harmonious manner. Objects in a system may interact with each other by practicing message passing. Suppose a system has two objects: Object1 and Object2, the object Object1 sends a message to other object Object2, if Object1 wants Object2 to execute one of its methods.

    What are the Features of Message Passing?

    1. Message passing between the objects is generally unidirectional.

    2. Message passing allows all the communications between the objects possible.

    3. Essentially involves invoking class methods.


    Post a Comment

    * Please Don't Spam Here. All the Comments are Reviewed by Admin.