Object-Oriented Programming (OOP) in C++
Introduction
Object-Oriented Programming (OOP) is a programming approach in which a program is organized around objects and classes rather than only functions and procedures.
C++ is an object-oriented programming language that supports the major concepts of OOP.
Main Concepts of OOP
- Class: A blueprint or template for creating objects.
- Object: An instance of a class.
- Encapsulation: Wrapping data and functions together in a class.
- Abstraction: Hiding unnecessary implementation details.
- Inheritance: Creating a new class from an existing class.
- Polymorphism: Allowing the same function or interface to behave differently in different situations.
1. Class
A class is a user-defined data type that contains data members and member functions.
Example
#include <iostream> #include <string> using namespace std; class Student { public: string name; int age; void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } };
2. Object
An object is an instance of a class. It is used to access the members of that class.
Example
int main() { Student s1; s1.name = "Anuradha"; s1.age = 22; s1.display(); return 0; }
Output
Name: Anuradha Age: 22
3. Encapsulation
Encapsulation means binding data and functions together inside a class and controlling access to the data.
C++ provides three access specifiers:
- public: Members can be accessed from outside the class through an object.
- private: Members can be accessed directly only within the class and its friends.
- protected: Members can be accessed within the class, its friends, and derived classes, subject to access rules.
Example
#include <iostream> using namespace std; class Account { private: int balance = 0; public: void setBalance(int b) { balance = b; } int getBalance() { return balance; } }; int main() { Account a; a.setBalance(5000); cout << "Balance: " << a.getBalance(); return 0; }
Output
Balance: 5000
Here, balance cannot be accessed directly from outside the class because it is declared as private. The public member functions provide controlled access to it.
4. Abstraction
Abstraction means hiding implementation details and showing only the necessary information to the user.
For example, when we use an ATM, we select options such as withdraw or deposit without needing to understand the internal implementation.
Example
#include <iostream> using namespace std; class Calculator { public: void add(int a, int b) { cout << "Sum = " << a + b; } }; int main() { Calculator c; c.add(10, 20); return 0; }
Output
Sum = 30
The user only needs to call add() without knowing the internal implementation of the addition operation.
5. Inheritance
Inheritance allows one class to acquire accessible properties and functions from another class.
- Base class: The parent class from which properties and functions are inherited.
- Derived class: The child class that inherits from the base class.
Example
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Animal is eating" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking" << endl; } }; int main() { Dog d; d.eat(); d.bark(); return 0; }
Output
Animal is eating Dog is barking
The Dog class inherits the eat() function from the Animal class. Therefore, an object of Dog can call both eat() and bark().
6. Polymorphism
Polymorphism means "many forms." It allows the same function name or interface to perform different tasks depending on the arguments or the object involved.
The two major types of polymorphism are:
6.1 Compile-Time Polymorphism
Compile-time polymorphism is resolved by the compiler. Common examples include:
- Function overloading
- Operator overloading
Example: Function Overloading
#include <iostream> using namespace std; class Calculator { public: int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; int main() { Calculator c; cout << c.add(10, 20) << endl; cout << c.add(10, 20, 30); return 0; }
Output
30 60
Here, two functions have the same name, add(), but accept different numbers of arguments. The compiler selects the appropriate function based on the arguments supplied.
6.2 Runtime Polymorphism
Runtime polymorphism can be achieved using function overriding and virtual functions.
Example
#include <iostream> using namespace std; class Animal { public: virtual void sound() { cout << "Animal makes a sound" << endl; } virtual ~Animal() = default; }; class Dog : public Animal { public: void sound() override { cout << "Dog barks" << endl; } }; int main() { Animal* a; Dog d; a = &d; a->sound(); return 0; }
Output
Dog barks
The sound() function is declared virtual in the base class. When the base-class pointer a points to a Dog object, the overridden Dog::sound() function is called at runtime.
Advantages of OOP
- Reusability: Existing code can be reused through inheritance and composition.
- Security: Encapsulation helps control access to data.
- Maintainability: Programs can be easier to modify and maintain.
- Flexibility: Polymorphism supports flexible program design.
- Modularity: Large programs can be divided into classes and objects.
- Easier debugging: Problems can often be isolated within specific classes or components.
Summary
The main concepts of Object-Oriented Programming in C++ are:
Object-Oriented Programming (OOP) │ ├── Class ├── Object ├── Encapsulation ├── Abstraction ├── Inheritance └── Polymorphism
These concepts form the foundation of Object-Oriented Programming in C++. Understanding them helps developers design programs that are organized, reusable, and easier to maintain.