Advanced C++ Programming and Algorithms

OOPs (Object Oriented Programming) in C++

OOPs (Object Oriented Programming) in C++

Introduction

In the C language, our program was organised around logic and functions, but in the object-oriented programming language, C++, our program is organised about objects rather than logic and functions. By dividing data into two memory blocks, namely data and functions, this method makes the code modular and versatile. The primary focus of object-oriented programming is on classes and objects. Data is modelled in OOPs as objects with properties and functionalities. We shall discover the many OOPs ideas in C++ in this blog.

In this article we are covering the foundational ideas of OOPs that serve as its building pieces.

Class

You may think of a class as a container for functions and data variables.

A class is an object-only data type that is specified by the user and holds member functions and variables together. It is a template by which objects will be created and they will all showcase the features of the class definition.

Object

The class's instances are objects.

Classes are used to build objects. The same process used to declare variables also applies to declaring objects. A real-world entity that physically exists in the world and has some behaviour and state is referred to as an object. Eg. chair, a table, a dog, a person, a car, etc.

class Car
{
    //Data members
    char name[20];
    int speed;
    int weight;

public:
    // Member Functions
    void brake(){
    }
    void slowDown(){
    }
};

int main()
{
  Car ford; //object of class Car
}

Abstraction

It is a way of shielding the user from unimportant information like the implementation.

Consider a scenario where someone is downloading and uploading files. He is aware of the appropriate alternatives to select. He will click the appropriate button to begin uploading or downloading the material. Your phone will display if the file has been downloaded or uploaded. As it is irrelevant to you, what occurs on the server when you press send is concealed from you. When someone clicks upload or download, just the section the user wants to view is displayed; the background activity that occurs is concealed from him.

Encapsulation

keeping the information and functionality in a single, capsule-like entity.

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

Inheritance

A derived class inherits all the data members and functions from a base class.

The method through which a relationship exists between two base classes and a derived class is known as inheritance. The base class's attributes and characteristics are transferred to the derived class. The class from which the characteristics are derived is known as the base class, and the class from which the features are inherited is known as the derived class. The classes Male and Female, for instance, are derived classes from the basic class HumanBeing.

#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;
}

Polymorphism

Redefining function behaviours or providing alternate function behaviours is Polymorphism

Polymorphism redefines how something functions by either altering how it is carried out or by altering the components used to carry it out. A person is capable of juggling many roles at once. A woman simultaneously serves as a mom, sister, wife, and worker. Polymorphism is the term for this. Two crucial ideas are included in polymorphism.

  1. Operator Overloading: Operator overloading occurs when an operator behaves differently in various situations.
  2. Function Overloading: Function overloading refers to when a function exhibits diverse behaviour in various contexts. This implies that a function with the same name in two distinct classes will provide two entirely different results.

#include <iostream>
using namespace std;

// Member Function with two int parameters
int sum(int num1, int num2) {
    return num1 + num2;
}

// Member  Function with two double parameters
double sum(double num1, double num2) {
    return num1 + num2;
}

// Member Function with three int parameters
int sum(int num1, int num2, int num3) {
    return num1 + num2 + num3;
}

int main() {
    // Call function with two int parameters
    cout<<sum(3, 4) << endl;

    // Call function with two double parameters
    cout<<sum(3.4, 6.6) << endl;

    // Call function with three int parameters
    cout<<sum(9, 3, 8) << endl;

    return 0;
}

write your code here: Coding Playground

As you can see, the program uses the same function sum(), but it takes a different amount of parameters and various types of inputs. Two integers make up the first sum(), two double numbers make up the second sum(), and three integers make up the third sum().

Advantages of OOPs

  1. OOPs enable code reuse and increase the use of preexisting classes.
  2. Because classes and objects are used in OOPs, code maintenance is simple and doesn't need any reorganisation.
  3. Additionally, it aids in data concealment, protecting the data and information from leakage or exposure.
  4. Implementing object-oriented programming is simple.

You should now have a better understanding of the necessity for OOP development while handling medium-to-large scale projects. In this blog we went over the fundamentals of OOPs, such as polymorphism, inheritance, encapsulation, etc., which are the building blocks for the C++ OOP architecture. You also learnt about polymorphism and inheritance examples, as well as the benefits of C++ OOPs.