Wednesday, March 16, 2022

overloading of constructor ,operators and functions in cpp

 #include <iostream>

using namespace std;
class hi
{
    int num1, num2, num3;

public:
    hi(int num1, int num2, int num3)
    {
        this->num1 = num1;
        this->num2 = num2;
        this->num3 = num3;
    }
    hi() {}
    hi(int num1)
    {
        this->num1 = num1;
    }
    hi(int num1, int num2)
    {
        this->num1 = num1;
        this->num2 = num2;
    }
    hi(int num11, int num22, int num33, int num44)
    {
        cout << "the sum of num 11, num22 and num 33 is " << num11 + num22 + num33 << endl;
    }
    void showdata(int num12)
    {
        cout << "num12 is" << num1;
    }
    void showdata(int num12, int num123)//function overloading
    {
        cout << "num12 is" << num12 << endl;
        cout << "num123 is" << num123 << endl;
    }
    void print()
    {
        cout << num1 << " and " << num2 << endl;
    }
    hi operator+(hi a) // binary operator overloading
    {
        hi temp;
        temp.num1 = num1 + a.num1;
        temp.num2 = num2 + a.num2;
        return temp;
    }
    // logical operator overloading
    bool operator==(hi a)
    {
        if ((num1 == a.num1) && (num2 == a.num2) && (num3 == a.num3))
            return true;
        else
            return false;
    }
    // relational operator overloading
    bool operator<(hi d)
    {
        if (num1 < d.num1)
        {
            return true;
        }
        if (num1 == d.num1 && num2 < d.num2)
        {
            return true;
        }

        return false;
    }
    hi operator++() // unary operator
    {
        return (num1 = num1 + num1);
    }
    void print2()
    {
        cout << "num 1 is " << num1 << endl;
    }
};

int main()
{
    hi a(1, 2, 3), b(1, 2, 3), c(1, 2), d(3, 4), e(23);
    if (a == b)
    {
        cout << "equals a and b" << endl;
    }
    else
    {
        cout << "not equal" << endl;
    }
    if (c < d)
    {
        cout << "c<d" << endl;
    }
    else
    {
        cout << "no c less d" << endl;
    }
    ++e;
    e.print2();
    hi f = c + d; // it is like c.+(d)
    f.print();

    return 0;
}

refrence link

No comments:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts