Object-Oriented Programming or OOPs is a way of programming where we organize programs using classes and objects. C++ supports OOPs, which makes it easier to represent real-life things in a program and reuse code.
The main concepts of C++ OOPs are:
- Object
- Class
- Inheritance
- Abstraction
- Encapsulation
- Polymorphism
Object
An object is a real-world thing that we can represent in a program. An object has data and behavior. For example, a Car object can have data like color and speed. It can also perform actions like start() and stop(). In C++, an object is created from a class.
Class
A class is a blueprint or template used to create objects. It defines the data and functions that an object can have. For example, Car can be a class, while your actual car can be an object of the Car class.
class Car {
public:
string color;
void start() {
cout << "Car is starting";
}
};
Here, Car is a class that contains data and a function.
Inheritance
Inheritance allows one class to use the data and functions of another class. For example, a Dog class can inherit common features from an Animal class.
Animal
↓
Dog
Here, Animal is the base class and Dog is the derived class. Inheritance helps us reuse existing code instead of writing the same code again.
Abstraction
Abstraction means showing only the important information and hiding unnecessary details. For example, when you drive a car, you use the steering wheel, brakes, and accelerator. You do not need to know how the engine works internally. In C++, abstraction helps us focus on what an object does without worrying about all its internal details.
Encapsulation
Encapsulation means keeping data and functions together inside a class and controlling access to that data. For example, a BankAccount class can contain account data and functions such as deposit() and withdraw() in one place. C++ uses access specifiers such as private, protected, and public to control access to class members.
Polymorphism
Polymorphism means many forms. It allows the same function or operation to behave differently in different situations. For example, different animals can have different versions of a sound() function:
Dog → Bark
Cat → Meow
Cow → Moo
So, the same function name can be used for different behaviors.
These concepts are the foundation of C++ Object-Oriented Programming. In the upcoming chapters, we will understand Inheritance, Polymorphism, Encapsulation, and Abstraction in more detail with simple C++ examples.
Key Points to Remember
- OOPs stands for Object-Oriented Programming.
- Class is a blueprint used to create objects.
- Object represents a real-world entity in a program.
- Inheritance helps reuse features of an existing class.
- Abstraction hides unnecessary details.
- Encapsulation keeps data and functions together.
- Polymorphism allows the same function or operation to have different forms.
What’s next?
In the next chapter, we will learn about Classes and Objects in C++, which are the basic building blocks of OOPs.
Frequently Asked Questions (FAQs)
Q1. What is C++ OOPs?
C++ OOPs is a programming approach that organizes code using classes and objects. It helps represent real-world entities and supports code reuse and better program organization.
Q2. What are the main OOPs Concepts in C++?
The main OOPs Concepts in C++ are classes, objects, inheritance, abstraction, encapsulation, and polymorphism. These concepts form the foundation of C++ Object-Oriented Programming.
Q3. What are C++ Classes and Objects?
C++ Classes and Objects are closely related. A class is a blueprint that defines data and functions, while an object is an actual instance created from that class.
Q4. What is inheritance in C++ OOPs?
Inheritance allows a derived class to use the data and functions of an existing base class. It supports code reuse and is one of the important C++ OOPs Concepts.
Q5. How does encapsulation work in C++?
Encapsulation keeps related data and functions together inside a class and controls access using private, protected, and public access specifiers.
Q6. What is polymorphism in C++ Object-Oriented Programming?
Polymorphism means “many forms.” In C++ Object-Oriented Programming, it allows the same function or operation to have different behavior in different situations.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
