ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    CSI-312
    Progress0 / 16 topics
    Topics
    1. Evolution of Object-Oriented (OO) Programming2. Object-Oriented (OO) Concepts and Principles3. Problem Solving in OO Paradigm4. OO Programme Design Process5. Classes6. Methods7. Objects and Encapsulation8. Constructors and Destructors9. Operator Overloading10. Function Overloading11. Virtual Functions12. Derived Classes13. Inheritance14. Polymorphism15. I/O and File Processing16. Exception Handling
    CSI-312›Object-Oriented (OO) Concepts and Principles
    Object Oriented ProgrammingTopic 2 of 16

    Object-Oriented (OO) Concepts and Principles

    7 minread
    1,156words
    Intermediatelevel

    Object-Oriented (OO) Concepts and Principles

    Object-Oriented Programming (OOP) is based on a set of concepts and principles that allow for designing software that is modular, reusable, and easier to maintain. These principles help in structuring the code around objects, which can interact with one another while hiding implementation details.

    Here are the core Object-Oriented Concepts and Principles:


    1. Object

    An object is a basic unit in OOP, representing a real-world entity or concept. It is an instance of a class and has two main components:

    • Attributes (State): Data or properties that describe the object. These are typically variables or fields within the object.
    • Methods (Behavior): Functions or procedures that define the behavior or actions the object can perform. These are functions defined within the class and act upon the object's state.

    Example: In a "Car" object:

    • Attributes: color, model, engine type, etc.
    • Methods: start_engine(), stop_engine(), accelerate(), etc.

    2. Class

    A class is a blueprint or template for creating objects. It defines the structure and behavior of the objects but does not itself hold data (that’s the job of the object instances). A class encapsulates data and methods into a single unit.

    • Attributes (instance variables or fields) are defined within a class.
    • Methods (functions or procedures) describe the behaviors and operations that can be performed on the objects of that class.

    Example:

    class Car {
    public:
        string color;
        string model;
        
        void start_engine() {
            // Logic to start the engine
        }
        
        void stop_engine() {
            // Logic to stop the engine
        }
    };
    

    In this example, the Car class defines the structure for all car objects.


    3. Encapsulation

    Encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class) and restricting direct access to some of the object’s components. This is typically achieved by using access specifiers (like public, private, protected in C++), ensuring that the internal state of the object is hidden from the outside world.

    • Private members: Accessible only within the class itself.
    • Public members: Accessible from outside the class.
    • Protected members: Accessible within the class and its derived classes.

    Benefits:

    • Protects the integrity of the object’s data.
    • Hides implementation details and exposes only necessary interfaces to the user.

    Example:

    class BankAccount {
    private:
        double balance;  // Private data, cannot be accessed directly
        
    public:
        void deposit(double amount) {
            if (amount > 0)
                balance += amount;
        }
        
        double get_balance() {
            return balance;
        }
    };
    

    Here, balance is private and cannot be directly accessed from outside the BankAccount class. Methods like deposit() and get_balance() provide controlled access to it.


    4. Inheritance

    Inheritance is a mechanism where a new class (called a derived class or child class) acquires the attributes and methods of an existing class (called a base class or parent class). This allows for code reuse and the creation of more specialized classes.

    • The derived class can also have additional features or override base class methods.

    Example:

    class Vehicle {
    public:
        void start() {
            cout << "Vehicle started" << endl;
        }
    };
    
    class Car : public Vehicle {
    public:
        void drive() {
            cout << "Car is driving" << endl;
        }
    };
    

    In this example, the Car class inherits from the Vehicle class and can access the start() method while adding its own method, drive().

    Types of Inheritance:

    • Single inheritance: A class inherits from one base class.
    • Multiple inheritance: A class inherits from more than one base class (supported by C++).
    • Multilevel inheritance: A class inherits from a class that is already derived from another class.
    • Hierarchical inheritance: Multiple classes inherit from the same base class.

    5. Polymorphism

    Polymorphism allows objects of different classes to be treated as objects of a common base class. The term means “many forms,” and it refers to the ability to call the same method on different objects, and each object can respond in a way that is appropriate to its type.

    There are two types of polymorphism in OOP:

    • Compile-time polymorphism (also known as method overloading or operator overloading): The method to be called is determined at compile time.
    • Run-time polymorphism (also known as method overriding): The method to be called is determined at runtime, often using inheritance and virtual functions.

    Example of Method Overriding:

    class Animal {
    public:
        virtual void sound() {
            cout << "Animal makes a sound" << endl;
        }
    };
    
    class Dog : public Animal {
    public:
        void sound() override {
            cout << "Dog barks" << endl;
        }
    };
    
    class Cat : public Animal {
    public:
        void sound() override {
            cout << "Cat meows" << endl;
        }
    };
    
    int main() {
        Animal* animal1 = new Dog();
        Animal* animal2 = new Cat();
    
        animal1->sound();  // Dog barks
        animal2->sound();  // Cat meows
    
        delete animal1;
        delete animal2;
    }
    

    Here, the method sound() is overridden in the Dog and Cat classes, and the correct version of sound() is called based on the object type at runtime.


    6. Abstraction

    Abstraction is the concept of hiding the complex implementation details and exposing only the essential features or interfaces to the user. It helps in reducing complexity and allows focusing on high-level mechanisms.

    • Abstract classes and interfaces are used to achieve abstraction.
    • An abstract class can have abstract methods (methods without implementation) that must be implemented by derived classes.
    • An interface (in languages like Java and C#) can only declare methods, not provide their implementation.

    Example:

    class Shape {
    public:
        virtual void draw() = 0;  // Pure virtual function
    };
    
    class Circle : public Shape {
    public:
        void draw() override {
            cout << "Drawing a Circle" << endl;
        }
    };
    

    In this example, Shape is an abstract class, and draw() is a pure virtual function that must be implemented in any derived class.


    7. Association, Aggregation, and Composition

    These are relationships between objects that represent how one object is related to another.

    • Association: A general relationship where objects can interact with each other. For example, a "Teacher" object can associate with a "Student" object.
    • Aggregation: A special type of association where the child object can exist independently of the parent object. For example, a "Library" contains "Books," but books can exist outside of the library.
    • Composition: A stronger form of aggregation where the child object cannot exist without the parent object. For example, a "House" contains "Rooms," and rooms cannot exist independently without the house.

    Conclusion

    The main Object-Oriented Principles—Encapsulation, Inheritance, Polymorphism, and Abstraction—form the foundation for designing robust, maintainable, and reusable code. They allow developers to break down complex systems into manageable objects and enable the development of scalable and flexible applications. Understanding and applying these principles is crucial for mastering OOP in languages like C++.

    Previous topic 1
    Evolution of Object-Oriented (OO) Programming
    Next topic 3
    Problem Solving in OO Paradigm

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,156
      Code examples0
      DifficultyIntermediate