C ++ OOPs (Object-Oriented Programming) Concepts
OOPs (Object-Oriented Programming System) in C++ is a programming paradigm that revolves around the concept of objects.
OOPs in C++ allow you to define classes, create objects, and implement the core OOP concepts like Abstraction, Encapsulation, Inheritance, and Polymorphism.
OOPs (Object Oriented Programming System)
- Objects
- Classes
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Objects
- In OOP, an object is a self-contained unit that represents a real-world entity or concept.
- These objects are created from classes, which define their structure and behavior.
- In C++, you can create objects from classes using the following syntax:
1ClassName objectName; // Creates an object of the class ClassName
2
Classes
- In C++, a class is a template for creating objects (instances).
- Classes define the structure and behavior of objects that will be created based on that class.
- In C++, the syntax for defining a class is as follows:
1class ClassName {
2public:
3 // Public members and methods go here.
4
5private:
6 // Private members and methods go here.
7
8protected:
9 // Protected members and methods go here.
10};
- class: This keyword is used to declare the beginning of a class definition.
- ClassName: Replace this with the desired name you want to give to your class.
- It's common to use CamelCase notation for class names, such as MyClass or PersonDetails.
- public, private, and protected: These are access specifiers that control the visibility and accessibility of class members (data members and member functions).
- public: Members declared under the public section are accessible from outside the class.
- private: Members declared under the private section are not accessible from outside the class.
- protected: Members declared under the protected section are accessible within the class and its derived classes.
- // Comments: These are comments that can be used to provide explanations or documentation for the class, its members, or methods.
- A simple example of a class definition in C++
1#include <iostream>
2using namespace std;
3
4// Define a class called 'Person'
5class Person {
6public:
7 // Data members
8 string name;
9 int age;
10
11 // Member function to display person's information
12 void displayInfo() {
13 cout << "Name: " << name << endl;
14 cout << "Age: " << age << " years" << endl;
15 }
16};
17
18int main() {
19 // Create an object of the 'Person' class
20 Person person1;
21
22 // Initialize object's members
23 person1.name = "Alice";
24 person1.age = 30;
25
26 // Access and display object's members
27 person1.displayInfo();
28
29 return 0;
30}
Abstraction
- Abstraction is about simplifying things by focusing on what's important and ignoring the details that aren't crucial.
- Abstraction helps us understand and work with complex things more easily.
- Example: When you drive a four wheeler, you don't need to know how the engine works. You just use the brake, and steering wheel to control it.
- We use functions and classes to abstract complex operations. For instance, a "calculate tax" function hides all the tax calculation details.
Encapsulation
- Encapsulation is like putting data and the code that works on it into a box, hiding the inner workings.
- Encapsulation keeps data safe and organized, and it lets you control how data is accessed and modified.
- Example: A vending machine encapsulates snacks inside. You can't directly grab snacks, you need to use buttons, and the machine handles the rest.
Inheritance
- Inheritance is when a new class can inherit properties and behaviors from an existing class.
- Inheritance lets you reuse code, create hierarchies, and build more specific things from more general ones.
- Example: You have a "Vehicle" class. From that, you create subclasses like "Car" and "Bicycle." Both cars and bicycles have wheels, but cars have engines too.
Polymorphism
- Polymorphism means different things can respond to the same action in their way.
- Polymorphism allows flexibility and simplifies code. You can treat different things in a similar way.
- Example: You have a "Play" button on various devices—a TV, a phone, and a computer. Pressing "Play" does different things, but you use the same action.
These concepts are the building blocks of programming, helping you create organized, flexible, and understandable code that can do a wide range of tasks efficiently.
Why is C++ a partial OOP?
C++ is often referred to as a "partial" or "multi-paradigm" OOP language because it supports multiple programming paradigms, including both procedural and object-oriented programming.
Here are some reasons why C++ is considered "partial" OOP:
- Legacy Support: C++ was developed as an extension of the C programming language, which is primarily procedural. As a result, C++ retained many of the procedural programming features from C.
- Backward Compatibility: C++ values backward compatibility with C, which means that C code can be integrated seamlessly into C++ programs.
- Hybrid Approach: C++ allows developers to mix both procedural and object-oriented programming styles within the same program.
- This flexibility is useful when transitioning from a procedural codebase to an object-oriented one.
Advantages of OOPS
Code Reusability
- The ability to reuse code is one of OOP's key benefits.
- Once you've defined a class, you can create multiple objects from it, reducing the need to write repetitive code.
- This promotes efficiency and minimizes errors.
Modularity
- OOP encourages the organization of code into modular components.
- Each class represents a self-contained module, making it easier to manage and maintain the codebase.
Maintainability
- Maintaining an OOP-based codebase is more straightforward because changes made to one class do not necessarily affect others.
- This reduces the risk of unintended consequences when modifying code.