Skip to main content

Introduction to OOP

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of "objects". Objects are instances of classes, which can contain data in the form of fields, also known as attributes, and code in the form of procedures, known as methods. In OOP, computer programs are designed by making them out of objects that interact with one another.

Benefits of OOP

  • Reusability: OOP allows classes to be reused in the form of inheritance.
  • Data Redundancy: It is a way to bind the data and the functions that operate on them so that no other part of the code can access this data.
  • Code Maintenance: It makes software easier to maintain. Since the design is modular, part of the system can be updated in case of issues without a need to make large-scale changes.
  • Security: With data hiding, the programmers can build secure systems that can't be invaded by code in other parts of a program.

Basic Concepts of OOP

There are four major principles that make an language Object Oriented. These are:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation

Encapsulation is defined as wrapping up of data and information under a single unit. In C++, Encapsulation is defined as binding together the data and functions that manipulate the data.

class Adder{
// private data
private:
int sum;

public:
// constructor
Adder(int i = 0) {
sum = i;
}
// interface to outside world
void addNum(int number) {
sum += number;
}
// interface to get sum value
int getTotal() {
return sum;
}
};

Inheritance

Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which is defined in other class.

class Shape {
public:
void setWidth(int w){
width = w;
}
void setHeight(int h){
height = h;
}

protected:
int width;
int height;
};

// Derived class
class Rectangle: public Shape{
public:
int getArea(){
return (width * height);
}
};

Polymorphism

Polymorphism is a concept by which we can perform a single action in different ways. So, polymorphism means many forms. There are two types of polymorphism in C++: compile time polymorphism (Overloading) and runtime polymorphism (Overriding).

#include <iostream>
using namespace std;

class Shape {
protected:
int width, height;

public:
Shape( int a = 0, int b = 0) {
width = a;
height = b;
}
virtual int area() =0;
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
return (width * height);
}
};

Abstraction

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only important things to the user and hides the internal details, for example, sending SMS, you just type the text and send the message. You don't know the internal processing about the message delivery.

class Adder{
// private data
private:
int sum;

public:
// constructor
Adder(int i = 0) {
sum = i;
}
// interface to outside world
void addNum(int number) {
sum += number;
}
// interface to get sum value
int getTotal() {
return sum;
}
};

In conclusion, understanding these concepts and how they can be implemented in C++ is crucial for writing effective and efficient code. With practice, these concepts will become second nature and you'll be able to see the benefits of OOP in your work. Happy coding!