Object-Oriented Programming (OOP) is a programming paradigm that focuses on
organizing code using objects, which encapsulate data and behavior. C++ supports
OOP principles, making it a powerful language for building modular, reusable, and
maintainable software.
1. Core OOP Principles
1.1 Encapsulation
Encapsulation binds data (variables) and functions (methods) that manipulate the
data into a single unit, called a class. Access to the data is restricted through
access specifiers.
1.2 Inheritance
Inheritance allows a class (child) to acquire the properties and behaviors of
another class (parent). It promotes code reuse and establishes a relationship
between classes.
1.3 Polymorphism
Polymorphism enables functions or methods to behave differently based on the
context. It is achieved using function overloading, operator overloading, and
inheritance.
1.4 Abstraction
Abstraction hides the implementation details of a system and exposes only the
essential features.
, 2. Classes and Objects
2.1 Class
A class is a blueprint for creating objects. It defines the properties (attributes) and
behaviors (methods) of objects.
Syntax:
class ClassName {
public:
// Attributes
// Methods
};
Example:
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
2.2 Object
An object is an instance of a class.
Example:
Car car1;
car1.brand = "Toyota";
car1.year = 2020;
car1.display(); // Output: Brand: Toyota, Year: 2020