Introduction to Object-Oriented Programming
Object-Oriented Programming makes it easier to create modular, reusable, and maintainable software.

As building software grows in complexity, Object-Oriented Programming (OOP) has become the dominant paradigm in software development for building scalable products. OOP provides a way to organize and manage complex codebases by encapsulating data and behavior into objects, which can interact with each other to perform complex tasks. In this beginner's guide, we will cover the fundamentals of Object-Oriented Programming, including its principles, concepts, and terminologies with examples!
Authored by: Karan Jagtiani

Karan is a Software Engineer with 1.5 years of experience working with various technologies across different domains. He currently works for HackerRank as a Software Engineer, specifically, as Backend & DevOps engineer. If you want to connect with him or get to know him better, you can visit his website: https://karanjagtiani.com
Introduction
Object-Oriented Programming (OOP) is a programming paradigm that relies on the concept of Objects & Classes. With the help of this core principle, designing code and building software becomes much easier. Object-Oriented Programming makes it easier to create modular, reusable, and maintainable software.

Concepts of Object-Oriented Programming
In addition to the principles of OOP, there are several concepts that are fundamental to the paradigm:
Class
A Class is like a blueprint or a template for creating objects that define what data the Object can hold (i.e. its properties), as well as the actions that the Object can perform (i.e. its methods).
Object
An object is an instance of a class, which has its own set of data and behavior. It represents a real-world object and can interact with other objects.
Method
A method is a function defined within a class that defines the behavior of the object. It can modify the object's data or perform actions on it.
Interface
An interface is a set of methods defined for an object, which allows other objects to interact with it. It defines how other objects can interact with an object.
Constructor
A constructor is a special method that is called when an object is created, allowing the object to be initialized with specific values.
Principles of Object-Oriented Programming
Object-Oriented Programming (OOP) is based on four fundamental principles, which are Encapsulation, Inheritance, Polymorphism, and Abstraction. Let's explore each of these principles in detail with examples.
Encapsulation
Encapsulation is the concept of bundling data (attributes) and behavior (methods) within a single entity (object) and restricting access to the internal details of that object. In other words, it refers to the process of hiding the implementation details of an object and exposing only the necessary functionality. The encapsulation principle helps to ensure that the object's data is protected and cannot be accidentally modified by other parts of the program.

