Write short notes on any two: i) Virtual functions ii) Operator overloading iii) Interface iv) Inline function


Q.) Write short notes on any two:

i) Virtual functions ii) Operator overloading iii) Interface iv) Inline function

Subject: Object Oriented Programming

Virtual Functions:

Virtual functions are member functions that are declared in a base class and can be redefined (overridden) in derived classes. This allows derived classes to inherit the implementation of a base class function, but also to provide their own unique implementation if desired. Virtual functions are declared using the virtual keyword in the base class.

When a derived class overrides a virtual function, the compiler generates a new function that is specific to the derived class. This new function is called the "overriding function". When an object of the derived class calls the virtual function, the compiler automatically calls the overriding function.

Virtual functions are used to achieve polymorphism, which is the ability for objects of different classes to respond to the same message in different ways.

Operator Overloading:

Operator overloading is a feature that allows you to define how operators behave when applied to objects of a particular class. For example, you can overload the + operator to define how two objects of a particular class should be added together.

To overload an operator, you simply define a member function that has the same name as the operator. For example, to overload the + operator, you would define a member function called operator+() in the class.

When an operator is overloaded, the compiler automatically calls the operator function when the operator is applied to objects of the class. This allows you to define custom behavior for operators, such as adding two objects together or comparing two objects.

Inline Function:

An inline function is a function that is expanded in-line at the call site. This means that the function call is replaced with the body of the function. Inline functions are used to reduce the overhead of function calls.

To declare an inline function, you simply use the inline keyword before the function declaration. For example:

inline int add(int a, int b) {
  return a + b;
}

Inline functions are typically used for small functions that are called frequently. By inlining these functions, you can improve the performance of your program.

Interface:

An interface is a contract between a class and its clients. It specifies the methods that the class must implement, but it does not specify how the methods are implemented. This allows the class to change its implementation without affecting its clients.

Interfaces are defined using the interface keyword. For example:

interface IShape {
  public:
    virtual double area() = 0;
    virtual double perimeter() = 0;
};

This interface specifies that any class that implements it must have an area() method and a perimeter() method. However, the interface does not specify how these methods are implemented.

Classes that implement an interface must use the implements keyword in their declaration. For example:

class Circle : public IShape {
  public:
    double area() override {
      return M_PI * radius * radius;
    }
    double perimeter() override {
      return 2 * M_PI * radius;
    }
  private:
    double radius;
};

This class implements the IShape interface by providing implementations for the area() and perimeter() methods.