



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Object-Oriented Programming (OOP) is a paradigm that structures programs around objects, rather than actions and data rather than logic. It's a fundamental approach in C++ that promotes modularity, reusability, and maintainability.
I. Introduction to OOP
โ Definition: A programming paradigm based on the concept of "objects", which can contain data (attributes) and code (methods). โ Core Idea: Modeling real-world entities or concepts into software components. โ Key Pillars: Encapsulation, Inheritance, Polymorphism, Abstraction.
A. Classes and Objects
โ Class: โ Definition: A blueprint or template for creating objects. It defines the structure (data members) and behavior (member functions) that objects of that class will have. โ Conceptual Visual: Imagine a "Car Blueprint" defining properties (color, model, speed) and actions (start, stop, accelerate). โ Example Code: class Car { public: std::string color; // Data member std::string model; // Data member
void accelerate() { // Member function // Code to make car accelerate } };
โ Object: โ Definition: An instance of a class. It's a concrete entity created from the class blueprint, occupying memory and having its own set of data members. โ Conceptual Visual: From the "Car Blueprint," you build actual cars: a "Red Sedan" (object 1), a "Blue SUV" (object 2), each with its own color and model. โ Example Code:
Car mySedan; // mySedan is an object of the Car class mySedan.color = "Red"; mySedan.model = "XYZ"; mySedan.accelerate();
II. Pillars of OOP
A. Encapsulation
โ Definition: The bundling of data (attributes) and the methods (functions) that operate on that data within a single unit (class). It also involves data hiding , restricting direct access to some of an object's components. โ Pragmatic Tip: Use private access specifier for data members and public methods (getters/setters) to control access. โ Key Benefits: โ Data Hiding: Protects data from external, unauthorized modification. โ Modularity: Makes code easier to manage and modify. โ Flexibility: Allows internal implementation changes without affecting external code. โ Conceptual Visual: Imagine a pill capsule containing medicine (data) and a coating (methods) that controls how the medicine is released. You can't directly touch the medicine. โ Example Code: class BankAccount { private: double balance; // Encapsulated data public: void deposit(double amount) { // Public method to access/modify balance if (amount > 0) balance += amount; } double getBalance() const { // Public method to read balance return balance; } };
B. Inheritance
โ Definition: A mechanism where one class (child/derived class) acquires the properties and behaviors of another class (parent/base class). โ Relationship: "Is-A" relationship (e.g., a Dog is a Animal).
โ Definition: Achieved using virtual functions and pointers/references to base classes. Decisions are made at runtime. โ Virtual Functions: A member function declared with the virtual keyword in the base class, intended to be overridden in derived classes. โ Abstract Class: A class with at least one pure virtual function (virtual void func() = 0;). Cannot be instantiated directly. โ Conceptual Visual: Imagine a "Shape" (base class) with a draw() virtual function. A "Circle" and "Square" (derived classes) override draw(). At runtime, calling draw() on a Shape* variable correctly calls the draw() of the actual object (Circle or Square). โ Example Code (Runtime): class Shape { public: virtual void draw() { /* Base draw / } // Virtual function }; class Circle : public Shape { public: void draw() override { / Circle draw / } // Overridden }; // ... in main: Shape s = new Circle(); s->draw();
D. Abstraction
โ Definition: The process of hiding the complex implementation details and showing only the essential features of an object. โ How to achieve in C++: โ Abstract Classes: Classes with pure virtual functions (must be implemented by derived classes). โ Interfaces (Pure Abstract Classes): Classes where all functions are pure virtual. โ Access Specifiers (private/protected): Hiding internal data and helper methods. โ Key Benefits: โ Simplification: Reduces complexity for the user. โ Clarity: Focuses on "what it does" rather than "how it does it." โ Security: Prevents accidental or intentional misuse of internal logic. โ Conceptual Visual: Imagine driving a car. You interact with the steering wheel, pedals, gear stick (essential features), but you don't need to know the intricate workings of the engine or transmission (hidden details).
III. Constructors and Destructors
โ Constructors: โ Definition: Special member functions that are automatically called when an object of the class is created. โ Purpose: Initialize the object's data members. โ Characteristics: Same name as the class, no return type, can be overloaded. โ Types: Default, Parameterized, Copy. โ Destructors: โ Definition: Special member functions that are automatically called when an object goes out of scope or is explicitly deleted. โ Purpose: Clean up resources (e.g., free dynamically allocated memory) held by the object. โ Characteristics: Same name as the class preceded by ~, no return type, cannot be overloaded, takes no arguments.
IV. Access Specifiers
โ Definition: Keywords that define the accessibility of class members (data members and member functions). โ Types: โ public: Members are accessible from anywhere (inside or outside the class). โ private: Members are accessible only from within the same class. (Default for classes). โ protected: Members are accessible from within the same class and from derived classes.
Pragmatic Best Practices for OOP in C++:
โ Design First: Think about your classes, their relationships, and responsibilities before coding. โ Encapsulate Aggressively: Make data private by default. Provide public getters/setters only when necessary. โ Favor Composition over Inheritance (Sometimes): While inheritance is powerful, sometimes "has-a" (composition) relationships are more flexible than "is-a" (inheritance) for complex systems. โ Use Virtual Functions for Polymorphism: When you need runtime behavior changes based on object type. โ Clean Up Memory: If you use new, always use delete in the destructor to prevent memory leaks.