For example, let's consider a class named "BankAccount". This class may contain private data such as "accountNumber", "balance", and "interestRate", and behavior such as "deposit()", "withdraw()", and "calculateInterest()".
Encapsulation allows us to protect the data of the bank account object, so that it can only be accessed through well-defined methods, such as "getBalance()" and "deposit()". This ensures that the internal state of the bank account is maintained correctly and prevents unauthorized access to its data.
Here is a C++ code explaining the above example:
#include <iostream>
using namespace std;
class BankAccount
{
private:
int accountNumber;
double balance;
double interestRate;
public:
BankAccount(int accNo, double bal, double intRate)
{
accountNumber = accNo;
balance = bal;
interestRate = intRate;
}
double getBalance()
{
return balance;
}
void deposit(double amount)
{
balance += amount;
}
void withdraw(double amount)
{
balance -= amount;
}
double calculateInterest()
{
return balance * interestRate;
}
};
int main()
{
BankAccount myAccount(1234, 1000, 0.05); // Object instantiation
cout << "Balance: " << myAccount.getBalance() << endl;
myAccount.deposit(500); // Modify private attributes through exposed methods
cout << "Balance after deposit: " << myAccount.getBalance() << endl;
myAccount.withdraw(200); // Modify private attributes through exposed methods
cout << "Balance after withdrawal: " << myAccount.getBalance() << endl;
cout << "Interest earned: " << myAccount.calculateInterest() << endl;
return 0;
}
Inheritance
Inheritance is the process by which one class (the subclass) derives the properties and methods of another class (the superclass). Inheritance allows subclasses to reuse the code of their superclasses, thereby reducing code duplication and promoting code reuse. Subclasses can also add their own unique properties and behaviors to the inherited code.
For example, let's consider a class hierarchy consisting of a superclass named "Animal" and two subclasses named "Cat" and "Dog". The Animal class may contain properties such as "name" and "age", and methods such as "eat()" and "sleep()". The Cat subclass can inherit these properties and methods from the Animal class and add its own unique properties and behaviors, such as "meow()" and "climb()". Similarly, the Dog subclass can inherit from the Animal class and add its own unique properties and behaviors, such as "bark()" and "fetch()".
Here is a C++ code explaining the above example:
#include <iostream>
using namespace std;
// Superclass
class Animal
{
protected:
string name;
int age;
public:
Animal(string n, int a)
{
name = n;
age = a;
}
void eat()
{
cout << "Eating..." << endl;
}
};
// Subclass
class Cat : public Animal
{
public:
Cat(string n, int a) : Animal(n, a) {}
void meow()
{
cout << "Meow..." << endl;
}
};
// Subclass
class Dog : public Animal
{
public:
Dog(string n, int a) : Animal(n, a) {}
void bark()
{
cout << "Bark..." << endl;
}
};
int main()
{
Cat myCat("Whiskers", 3);
Dog myDog("Rufus", 5);
myCat.eat();
myCat.meow();
myDog.eat();
myDog.bark();
return 0;
}
Polymorphism
Polymorphism refers to the ability of objects to take on multiple forms. It allows different objects to be treated as if they were of the same type, as long as they share a common interface. Polymorphism enables code to be more modular and flexible, allowing it to be reused in different contexts.
For example, let's consider a class hierarchy consisting of a superclass named "Shape" and two subclasses named "Circle" and "Rectangle". The Shape superclass implements a virtual method for calculating the area, and both the Circle and Rectangle classes can have different implementations of the virtual method, which calculates the area of the shape. We can create a list of Shape objects, containing both Circle and Rectangle objects, and then iterate through the list, calling the "area()" method on each object. The code will be able to handle both Circle and Rectangle objects, even though they have different implementations of the "area()" method.
Here is a C++ code explaining the above example:
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double area()
{
return 0;
}
};
class Rectangle : public Shape
{
private:
double length;
double breadth;
public:
Rectangle(double l, double b)
{
length = l;
breadth = b;
}
double area()
{
return length * breadth;
}
};
class Circle : public Shape
{
private:
double radius;
public:
Circle(double r)
{
radius = r;
}
double area()
{
return 3.14 * radius * radius;
}
};
int main()
{
Shape *shape;
Rectangle rect(10, 5);
Circle cir(5);
// Polymorphism
shape = ▭
cout << "Area of rectangle: " << shape->area() << endl;
shape = ○
cout << "Area of circle: " << shape->area() << endl;
return 0;
}
Abstraction
Abstraction is the practice of focusing on the essential features of an object and ignoring the non-essential ones. This allows for the creation of simple and easy-to-use interfaces for complex systems.
For example, consider a car. A customer doesn't need to know how the engine works or how the transmission shifts gears. Instead, they only need to know how to start and drive the car.
In C++, abstraction can be achieved using abstract classes. An abstract class is a class that cannot be instantiated but can be inherited by other classes. It defines a set of methods that must be implemented by any class that inherits from it but does not implement those methods itself.
Here is an example of an abstract class in C++:
#include <iostream>
using namespace std;
class Vehicle
{
public:
virtual void start() = 0; // pure virtual method
virtual void stop() = 0; // pure virtual method
};
class Car : public Vehicle
{
public:
void start()
{
cout << "Starting..." << endl;
}
void stop()
{
cout << "Stopping..." << endl;
}
};
int main()
{
Car myCar;
myCar.start();
myCar.stop();
return 0;
}
In this example, the Vehicle class is an abstract class that defines two pure virtual methods: start() and stop(). These functions must be implemented by any class that inherits from Vehicle.
The Car class is a concrete class that inherits from Vehicle and implements the start() and stop() methods. By using abstraction in this way, the user of the Car class only needs to know how to start and stop the car, without needing to know the details of how those methods are implemented.
Conclusion
In conclusion, we have covered the fundamentals of Object-Oriented Programming, including its principles, concepts, and terminologies with examples. We hope that this beginner's guide has provided you with a solid foundation for understanding Object-Oriented Programming and that you are now equipped with the knowledge to begin building modular, reusable, and maintainable software.
Thank you very much for taking the time to read this blog post! I truly appreciate it. I enjoyed writing this post and I hope it provided you with useful information that you can apply in your daily life. In case you have any questions or comments, please do not hesitate to reach out to me. I would love to hear your feedback and opinions on this topic.
Additionally, if you are interested in learning more about me and my work, feel free to visit my website. You will find a broad range of resources and links to my social media profiles. I am always happy to connect with new people who share similar interests and passions, so please do not hesitate to get in touch with me.
Once again, thank you for reading, and stay tuned for more exciting content coming soon!
Important resources:
How Software is Developed? A Step-By-Step Guide
Construct Week at Masai: Building a Fastrack Website Clone
Understanding SDE Levels- SDE-1 vs SDE-2 vs SDE-3 Differences