Answer to Question #231426 in C++ for Ali

Question #231426
✓ Write a class Sum. Two data members q and r are declared in class Sam. The data members q

and rare initialized in a constructor

✓ Create a function In() to input the values of q and r. Also create a function Display() to the input values. Create a function to overload the binary addition operator to add two values.

display

✓ In main function create three objects m,n and o.

✓ Through mand o objects access the member function In() to input data twice. ✔ Now add these values from mand o and save in n. (Hint: n=m+o)

✔ Again through m, n and o objects access the member function Display() to display data

✔Show the output of program.
1
Expert's answer
2021-08-31T01:51:37-0400


#include <iostream>


using namespace std;


class Sum{
    private:
        int q;
        int r;
    public:
        Sum(){
            q=0;
            r=0;
        }
        void In(){
            cout<<"\nEnter the value of q: ";
            cin>>q;
            cout<<"\nEnter the value of r: ";
            cin>>r;
        }
        void Display(){
            cout<<"\nThe value of q is: "<<q;
            cout<<"\nThe value of r is: "<<r;
        }
        Sum operator + (const Sum& obj) {
        Sum temp;
        temp.q = q + obj.q;
        temp.r = r + obj.r;
        return temp;
    }
};
int main()
{
    Sum m,n,o;
    m.In();
    o.In();
    n=m+o;
    m.Display();
    n.Display();
    o.Display();
    return 0;
}

Output





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS