OOPS Concept in C++

OOPS Concept in C++

There are four pillars of Object-Oriented Programming concept and they pretty much run the OOPS concept in C++ – 

These functionalities are implemented with the help of Classes and Objects.

We will learn more about them in detail in the upcoming posts.

PrepSter Tip: Consider Classes and Objects as the heart of any object-oriented language.

Other Features of OOPS in C++ –

  • Dynamic Binding
  • Message Passing
OOPs Concept in C++

OOPs Concept Brief

Objects in C++ –

This is the primary unit in object oriented programming, represent a collection of number of entities. Each object will contain its own data and code to do manipulation in the the other part of code/data, objects are instances of a class.

Example – Fruit is an Object and data will be – name, color, size, volume or student – name, rollNo, Age, percentage etc

  • Objects are real world representation in a code and contains its properties & functions etc properties like name, color, size etc.
  • Objects are Run time entities, i.e. they will only take up space at run time not compile time for any program
  • Objects can interact without having to know details of each others data or code
  • On program execution objects talk to one another by sending messages

Classes in C++ –

You can consider a class a blueprint or enclosing entity for objects. Class is a collection of objects of similar type. Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class.

Example – student1, student2, student3 are the member of class student and they will have their own properties like name, rollNo, Age etc.

Prepster Tip – Class is a user defined data type like structures and unions in C

class student
{
    char name[20];
    int rollNo;
  public:
    void getdetails(){}
};

int main()
{
    student s1; //s1 is a object 
}

Note – The below object oriented concepts have their own posts, don’t worry if you don’t understand them completely, we are just trying to introduce these and will cover each of them in detail in the later posts.

Syntax for Class

class class_name
{

private:
// Here we will have private data members
// functions declarations of the class

public:
// Here we will have public data members
// functions declarations of the class

protected:
// Here we will have protected data members
// functions declarations of the class
};

Data Abstraction – The concept of representing important details and hiding away the implementation details is called data abstraction. This programming technique separates the interface and implementation.

Use Case –

#include 
using namespace std;
int main()
{
cout<<"Data Abstraction Example"<<endl;
return 0;
}

Here, we don’t have to know how cout implemented the printing of the statement, it is declared somewhere else generally in the iostream file. Here the coder only cares about interface not the implementation.

Data Encapsulation Encapsulation can be used to hide data members and members function. Under this definition, encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition.

  • Data is hidden from outside world and is kept safe from any misuse.
  • Only functions wrapped inside same scope can access it.

Data Hiding is the process of removing direct access by objects, they can only use functions to access such data.

Use Case – Consider program A which runs to authenticate password at the time of logging in. It sends the information to program B. Program B runs its code and returns only True/False to program A by comparing actual password and input password.

Here Program B hides the implementation and UI Accessible program A never gets to know the actual password but, only knows if it password entered is correct or incorrect.

Inheritance – Inheritance in OOPS is the ability inherit properties of an another class in its own.

Inheritance plays around Super class(parent class) and sub Class(Child class).

Example – A class car will be able to inherit properties of a super class vehicle.

  1. Provides the concept of re-usability, e.g. all vehicles have wheels, rather than creating data property of noTypes in each subClass Bike, Car, Truck we can just declare it once in Vehicle class and inherit properties from it.

Polymorphism – Polymorphism basically means ability to take more than one form, for example you may be a brother to someone, or father or grandfather, student, employer or employee. So, you’re a different pesonality in different situations. We will discuss polymorphism in detail when we study polymorphism page.

Dynamic Binding – Dynamic binding also called dynamic dispatch is the process of linking procedure call to a specific sequence of code (method) at run-time. It means that the code to be executed for a specific procedure call is not known until run-time. Dynamic binding is also known as late binding or run-time binding.

Tab Content

Message Passing – The process by which one object can interact with other object is called message passing. We will discuss more in detail about this in future posts.

Benefits of Object Orient Programming

  • More secure code and hacking resistivity, because of data hiding.
  • For any class multiple objects(instances) can co exist without any overlap of interest.
  • The code looks more organised as code segmentation(partition) is possible due to objects/classes
  • Because of inheritance redundant code is eliminated, as super class properties are extended to subclasses.
  • The Code is scalable from small to large.

The table below summaries the accessibility in all case scenerios –

Please Login/Signup to comment