OOPs Concepts in C++

OOPs Concepts 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 Objects: The concepts of objects and classes are linked with each other and form the foundation of the object-oriented paradigm. An object is described as an instance of the class.

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

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.

Class

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.
  • x co-ordinate, which denotes the x co-ordinate of the center.
  • y co-ordinate which denotes the y co-ordinate of the center.
  • 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.

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.

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.

It allows new classes to acquire the properties of an existing class. It helps in reusing the code and the programmer need not to write the same code again and again for different classes. 

They just need to make use of inheritance property. Existing classes are called base classes or parent classes or superclasses while the new classes are called derived classes or child classes or subclasses. 


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

Example: From a class mammal, a number of classes can be derived such as humans, cats, dogs, cows, buffalo, etc. All have the distinct characteristics of a mammal. 
In addition, each has its own particular characteristics. It can be said that buffalo is-a mammal.

Types of Inheritance
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • 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.



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.


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.

Features of message passing

  • Message passing between the objects is generally unidirectional.
  • Message passing allows all the communications between the objects possible.
  • Essentially involves invoking class methods.

Previous
Next Post »

Comment Below: ConversionConversion EmoticonEmoticon