Function Overloading, operator overloading, function overriding in CPP/C++ || Polymorphism in CPP


 Function/Method Overloading in CPP/C++:

#include<bits/stdc++.h>

using namespace std;

class prime{

public:

        void sum(int a, int b)

        {

                printf("%d\n", (a + b));

        }

        void sum(int a, double b)

        {

                cout<<(a + b)<<endl;

        }

        void sum(double a, double b)

        {

                cout<<(a + b)<<endl;

        }

};

int main()

{

        prime obj;

        obj.sum(10.30, 20.45);

        return 0;

}



Operator Overloading in CPP/C++:

#include<iostream>

using namespace std;

int main()

{

        string s1, s2, s;

        s1 = "Prime ";

        s2 = "University";

        s = s1 + s2;

        cout<<s<<endl;

        int num1, num2, num;

        num1 = 100;

        num2 = 300;

        num = num1 + num2;

        cout<<num<<endl;

        return 0;

}

Method Overriding/Run Time Polymorphism:

#include<iostream>

using namespace std;

class prime{

public:

        void display(){

                cout<<"Prime, CSE Dept."<<endl;

        }

};

class cse : public prime{

public:

  void display(){

                cout<<"Prime, CSE Dept. from CSE class"<<endl;

        }

};

int main()

{

        cse obj;

        obj.display();

        return 0;

}





No comments:

Post a Comment