Write a program that can overload two ” + ” operator.(For intake 41)

Question

In C++, we can make operators work for user-defined classes. This means 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 overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +.

in progress 2
Ask me anything 1 month 23 Answers 345 views 0

Answers ( 23 )

  1. Name: Tamanna Tasnim
    ID: 142

    #include
    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2);
    };

    crood crood:: operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  2. NAME: Md. Emon Ali
    ID:154
    #include
    using namespace std;

    class myclass {
    public:
    int x,y;
    myclass () {};
    myclass (int,int);
    myclass operator + (myclass);
    };

    myclass::myclass (int a, int b) {
    x = a;
    y = b;
    }

    myclass myclass::operator+ (myclass ob) {
    myclass temp;
    temp.x = x + ob.x;
    temp.y = y + ob.y;
    return (temp);
    }

    int main () {
    myclass a (3,1);
    myclass b (1,2);
    myclass c;
    c = a + b;
    cout<< c.x <<endl<< c.y;
    return 0;
    }

  3. Moinul islam
    ID- 18192103180

    #include
    using namespace std;
    class coord
    {
    int x,y;
    public :
    coord() {x=0; y=0;}
    coord(int i, int j) {x=i; y=j;}
    void get_xy(int &i, int &j) {i=x; j=y;}
    coord operator + (coord ob2);
    };
    coord coord :: operator + (coord ob2)
    {
    coord temp;
    temp.x = x+ob2.x;
    temp.y = y+ob2.y;
    return temp;
    }
    int main()
    {
    coord o1(10, 10), o2(5,3), o3;
    int x, y;
    o3 = o1+ o2;
    o3.get_xy(x,y);
    cout << "(o1+o2) x : "<< x << ", y : "<< y << endl;
    return 0;
    }

  4. Name: Sabila
    ID: 136

    #include
    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood(){x=0;y=0;}
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j){i=x;j=y;}
    crood operator+(crood ob2);
    };

    crood crood:: operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  5. Name:Shamima Akter Shimu Id:18192103144 #include
    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2);
    };

    crood crood:: operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  6. Name: Nigar Sultana Era
    Id : 175
    Sec: 4
    #include
    using namespace std;
    class coord
    {
    int x,y;
    public :
    coord() {x=0; y=0;}
    coord(int i, int j) {x=i; y=j;}
    void get_xy(int &i, int &j) {i=x; j=y;}
    coord operator + (coord ob2);
    };
    coord coord :: operator + (coord ob2)
    {
    coord temp;
    temp.x = x+ob2.x;
    temp.y = y+ob2.y;
    return temp;
    }
    int main()
    {
    coord o1(10, 10), o2(5,3), o3;
    int x, y;
    o3 = o1+ o2;
    o3.get_xy(x,y);
    cout << "(o1+o2) x : "<< x << ", y : "<< y << endl;
    return 0;
    }

  7. Name: Tabassum Mehrin Prova
    ID:177
    #include
    using namespace std;

    class myclass {
    public:
    int x,y;
    myclass () {};
    myclass (int,int);
    myclass operator + (myclass);
    };

    myclass::myclass (int a, int b) {
    x = a;
    y = b;
    }

    myclass myclass::operator+ (myclass ob) {
    myclass temp;
    temp.x = x + ob.x;
    temp.y = y + ob.y;
    return (temp);
    }

    int main () {
    myclass a (3,1);
    myclass b (1,2);
    myclass c;
    c = a + b;
    cout<< c.x <<endl<< c.y;
    return 0;
    }

  8. Name: Rakibul Hasan Shafin

    Intake: 41

    Section: 04

    ID: 18192103165

    #include
    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2);
    };

    crood crood:: operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  9. Meftahul Jannat
    ID:18192103149
    Intake:41
    Section:04

    #include
    using namespace std;
    class coord
    {
    int x,y;
    public :
    coord() {x=0; y=0;}
    coord(int i, int j) {x=i; y=j;}
    void get_xy(int &i, int &j) {i=x; j=y;}
    coord operator + (coord ob2);
    };
    coord coord :: operator + (coord ob2)
    {
    coord temp;
    temp.x = x+ob2.x;
    temp.y = y+ob2.y;
    return temp;
    }
    int main()
    {
    coord o1(10, 10), o2(5,3), o3;
    int x, y;
    o3 = o1+ o2;
    o3.get_xy(x,y);
    cout << "(o1+o2) x : "<< x << ", y : "<< y << endl;
    return 0;
    }

  10. Sorry it a private answer.

  11. #include
    using namespace std;
    class crood{
    int x,y;
    public:
    crood() {x=0;y=0; }
    crood(int i,int j) {x=i;y=j;}
    void get_xy(int &i,int &j) {i=x;j=y;}
    crood operator + (crood ob2);
    };
    crood crood::operator+(crood ob2){
    crood #temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;
    return temp;
    }
    int main()
    {
    crood o1(10,15),o2(5,10),o3;
    int x,y;
    o3=o1+o2;
    o3.get_xy(x,y);
    cout<<" (o1+o2) X: " <<x<<", y: "<<y<<"n";
    return 0;
    }

  12. Tahmid Hossain Rasel
    ID:168

    #include
    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood(){x=0;y=0;}
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j){i=x;j=y;}
    crood operator+(crood ob2);
    };

    crood crood:: operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  13. D.K.Shah Alam
    id.18192103176,sec.4

    #include

    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;
    return temp;
    }
    };

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  14. //Name: Syeda Nafia Sultana, ID: 17181103058, Intake: 37//
    #include
    using namespace std;
    class coord {
    int x,y;
    public:
    coord () {x=0;y=0;}
    coord(int i,int j){x=i;y=j;}
    void get_xy(int &i, int &j){i=x; j=y;}
    coord operator ++();
    };
    coord coord :: operator ++()
    {
    x++;
    y++;
    return *this;
    }int main ()
    {
    coord o1(10,10);
    int x,y;
    ++ o1;
    o1.get_xy(x,y);
    cout<<"(++o1)X:"<<x<<",Y:"<<y<<"n";
    return 0;
    }

  15. Arman Hossain
    Id-18192103156
    sec-04

    #include

    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;
    return temp;
    }
    };

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  16. Sorry it a private answer.

  17. Name : MD Masudul Islam Asif
    ID : 18192103147

    #include
    using namespace std;
    class crood{
    int x,y;
    public:
    crood() {x=0;y=0; }
    crood(int i,int j) {x=i;y=j;}
    void get_xy(int &i,int &j) {i=x;j=y;}
    crood operator + (crood ob2);
    };
    crood crood::operator+(crood ob2){
    crood #temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;
    return temp;
    }
    int main()
    {
    crood o1(10,15),o2(5,10),o3;
    int x,y;
    o3=o1+o2;
    o3.get_xy(x,y);
    cout<<" (o1+o2) X: " <<x<<", y: "<<y<<"n";
    return 0;
    }

  18. Name: Md. Mintu Hossain
    ID: 17181103110
    Intake: 37

    #include
    using namespace std;
    class coordinate
    {
    int x,y;
    public:
    coordinate(){x=0;y=0;}
    coordinate(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_values(int &i, int &j){i=x;j=y;}
    coordinate operator+(coordinate ob2);
    };

    coordinate coordinate:: operator+(coordinate ob2)
    {
    coordinate temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    coordinate ob1(10,20), ob2(20,30),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_values(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  19. Name: Rois Uddin Khan Emon
    Intake:41
    Section:04
    ID:18192103153

    #include
    using namespace std;
    class coord
    {
    int x,y;
    public :
    coord() {x=0; y=0;}
    coord(int i, int j) {x=i; y=j;}
    void get_xy(int &i, int &j) {i=x; j=y;}
    coord operator + (coord ob2);
    };
    coord coord :: operator + (coord ob2)
    {
    coord temp;
    temp.x = x+ob2.x;
    temp.y = y+ob2.y;
    return temp;
    }
    int main()
    {
    coord o1(10, 10), o2(5,3), o3;
    int x, y;
    o3 = o1+ o2;
    o3.get_xy(x,y);
    cout << "(o1+o2) x : "<< x << ", y : "<< y << endl;
    return 0;
    }

  20. NAME: MD Tayeb
    Intake: 41
    Sec: 4
    ID: 18192103164

    #include
    using namespace std;

    class myclass {
    public:
    int x,y;
    myclass () {};
    myclass (int,int);
    myclass operator + (myclass);
    };

    myclass::myclass (int a, int b) {
    x = a;
    y = b;
    }

    myclass myclass::operator+ (myclass ob) {
    myclass temp;
    temp.x = x + ob.x;
    temp.y = y + ob.y;
    return (temp);
    }

    int main () {
    myclass a (3,1);
    myclass b (1,2);
    myclass c;
    c = a + b;
    cout<< c.x <<endl<< c.y;
    return 0;
    }

  21. id:18192103139
    #include
    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2);
    };

    crood crood:: operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;

    return temp;
    }

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  22. arup Raton Chowdhury
    Id-18192103155
    sec-04

    #include

    using namespace std;
    class crood
    {
    int x,y;
    public:
    crood()
    {
    x=0;
    y=0;
    }
    crood(int i, int j)
    {
    x=i;
    y=j;
    }
    void get_xy(int &i, int &j)
    {
    i=x;
    j=y;
    }
    crood operator+(crood ob2)
    {
    crood temp;
    temp.x=x+ob2.x;
    temp.y=y+ob2.y;
    return temp;
    }
    };

    int main()
    {
    crood ob1(10,10), ob2(5,3),ob3;
    int x,y;
    ob3=ob1+ob2;
    ob3.get_xy(x,y);
    cout<< "(ob1+ob2) X:" <<x<< " Y:" <<y<< "n";
    return 0;
    }

  23. Md Ashraful Hossain
    ID -18192103137

    #include
    using namespace std;
    class myclass {
    public:
    int x,y;
    myclass () {};
    myclass (int,int);
    myclass operator + (myclass);
    };
    myclass::myclass (int a, int b) {
    x = a;
    y = b;
    }
    myclass myclass::operator+ (myclass ob) {
    myclass temp;
    temp.x = x + ob.x;
    temp.y = y + ob.y;
    return (temp);
    }
    int main () {
    myclass a (3,1);
    myclass b (1,2);
    myclass c;
    c = a + b;
    cout<< c.x <<endl<< c.y;
    return 0;
    }

Leave an answer