Forem

Cover image for Object Oriented Programming for Interview:)
Madhav Ganesan
Madhav Ganesan

Posted on

Object Oriented Programming for Interview:)

It is a type of programming paradigm which uses the concept of objects to solve real world problems.

Languages that follows OOPS paradigm

C++, Java, Python, Javascript, Ruby, Swift, Objective-c, C#

OOPS Concepts:

Class

  • It is also called as Blueprint of an object.
  • It is an user defined data type used to create an instance of a class. It consists of data members and member functions.
  • (Note: Classes do not use memory. They merely serve as a template from which items are made)

Image description

Objects

  • It is an instance of a class.
  • Objects have been made with class as a blueprint.

Image description

Main features

Abstraction

  • This shows important things to the user and hides the internal details.
  • It refers to providing only necessary information about the data to the outside world, hiding the background details or implementation.
  • Abstraction is about simplifying the interface

Ex. We know the important features of the phone such as battery,sim,design and os. But we may not know about how it works like memory allocation operation etc

Encapsulation

  • It is principle in which all important information is contained inside an object.
  • It protects the internal state of an object by keeping its data members private. Encapsulation hides the internal implementation details of a class from external code.
  • Encapsulation is concerned with data security and integrity by restricting access to the internal state

Inheritance

  • It is a property of OOPS in which a class can derive properties and characteristics from another class.

Types of inheritance

Single Inheritance:
Child class derived directly from the base class

Image description

Multiple Inheritance:
Child class derived from multiple base classes.

Image description

Multilevel Inheritance:
Child class derived from the class which is also derived from another base class.

Image description

Hierarchical Inheritance:
Multiple child classes derived from a single base class.

Image description

Hybrid Inheritance:
Inheritance consisting of multiple inheritance types of the above specified.

Image description

Polymorphism

It means having many forms.

Compile-time Polymorphism (Method Overloading):

  • This occurs when multiple methods in the same class have the same name but different parameters.
class Example {
public:
    void display(int i) {
        std::cout << "Integer: " << i << std::endl;
    }
    void display(double d) {
        std::cout << "Double: " << d << std::endl;
    }
};
Enter fullscreen mode Exit fullscreen mode

Runtime Polymorphism (Method Overriding):

  • This occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
  • This is typically achieved using virtual functions in C++.
class Base {
public:
    virtual void show() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    void show() override {
        std::cout << "Derived class" << std::endl;
    }
};
Enter fullscreen mode Exit fullscreen mode

Dynamic Binding (late binding/dynamic dispatch)

  • It is the process of determining the method or function to be executed at runtime, rather than at compile time.

Dynamic Typing

  • It refers to the ability of a programming language to determine the data type of a variable at runtime, rather than at compile time.

Dynamic Loading

Dynamic loading is a mechanism where a program loads a library or module into memory only when it is explicitly requested or required. This helps reduce memory usage and improve performance by loading only the necessary components.

Abstract Class

An abstract class in C++ is a class that cannot be instantiated on its own and is designed to be inherited by other classes. It contains at least one pure virtual function.

#include <iostream>

// Abstract Class
class Shape {
public:
    // Pure virtual function
    virtual void draw() const = 0;

    // Regular function
    void setColor(const std::string& color) {
        this->color = color;
    }

protected:
    std::string color;
};

// Derived Class
class Circle : public Shape {
public:
    void draw() const override {
        std::cout << "Drawing a circle with color " << color << std::endl;
    }
};

int main() {
    // Shape s; // Error: Cannot instantiate an abstract class
    Circle c;
    c.setColor("red");
    c.draw();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Interface

An interface in C++ is essentially an abstract class that contains only pure virtual functions

#include <iostream>

// Interface
class Drawable {
public:
    // Pure virtual function
    virtual void draw() const = 0;
};

// Another Interface
class Colorable {
public:
    // Pure virtual function
    virtual void setColor(const std::string& color) = 0;
};

// Implementing multiple interfaces
class Square : public Drawable, public Colorable {
public:
    void draw() const override {
        std::cout << "Drawing a square with color " << color << std::endl;
    }

    void setColor(const std::string& color) override {
        this->color = color;
    }

private:
    std::string color;
};

int main() {
    Square s;
    s.setColor("blue");
    s.draw();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Struct vs Class

Default Access Control:
struct: Members of a struct are public by default.
class: Members of a class are private by default.

It is used for simple data structures where data encapsulation is not a primary concern
it is used for more complex objects where encapsulation, data hiding, and member functions are typically required.

Constructor

It is a special member function of a class that is automatically called when an object of the class is created. It initializes the object.

#include <iostream>

class MyClass {
public:
    // Constructor
    MyClass(int value) : value(value) {
        std::cout << "Constructor called. Value: " << value << std::endl;
    }

    // Destructor
    ~MyClass() {
        std::cout << "Destructor called. Cleaning up resources." << std::endl;
    }

    // Member function to display the value
    void display() const {
        std::cout << "Value: " << value << std::endl;
    }

private:
    int value;
};

int main() {
    // Create an object of MyClass
    MyClass obj(10);
    obj.display();

    // Destructor will be called automatically when 'obj' goes out of scope
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Destructor

It is a special member function of a class that is automatically called when an object of the class is destroyed. It cleans up resources that the object may have acquired during its lifetime.
(Note: No, you cannot overload the destructor in C++)

Stay Connected!
If you enjoyed this post, don’t forget to follow me on social media for more updates and insights:

Twitter: madhavganesan
Instagram: madhavganesan
LinkedIn: madhavganesan

Top comments (1)

Collapse
 
madhurima_rawat profile image
Madhurima Rawat

Great article πŸ‘! All the Illustrations are great πŸ‘