Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and programs. It emphasizes the use of classes and objects, encapsulation, inheritance, and polymorphism to create reusable and modular code.
The basic principles of OOP are:
A class in C++ is a blueprint for creating objects. It defines a type by bundling data and methods that operate on that data. For
An object is an instance of a class. It contains actual values for the attributes defined in the class.
Encapsulation is the bundling of data and methods that operate on the data into a single unit called a class, restricting access to some of the object's components. It is implemented in C++ using access specifiers: private
, protected
, and public
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. In C++, it is implemented using abstract classes and interfaces (pure virtual functions).
Inheritance is a feature of OOP that allows a class to inherit properties and methods from another class. Types of inheritance in C++ include:
C++ does not have a super
keyword like some other languages. Instead, the base class methods can be accessed using the scope resolution operator (::
).
Polymorphism is the ability of different objects to respond, each in its own way, to identical messages (function calls). Types of polymorphism in C++ include:
Constructors: Special member functions that are called when an object is instantiated. They initialize the object. Constructors have the same name as the class and no return type.