Object-Oriented Programming(OOP) in C++

OOP in C++

The key update of the C++ programming language was the addition of the OOPS concept to the already powerful C Programming Language.

Advantages of OOP

  • Fast and easier to execute
  • Provides a clear structure for the code
  • Helps to keep the C++ code DRY(“Don’t Repeat Yourself”)
  • DRY makes the code easier to modify, maintain and debug
  • Possible to create reusable applications with less code with shorter development time

Concepts that form the base of object-oriented programming:

Object

An Object is an instance of a class and is also know as the basic component of object-oriented programming(That is data and function that operate on data are tied up as a unit called an object). When a class is defined, no memory is allocated but when an object is created only then the memory is allocated.

#include <bits/stdc++.h>
using namespace std;
class GeeksGod  // class class_name
{
    public:
    string name;
    void printname()
    {
       cout << "Name is: " << name;
    }
};  // class ended with a semicolon
  
int main() 
{
    GeeksGod obj1;    // Declare an object of class GeeksGod
    obj1.name = "Mayank Mandhana";   // accessing data member
    obj1.printname();   // accessing member function
    return 0;
}

Class

The building block of C++ that leads to Object-Oriented programming is a Class. A class is a user-defined data type or data structure that is declared with the keyword class, that has data members and member functions as its members. Features of classes are as follows:

  • Class is defined between the curly brackets and terminated by a semicolon
  • Data members and member functions are contained in a class
  • Data variables and functions used to operate these variables and together these data members and member functions define the properties and behavior of the objects in a Class
  • In the example of the Objects as defined above, “class GeeksGod”, the class refers to the keyword and GeeksGod is the class name

Abstraction

Data Abstraction refers to providing only the necessary information of the data and hiding their implementation and background details from the outside world, i.e., to show only the needed information in the program without showing the details. For example, a database system hides certain details of how data is stored, created and maintained.

Features of Data Abstraction are:

  • Class internals get protected from unintentional user-level errors
  • The coder do not have to write the low-level code
  • Code duplicity is avoided
  • Code reuse and proper partitioning across classes
  • It permits internal implementation characteristics to be changed without affecting the users
#include <iostream>
using namespace std;
class Abstraction
{
    private:
        int n1, n2;
    public:
        void set(int a, int b)
        {
            n1 = a;
            n2 = b;
        }          
        void displayn()
        {
            cout<<"n1 = " << n1 << endl;
            cout<<"n2 = " << n2 ;
        }
};
int main() 
{
    Abstraction obj;
    obj.set(1, 2);
    obj.displayn();
    return 0;
}

Encapsulation

Encapsulation is an Object-Oriented Programming concept that binds the data and functions together that has the capability to manipulate the data and keeps them safe from outside interference and misuse.

#include<iostream>
using namespace std;
class Encap
{
private:
  // entity outside this class cannot access these data members directly
   int num;
   char ch;
public:
   int getNum() 
   const 
   {
      return num;
   }
   char getCh() const 
   {
      return ch;
   }
   void setNum(int num) 
   {
      this->num = num;
   }
   void setCh(char ch) 
   {
      this->ch = ch;
   }
};
int main()
{
   Encap obj;
   obj.setNum(1);
   obj.setCh('GeeksGod');
   cout<<obj.getNum()<<endl;
   cout<<obj.getCh();
   return 0;
}

OUTPUT
------
1
GeeksGod

Inheritance

In C++, it is possible to inherit attributes from one class to the other class. The capablity of a class to derive resources and characteristics from another class is called Inheritance. One of the most useful aspects of object-oriented programming is code reusability and helps to reduce the code size.

As the name suggests Inheritance is the process of forming a new class from an existing class called a base class, the new class formed is called a derived class.

Syntax:

class Subclass_name : access_mode Superclass_name

Example:

class humans
{ 
    public:
    int legs = 2;
};


class hands : public humans // hands class inheriting humam class
{ 
    public:
    int tail = 2;
};

int main()
{
    humans d;
    cout << d.legs;
    cout << d.hands;
}

OUTPUT
------
2 2

Polymorphism

As the name suggests, it simply means more than one form. Polymorphism is the capability to use an operator or function in multiple ways, i.e. a function or an operator functioning in many ways differently according to the usage.

class Plant // Base Class
{
  public:
    void plantcolor() 
  {
    cout << "The colors of plant are: \n" ;
  }
};

class Grass : public Plant // Derived class
{
  public:
    void plantcolor() 
  {
    cout << "The color of the grass is: Green \n" ;
  }
};

class Phantom_Orchid : public Plant // Derived class
{
  public:
    void plantcolor() {
    cout << "The color of Phantom Orchid is White \n" ;
  }
};
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?