Notes on Constructors, Overloading, and Inheritance in C++

Constructors in Object-Oriented Programming

Definition and Purpose

A constructor is a special member function of a class that is automatically called whenever an object of that class is created. The constructor has the same name as the class, and it is crucial for initializing the object's data members. The constructor does not have a return value, not even void, and it cannot be inherited or declared as static or virtual.

Types of Constructors
  1. Dummy Constructor: This constructor performs no action and initializes data members with garbage values. For example:

   class Demo {
       private: int x;

       public:  
       Demo() {}  // Dummy Constructor
   };
  1. Default Constructor: This constructor does not take any arguments and initializes object members. For example:
    ```cpp
    class Demo {
    private: int x;
    public:
    Demo() {
    x = 0;
    }
    };

3. **Parameterized Constructor**: This constructor takes parameters to initialize an object. For example:

cpp
class Demo {
private: int x;
public:
Demo(int i) {
x = i;
}
};

4. **Copy Constructor**: This constructor creates a new object as a copy of an existing object. It primarily serves to initialize new objects with the values of existing ones. For example:

cpp
class Demo {
private: int x;
public:
Demo(Demo &n) {
x = n.x;
}
};

### Constructor Overloading
Similar to normal functions, constructors can be overloaded, meaning a class can have multiple constructors with different parameter lists. The constructors are identified by their argument types and numbers. Examples include:

cpp
class Demo {
public:
Demo() {} // Default Constructor
Demo(int x) {} // Parameterized Constructor
};

## Polymorphism

### Method Overloading
Method overloading allows multiple functions to have the same name with different parameter lists. This can be done by varying the number of parameters, the parameter types, or the return types:
1. **Different Number of Parameters**:  

cpp
void add(int x, int y) {
// implementation
}
void add(int x) {
// implementation
}

2. **Different Parameter Types**:  

cpp
void add(int x, float y) {
// implementation
}

3. **Different Return Types**:  

cpp
int add(int x, int y) {
return x + y;
}
float add(float x, float y) {
return x + y;
}

### Operator Overloading
Operator overloading is a feature that allows developers to redefine the way operators work for user-defined classes. This can lead to more intuitive coding and better readability. It can be classified as unary (operating on one operand) or binary (operating on two operands). For instance:

cpp
class Complex {
public:
Complex operator + (const Complex& obj) {
// implementation
}
};
```