Overloading programs in C++

Overloading in C++

In this article, we cover the function overloading, constructor overloading, and operator overloading programs in the C++ language.

Topics Covered

  • Write a C++ program to demonstrate an example of function overloading
  • Constructor Overloading programs in C++
  • Write a C++ program to overload unary increment (++) operator
  • Write a C++ program to overload binary + operator

1. Write a C++ program to demonstrate example of function overloading

#include  <iostream>
using namespace std;
 
void printcharacter();
void printcharacter( character c );
void printcharacter( character c, int num );
void printcharacter(int num, character c);
 
int main()
{

   //Printing the functions of different types
    printcharacter();
    printcharacter('#');
    printcharacter(10,'$');
    printcharacter('@',10);
     
    cout<< endl;
         
    return 0;
}

 //declare the first function
void printcharacter()
{
        cout<< endl<<"%";
}

//declare the second function
void printcharacter( character c )
{
        cout<< endl<< c;
}

//declare the third function
void printcharacter( character c, int num )
{
    int i=0;
         
    cout<< endl;
    for(i=0; i< num; i++)
        cout<< c;
}

//declare the fourth function
void printcharacter(int num, character c)
{
    int i=0;
         
    cout<< endl;
    for(i=0; i< num; i++)
        cout<< c;
}

OUTPUT:

2. Write a C++ program to demonstrate example of Constructor Overloading

#include <iostream>
using namespace std;
 
//declaration of the class
class Demo
{
    //Privately declare the data members( X,Y ) of integer type.
    private:
        int X;
        int Y;
 
    //Public blocks of member function to access data members.
    public:
        //Declaration of the default constructor to initialize data members.
            Demo (); 
 
        //Declaration of the parameterized constructor to initialize data members.
            Demo (int a, int b); 

        //To display output on the screen.
        void    Display();
     
};
 //End of class
 
//Definition of the parameterized constructor.
Demo:: Demo()
{
    X = 10;
    Y = 20;
}
 
//Definition of parameterized constructor.
Demo:: Demo(int a, int b)
{
    X = a;
    Y = b;
}
 
 
//Definition of Display() member function.
void Demo:: Display()
{
    cout << endl << "X: " << X;
    cout << endl << "Y: " << Y << endl;
}
 
int main()
{
    Demo d1;
    cout << "Default Constructor: " << endl;
    cout << "Value after initialization : " ;
    d1.Display();   
 
    Demo d2(30,40) ; //Ctor automatically call when object is created.
    cout << "Parameterized Constructor: " << endl;
    cout << "Value after initialization : " ;
    d2.Display();   
 
    return 0;
}

OUTPUT:

3. Write a C++ program to overload unary increment (++) operator

#include<iostream>
using namespace std;

class Increase
{
        int a, b;

     public:

        //taking input in the program 
        void accept()
        {
                cout<<"\n Enter Two Numbers : \n";
                cout<<" ";
                cin>>a;
                cout<<" ";
                cin>>b;
        }
       
        void operator++() //Overload Unary Increment operator
        {
                a++;
                b++;
        }
       //Function to print the final result
        void display()
        {
                cout<<"\n A : "<<a;
                cout<<"\n B : "<<b;
        }
};

//main function
int main()
{
        Increase id;
        id.accept();
        ++id;
        cout<<"\n\n After Incrementing : " << endl;
        id.display();
        return 0;
}

OUTPUT:

4. Write a C++ program to overload binary + operator

#include <iostream>
using namespace std;

class Complex {
   private:
    float real;
    float imag;

   public:
    // Constructor to initialize real and imaginary to 0
    Complex() : real(0), imag(0) {}

    void input() {
        cout << "Enter real and imaginary parts respectively: ";
        cin >> real;
        cin >> imag;
    }

    // Overload the + operator
    Complex operator + (const Complex& obj) {
        Complex temp;
        temp.real = real + obj.real;
        temp.imag = imag + obj.imag;
        return temp;
    }

//Function to print the final output of complex number
  void output() {
        if (imag < 0)
            cout << "Output Complex number: " << real << imag << "i";
        else
            cout << "Output Complex number: " << real << "+" << imag << "i";
    }
};
int main() {
    Complex complex1, complex2, result;

    cout << "Enter first complex number:\n";
    complex1.input();

    cout << "Enter second complex number:\n";
    complex2.input();
    result = complex1 + complex2;
    result.output();

    return 0;
}

OUTPUT:

You can try these above codes by your own on your machine or compiler.

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?