Zone Of Makos

Menu icon

OOPS in C++

Object-Oriented Programming (OOPS) is a programming paradigm that allows you to organize your code into objects, which are instances of classes. C++ is a powerful programming language that fully supports OOPS concepts. In this lesson, we'll explore the key OOPS concepts in C++ and how they can be used to write modular and reusable code.

Classes and Objects

In C++, a class is a user-defined data type that encapsulates data and methods. It serves as a blueprint for creating objects. An object is an instance of a class, and it represents a specific entity in your program. Objects have their own data and can perform operations defined by the class. You can create multiple objects from a single class, each with its own unique data.


class MyClass {
    // Data members
    int myVariable;
    
    // Member functions
    void myMethod() {
        // Code goes here
    }
};

int main() {
    // Create an object of MyClass
    MyClass myObject;
    
    // Access data members and call member functions
    myObject.myVariable = 10;
    myObject.myMethod();
    
    return 0;
}

Inheritance

Inheritance is a mechanism in which one class inherits properties and behaviors from another class. It allows you to create a hierarchy of classes, where subclasses inherit the attributes and methods of their parent classes. This promotes code reuse and allows you to create specialized classes based on existing ones.


class Animal {
public:
    void eat() {
        // Code to eat
    }
};

class Dog : public Animal {
public:
    void bark() {
        // Code to bark
    }
};

int main() {
    Dog myDog;
    
    myDog.eat();  // Inherited from Animal class
    myDog.bark(); // Unique to Dog class
    
    return 0;
}

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables you to write code that can work with objects of different types, providing flexibility and extensibility to your code. Polymorphism can be achieved through function overloading and function overriding.


class Shape {
public:
    virtual void draw() {
        // Code to draw a generic shape
    }
};

class Circle : public Shape {
public:
    void draw() override {
        // Code to draw a circle
    }
};

class Square : public Shape {
public:
    void draw() override {
        // Code to draw a square
    }
};

int main() {
    Shape* shapePtr;
    
    Circle circle;
    Square square;
    
    shapePtr = &circle;
    shapePtr->draw();  // Draws a circle
    
    shapePtr = □
    shapePtr->draw();  // Draws a square
    
    return 0;
}

Conclusion

OOPS in C++ is a powerful paradigm that allows you to write modular, reusable, and extensible code. Classes and objects provide a way to organize data and related functionality, while inheritance enables code reuse and promotes hierarchy. Polymorphism allows objects of different types to be treated uniformly, enhancing flexibility and extensibility. By leveraging these OOPS concepts, you can write clean, maintainable, and efficient code in C++. Keep practicing and exploring the world of OOPS to become a proficient C++ programmer!