What is OOP?

Introduction

  • Object oriented programming (OOP) is the principle of design and development programs using modular approach. So, in this blog post, I’m going to explain what is OOP.
  • The procedural programming focusses on processing of instructions order to perform a desired computation, it emphasizes more on doing things like algorithms. Used in a programming languages like c and pascal.
  • OOP combines both the procedures and the function that operate on the data into a single unit called the object. It follows bottom-up design techniques, it is piecing together the smaller system to give more complex systems.
  • Another significant element that assumes a vital role in object-oriented programming is the class. It is a template that represents a group of objects which share common properties and relationships.

C++ as object Oriented programming language

  • It is the object oriented, general – purpose programming language, derived from c language.
  • Existing code on c language can be used with C++, hence compatible. Even existing pre- complied libraries can be used with new C++, code.

Also, there is no additional cost for using C++, hence efficient.

Major improvements over C language

  1. Stream I/O
  2. Strong typing
  3. Inlining
  4. Default argument values
  5. Parameter passing by reference
  6. OOP advantages
classObject
• Animal• Dogs
• Cats
• Birds

Classes and Objects of OOP in C++

  • C++ is an Object Oriented programming language.
  • Everything in C++ is associated with classes and objects along with attributes and method.
  • Attributes and methods are the basically variables and functions that belongs to the class. These are usually known as “class members”.
  • A class is a user-defined data type that we can use in our program, and it work as an object constructor, or a “blueprint” for creating objects.

Creating the Class and methods

Class keyword can also be used to define a class and Methods are functions that belongs to the class. They defined as behavior/action taken by object.

Class MyClass{                       //Class
Public:                                    //specifier 
Void myMethod (){             // method declaration 
Cout<< “hello world!”;
}
};

Creating an object

  • In C++, an object is generated from class.
  • To create an object, provide the class name first, followed by the object name.
  • To reach the class attributes, apply the dot syntax (.) on the object.
MyClass myObj:        //create an object of MyClass 
MyObj.myNum = 4;
myObj.myString = “Hello”;

Access specifiers

Access specifiers determine how a class members (attributes and methods) can be accessed. C++ offers a set of three access specifiers.

  • Public – External access to members is allowed.
  • Private – members cannot be accessed externally from the class.
  • Protected – members cannot be accessed externally from the class, however, they can be accessed in inherited classes.

By default, all members of a class are private if you don’t specify an access specifier, they will be private.

Class MyClass {
public:                    // Public access specifier 
int y:_                      //public attribute 
private:                 // Private access specifier 
int x;                        // Private attribute 
};
int main() {
 MyClass myObj;
myObj.y=5;                //Allowed (public) 
myObj.K= 5;              // Not allowed (private)
 return 0; 
}

What is OOP principles

Object oriented programming is a methodology characterized by following concepts:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Encapsulation

The process of binding data members (variable, properties) and member functions(methods)into a single unit. As encapsulation is the hiding process of data members and functions so we use variables as private but if we want to access these private attributes in another class we use getter and setter methods.

Real life example of Encapsulation

  • Take an example of a pharmacy store.
  • You go to the shop and ask for a medicine.
  • There are only the chemist has the access to the medicines of the store and he knows what medicine to give you.
  • This reduce the risk of taking a medicine that is not prescribed to you.
  • Here medicines are the member variables, chemist is the member function and you are the external piece of code.
class Employee {
  private:
                // Private attribute
    int salary;

  public:
    // Setter
    void setSalary(int s) {
      salary = s;
    }
    // Getter
    int getSalary() {
      return salary;
    }
};

int main() {
  Employee myObj;
  myObj.setSalary(5000);
  cout << myObj.getSalary();
  return 0;
}

Abstraction

It signifies the representation of essential traits without going into deeper explanations. Data abstraction is a programming techniques that relies on the separation of interface and implementation.

Take an example of your laptop

  • When you press a key on keyword, the characteristics on the screen.
  • You need to know only this.
  • How exactly it works is not required. This is called abstraction.
class implementAbstraction {
private:
    int a, b;
public:
    // method to set values of
    // private members
    void set(int x, int y)
    {
        a = x;
        b = y;
    }
    void display()
    {
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
    }
};
int main()
{
    implementAbstraction obj;
    obj.set(10, 20);
    obj.display();
    return 0;
}

Inheritance

  • The mechanisms of deriving a new class from an old class is called inheritance.
  • The old class is known as base class(parent class) while the new class is known as derived class(child class) or subclass.
  • The inheritance is the strongest or dynamic feature of oop.
  • Through effective use of inheritance you can save of time in your programming.
  • It also reduce errors . Consequently, this will lead to an enhancement in the quality of work and productivity.
  • Inheritance allows us to utilize the attributes and methods from an already-existing class.
  • Inheritance can be categorized into:
    • Single
    • Multiple
    • Multilevel
    • Hybrid types
// Base class
class Student {
  public:
    string name= "Ali";
    void printInfo {
      cout << what is your name \n" ;
    }
};

// Derived class
class person: public Student {
  public:
    int age = 12;
};

int main() {
Student student;
  myCar.printInfo();
  cout <<student.name + " " +student.age;
  return 0;
}


Polymorphism

Polymorphism in C++ refers to the ability of different classes to be treated as instances of a common base class. It allows you to write code that can work with objects of various derived classes through a common interface.

example

A +” is used to add two numbers, but it can also be used to concatenate two strings. This is known as operator overloading because same operator may behave differently on different instances

// Base class
class Animal {
  public:
    void animalSound() {
      cout << "The animal makes a sound \n";
    }
};

// Derived class
class Cat : public Animal {
  public:
    void animalSound() {
      cout << "The cat says: meow \n";
    }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
      cout << "The dog says: bow wow \n";
    }
};

int main() {
 Animal myAnimal;
  Cat myCat;
  Dog myDog;

  myAnimal.animalSound();
  myCat.animalSound();
  myDog.animalSound();
  return 0;
}

Note…

If you are using Microsoft Visual Studio code or community then simply clone our GitHub Repository and start your project.

Implementation of OOP Principles in Code

The code illustrates several principles of Object-Oriented Programming (OOP) in C++, specifically encapsulation, inheritance, and polymorphism.

#include <iostream>
#include <string>
using namespace std;

// Encapsulation
class Add {
protected:  //Access Specifiers
    int num;
    int num2;
public:
   int getNum() {
        return num;
    }
  void setNum(int num,int num2) {
       this->num = num;
      this-> num2 = num2;
 }
 
 int Sum() {
        return num + num2;
    }
  
};

// inheritance 
class multiply : public Add {
public:
    int mul() {
    return num * num2;

    }
};

// polymorphism 
class multiplication : public multiply {
public:
    int mul(int num3) {   //using the same method name
        return num * num2 *num3;
    }
};


int main() {
    Add obj;
   multiply obj2;
   multiplication obj3;

    obj.setNum(2, 3);
    cout << "Sum: " << obj.Sum()<<endl;
    obj2.setNum(2, 3);
    cout << "multiplication of numbers : " <<obj2.mul() << endl;
    obj3.setNum(2, 3);
    cout << "multiplication of 3 numbrs : " <<obj3.mul(2);

    return 0;
}