C++

Concepts Of OOPs In C++ – Detailed Instructions

With the use of objects, complicated issues can be divided up and solved using object-oriented programming. Prior to the advent of object-oriented programming, also known as OOP, programs were created using procedural languages and consisted solely of a lengthy list of instructions. On the other hand, OOP is all about developing things that can communicate with one another. Because we can comprehend how these objects relate to one another, OOP makes it simpler to design programs.

Programming using objects (OOP)

When writing programs utilizing classes and objects in object-oriented programming, we make use of OOP properties like abstraction, encapsulation, inheritance, and polymorphism.

Class and Objects

An object is an instance of a class, which functions and data members like a blueprint. Let’s take the class Car as an example, which has data members (variables) like speed, weight, and price as well as functions like gearChange(), slowDown(), brake(), etc. Let’s imagine I make a FordFigo object of this type, which uses these data elements and functions and assigns its own values to them. Similar to that, we can use the blueprint to generate as many objects as we like (class).

//Class name is Car
class Car
{
    //Data members
    char name[20];
    int speed;
    int weight;
 
public:
    //Functions
    void brake(){
    }
    void slowDown(){
    }
};
 
int main()
{
   //ford is an object
   Car ford; 
}

Abstraction

Abstraction is the process of shielding the user from unimportant details. For instance, when you send an SMS, all you have to do is input the message, choose the recipient, and click send. Although the phone notifies you that the message has been sent, you are unaware of what is taking place behind the scenes because it is unimportant to you.

Encapsulation

Data and function are combined into a single, capsule-like unit through the process of encapsulation. This is done to prevent members outside the class from accessing secret information. In order to achieve encapsulation, we keep all class data members private and construct public functions that allow us to access or modify the values of these data members.

Inheritance

An attribute called inheritance allows a child class object to inherit the properties of a parent class.

#include <iostream>
using namespace std;
class ParentClass {
  //data member
  public:
    int var1 =100;
};
class ChildClass: public ParentClass {
  public:
  int var2 = 500;
};
int main(void) {
  ChildClass obj;
}

Now, this object obj can make use of ParentClass’s properties, such as variable var1.

Polymorphism

Examples of polymorphism include operator and function overloading. A property known as polymorphism allows an item to respond differently depending on the circumstances.
Function overloading allows us to have multiple functions with the same name but various numbers, types, or combinations of arguments.

Polymorphism Example

#include <iostream>
using namespace std;
class Sum {
  public:
    int add(int num1,int num2){
      return num1 + num2;
    }
    int add(int num1, int num2, int num3){
      return num1 + num2 + num3;
    }
};
int main(void) {
   //Object of class Sum
   Sum obj;

   //This will call the second add function
   cout<<obj.add(10, 20, 30)<<endl;

   //This will call the first add function
   cout<<obj.add(11, 22);
   return 0;
}

Output:

60
33

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button