Object-Oriented Programming (OOP) is a programming paradigm that organizes and structures code around the concept of "objects," which are self-contained units containing both data and behavior. In OOP, objects represent real-world entities, and the programming revolves around interactions between these objects.
The core principles of Object-Oriented Programming are:
- Encapsulation.
- Abstraction.
- Inheritance.
- Polymorphism.
Class
Object
//C++ Example Code to show working of Class and Objects #include <iostream> #include <string> using namespace std; class Car { public: // Attributes string brand; string model; string color; string fuelType; // Methods void ChangeGear() { cout << "The " << brand << " " << model << " is changing Gear...\n"; } void Accelerate() { cout << "The " << brand << " " << model << " is accelerating...\n"; } void Brake() { cout << "The " << brand << " " << model << " is braking...\n"; } }; int main() { // Creating car objects using the Car class Car car1; car1.brand = "Toyota"; car1.model = "Camry"; car1.color = "Blue"; car1.fuelType = "Petrol"; car1.ChangeGear(); car1.Accelerate(); car1.Brake(); return 0; }
The Toyota Camry is changing Gear... The Toyota Camry is accelerating... The Toyota Camry is braking...
The memory required to hold the object's data (attributes) and the code for its methods (member functions) is allocated from the computer's memory.(alert-success)
I hope till now you have understood the two most important terms of OOPs that is class and object. Now we are going to explore the core principles of OOPs one by one.
Encapsulation
Encapsulation is the concept of bundling data and methods within a class, hiding internal implementation details from the outside world. It allows for data abstraction and provides access control through public, private, and protected access specifiers. You can read more about Access Specifiers here.
Example: In the Car class, the attributes (make, model, year) may be declared as private, ensuring that they can only be accessed and modified through public methods like getMake() and setMake(). This way, the internal state of the car object is encapsulated and protected.
Example Code:
#include <iostream> #include <string> using namespace std; class Car { private: string make; string model; int year; public: // Constructor Car(string carMake, string carModel, int carYear) { make = carMake; model = carModel; year = carYear; } // Getter methods string getMake() const { return make; } string getModel() const { return model; } int getYear() const { return year; } // Setter methods void setMake(string newMake) { make = newMake; } void setModel(string newModel) { model = newModel; } void setYear(int newYear) { year = newYear; } }; int main() { // Creating a Car object using the constructor Car myCar("Toyota", "Camry", 2022); // Using the setter methods to modify attributes myCar.setMake("Honda"); myCar.setModel("Civic"); myCar.setYear(2021); // Displaying the updated car details cout << "\nUpdated Car Details:" << endl; cout << "Make: " << myCar.getMake() << endl; cout << "Model: " << myCar.getModel() << endl; cout << "Year: " << myCar.getYear() << endl; return 0; }
Updated Car Details:
Make: Honda
Model: Civic
Year: 2021
Access specifiers are keywords used in class definitions to control the visibility and accessibility of class members (attributes and methods) from outside the class. (alert-success)
Abstraction
Inheritance
Inheritance allows for code reuse, as the derived class can reuse the properties and behaviors of the base class, eliminating the need to rewrite common code.(alert-success)
They are classified into various types based on the hierarchy and relationships between classes and these are:
- Single Inheritance: A derived class inherits from only one base class.
- Multiple Inheritance: A derived class can inherit from two or more base classes.
- Multilevel Inheritance: A derived class inherits from another derived class, creating a chain of inheritance.
- Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
- Hybrid (or Virtual) Inheritance: A combination of multiple and multilevel inheritance.
No comments:
Post a Comment