Compile time polymorphism

 This type of polymorphism is achieved by function overloading or operator overloading which is also known as static binding or early binding.


Why is it called compile-time polymorphism?

Overloaded functions are called by comparing the data types and number of parameters. This type of information is available to the compiler at the compile time. Thus, the suitable function to be called will be chosen by the C++ compiler at compilation time.

There are the following types of compile-time polymorphism in C++ :

Function Overloading



Operator Overloading:

C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. 
For example, we can make use of the addition operator (+) for string class to concatenate two strings. We know that the task of this operator is to add two operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them.

Operator Overloading for + to add two operands

Example :

#include <iostream>
using namespace std;

class Complex {
private:
    int real, imag;

public:
     // Constructor
    Complex(int r = 0, int i = 0)
    {
        real = r;
        imag = i;
    }

     // Function to take user input
    void input() {
        cout << "Enter real and imaginary parts: ";
        cin >> real >> imag;
    }

    // Overloading the + operator for addition
    Complex operator+(Complex const& obj)
    {
        Complex res;
        res.real = real + obj.real;
        res.imag = imag + obj.imag;
        return res;
    }
    void print()
    {
    cout << real << " + i" << imag << endl;
    }
};

int main()
{
    Complex c1,c2;
   // Taking user input
    cout << "Enter first complex number:" << endl;
    c1.input();

    cout << "Enter second complex number:" << endl;
    c2.input();

    // An example call to "operator+"
    Complex c3 = c1 + c2;

    // Displaying the result
    cout << "Sum of complex numbers: ";
    c3.print();
}

Output

Enter first complex number:
Enter real and imaginary parts: 22 10
Enter second complex number:
Enter real and imaginary parts: 10 10
Sum of complex numbers: 32 + i20

Process returned 0 (0x0)   execution time : 35.635 s
Press any key to continue.


Operator Overloading for + to concatenate strings

Example:

#include <iostream>
#include <string>
using namespace std;

class MyString {
private:
    string str;

public:
    // Constructor
    MyString(string s = "")  {

        str = s;
    }

    // Function to take user input
    void input() {
        //cout << "\n Enter a string: ";
        cin >> str;
    }

    // Overloading the + operator for concatenation
    MyString operator+(const MyString& obj) {
        return MyString(str + obj.str);
    }

    // Function to display the string
    void display() const {
        cout << str << endl;
    }
};

int main() {
    MyString s1, s2;

    // Taking user input
    cout << "Enter first string: ";
    s1.input();
    cout << "Enter second string :";
    s2.input();

    // Concatenating using overloaded +
    MyString s3 = s1 + s2;

    // Displaying the result
    cout << "Concatenated String: ";
    s3.display();

    return 0;
}

Output:

Enter first string: VVIT
Enter second string :MARANGA
Concatenated String: VVITMARANGA

Process returned 0 (0x0)   execution time : 38.197 s
Press any key to continue.


Note:
  • A user-defined type must be present in at least one operand.
  • ".", "::", sizeof,  and C++'s single ternary operator, "?:", are among the operators that cannot be overloaded.
These are some of the operators that can be overloaded in C++ :

Arithmetic operators : - , + , / , * , % and -=, +=, /= , *= , %=
Boolean algebra : !=, ==, > ,< ,>=, <= , && ,||
Bit manipulation : &, |, ^ ,<< ,>> and |=, &= ,>>=, <<=, ^=
Memory management : new[], new, delete[], delete
 

No comments:

Post a Comment