0 like 0 dislike
5.8k views

Write a class called Adder that stores the sum of all the ints given to it. Your Adder class should allow you to write the following main function (and code like it):

int main()
{
int x,y;
cin >> x >> y;
Adder sum1; // sum1 is initialized to 0 
Adder sum2(x); // sum2 is initialized to x
Adder sum3(y); // sum1 is initialized to y 
cout << "sum1 is " << sum1 << '\n'; // prints "sum1 is 0" 
cout << "sum2 is " << sum2 << '\n'; // prints "sum2 is x"
cout << "sum3 is " << sum3 << '\n'; // prints "sum3 is y" 
sum1 += x; //adds x to sum1; now sum1 is x 
sum2 += -3; // adds -3 to sum2; now sum2 is x-3
sum3 += 2; //adds 2 to sum3; now sum3 is y+2 
if (sum1 == sum2)
cout << "sum1 and sum2 are the same\n";
else cout << "sum1 and sum2 are not the same\n";
if (sum1 == sum3)
cout << "sum1 and sum3 are the same\n";
else cout << "sum1 and sum3 are not the same\n";
}

You should write the functions that are necessary for Adder to be used as in the above program. Combine them all together in your answer to make a perfect program. Use const wherever appropriate, and do not write or use a cast operator. Make sure to include any necessary header files.

Example input:

3 5

Example output:

sum1 is 0
sum2 is 3
sum3 is 5
sum1 and sum2 are not the same
sum1 and sum3 are not the same

 

[Exercise] Coding (C) - asked in C++
ID: 24588 - Available when: Unlimited - Due to: Unlimited

edited by | 5.8k views
0 0
Called for Help
0 0
Called for Help

31 Answers

0 like 0 dislike
Hidden content!
#include ** * * ** ** *



using namespace std;



class Adder{
* * ***** *
****** ** ** *** **** * **
** **** * *** *** ******* *** *
* ** ** ** *** * * * * * ** * * a)
*** * ***** * * *** ****
* * *** ** * * ** ** ** ******* *********** * ** = * * ** ** + a.sum);
*** ** * *** *** * *** *** **** *this;
*** * * **** ******
*** ** * ** * *** ** ** Adder &a)
*** * * **
** ** ** ** *** * * ** ****** ** * ** *** * * * == a.sum)
** ** ****** ** ** * **** **** ** * * * * * ** * * ** * * * * * 1;
** ** * * ** ** **** ** ** **
* ** ***** ** * **** ** * * * *** * * * * ** ** *** **** 0;
*** * * * ** *****
* * **** *** * * * * ** * * * * ** * * *** ** ** os, const Adder &a)
**** ** ** * * * *******
* * **** * ** * *** * ** * * * ** * * * * * *** * ** a.sum;
* * ******** ** *** * * ** * ** **** **** *** os;
** * * * * ****** * **
***** * **
*** ** *** * * ** ** sum;

};


* * *

{
* *** ** *** ** * = 0;

}


*** a)

{
* * * *** * = a;

}



int main()

{
*** * * * ** * *** * * * x,y;
* ** * * ** * * ** * x ** * y;
* * * * ******* * ** * *** sum1; // sum1 is initialized to 0
*** * * ** * ** * * *** sum2(x); // sum2 is initialized to x
* * ** *** ** * * sum3(y); // sum1 is initialized to y
* ** ** * * *** * ** * ***** * ** * *** * * is " **** sum1 * * ** * '\n'; // prints ** **** * is 0"
** ** *** * ** * * ****** ** * * * ** ** is " * *** ** sum2 ** '\n'; // prints * * * is x"
** * * * ***** ** *** ** * ** ** **** * ** is " ** * * sum3 ** ** **** '\n'; // prints * ***** * is y"
** * ** * * ** * ** += x; //adds x to sum1; now sum1 is x
* * ** ** ** **** *** ** += -3; // adds -3 to sum2; now sum2 is x-3
* *** * * * * * * += 2; //adds 2 to sum3; now sum3 is y+2
** * * * * * * * *** (sum1 == sum2)
*** * * *** ** * *** * * * **** * * **** * *** ** **** * ***** and sum2 are the * * *** **
** * ** ** ***** *
** * * * ***** * ** *** * * * * ** * ** * ** * * ******* and sum2 are not the * ***
** ** *** ** * ** * * * (sum1 == sum3)
* * ** ****** * * *** * * * * * * ** **** * ** ** ** *** ** * and sum3 are the * * * *
* * * ** * * * *
* *** ** ** * **** ** ** * *** * ** *** * * ** * * * * * * ** **** and sum3 are not the ** * *
* * ***** * *** * **** 0;

}
answered by (5.2k points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.243.109
©2016-2026

Related questions

0 like 0 dislike
30 answers
[Exercise] Coding (C) - asked May 18, 2017 in C++
ID: 24769 - Available when: Unlimited - Due to: Unlimited
| 5.4k views
0 like 0 dislike
17 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24855 - Available when: Unlimited - Due to: Unlimited
| 3.6k views
0 like 0 dislike
15 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24854 - Available when: Unlimited - Due to: Unlimited
| 3.7k views
12,783 questions
183,442 answers
172,219 comments
4,824 users