0 like 0 dislike
4.6k 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 | 4.6k views
0 0
Called for Help
0 0
Called for Help

31 Answers

0 like 0 dislike
Hidden content!
* * * * ** *** * ** **
* * * * *** *
*** * * std;

class Adder

{
* * **
* * *** ***** *** * *** *
* *** ** * * ** * * * ***
* * ***** * ** ** ** ** ***** **** * ** *** * = 0;
* ** * ** * * ** * **** *
* ** ** * * ** * b)
* * * ** ** * *
* * ** * * * *** **** ** * * * ** ** * = b;
***** ** * *
* ******** * ** * * ** * ** * *


** ***** ** ***** * * * ** * ** a1)
** * * **** * *** *
** * * * *** * **** * * * ** * ** * * ** ** * * * *** = a1;
* ** ***** * * **
* ** *** * *** * * * * ****** **** ******
** * *** ******** *
** ** * *** **** * ***** * * * * *** * += b;
* * *** * **** *** **
* **** ** *** ** ** ** * ** * * * ***** **
** *** ** **** *
* *** **** * **** ******* *** *** * * * ****
***** **** * ** ** * ** *** ** ** ** * * * * * **** * ** * 1;
** ******* ** * ** ** * *** * * ** * * **
* ** ****** * ** ** * ****** * * *** ***** * ***** * ** ** 0;
* ** *** * * * *****
** ****
* * ***** ***** * * ** **** a;

};



int * *

{
* * * * * * * ** * * * a;

}



int main()

{

int x,y;

cin ***** ** x **** ** * y;
* sum1; // sum1 is ** **** to 0
* ** // sum2 is ** to x
** * // sum1 is ** ** * to y

cout *** ** ** ***** is ** ** ** ***** ***** * *** * ** '\n'; // * * * ** is * * **

cout * **** *** ** is * ** **** ** ** * ** ** '\n'; // ** * ***** is * * * *

cout * *** * ** * ** is ********** ** **** ** * *** ** '\n'; // * * ** * * * * is * * * **

sum1 += x; * * x to sum1; now sum1 is x

sum2 += -3; // adds -3 to sum2; now sum2 is x-3

sum3 += 2; *** 2 to sum3; now sum3 is y+2

if (sum1 == sum2)

cout * * * * * ** * * and sum2 are the ** * ***

else cout * ***** * ** * * ** and sum2 are not the *** ** *

if (sum1 == sum3)

cout * * *** * * * * and sum3 are the * **** *

else cout ** **** * ** ** * * and sum3 are not the * * * **

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
** ** *** ** ** **
* * * *
** *

** *
* ** * ** * x * * *** *
* ** // * * ** to 0

* *** // * * is *** to x
** * ** // ** * is ** to y

** * * *** *** *** is ** * * *** ** * * *** **** ** // * ******* is *

* * * ** * * * * * * ** *** ** * * * ** * // * * * ** * is *** ***
*** ** * *** * * * ** * * * * * ** * **** * *** ** * * * ** * * * * ** is ** *

*** += x; *** x to * is x

* += * *** -3 to *** * *** is
*** += 2; 2 to ** * is *

** * ** * *
**** **** * * ** ** * * * ******
** * ** * * ** * ** ** ** * * ****
* * * *
** * * * ** *** * * ** ** *
* * ***** ** ** * * ** * ** * ** * **

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
** ** ** * *** * *
* * * ** *



* * *** {

** * * ** * * ** * *
** * *** int ** *

**** * ** * *** ****
*** **** ** ****
** * *** ** * ** *** * * * = 0;
** ** ** * * ** } * *
* * ** n){
* ** * * *** ** ** **** ** = n;
* ** **** }
* * **** ** ** *** ** * * * *** * n) * * += n;}
* * ****** * * ** * * ** * ** ** *** x, * * * * * ** y); * * * *
* * * *** ** ** * * ** ** * * * * ** ** * * * ** * * * * * ** **** x);
*


*** * * * * * ** x, * *** ** * ** **
** ** * *** ** * ==

}
** * *** *** * * ****** ** ** *** * * * ** x){

** * * ** * * * * * * ***

}


* * **

{
* ** *
* **** ** * x * **** ** y;
* // is * * * to 0

* ** // ** * is *** ** to x
*** * // ** * is *** to y

* ** * ** * ** is * *** ** ** * * ** ** * * * * * // ** *** * * ** is *** ** *

* * * * *** * *** is * * ******** ** ** * * * // * * * ** * is * **** *
** **** * * ** is *** ** ********* * **** *** * ** // *** * *** * is ****

* += x; * x to * *** is x

*** += * // * -3 to * * is *
*** += 2; * ** 2 to * * * *** is y+2

if * * == ***
*** * * ** *** ******* and ** are the *** *** **
* * ** * * *** ** * * ** * are not * * ** * **

if * == * **
** **** * * * * *** and * are the ** ** * **
** * * * * * * ** and ** * * ** ** **

}
answered by (-134 points)
0 like 0 dislike
Hidden content!
* **

{
* * * *
*** * ** x * ******** y;
* // * is **** ** to 0

**** * ** // ** * * * x
* * * * * * ** y

**** * ** * * * * *** * * * * * ***** *** * ** * **** ** * *** ** *

* ** ** **** ** * * ** * * ** *** ** * * ** * is *** *
* *** * *** * is * * * ** * *** ** * * * * ***** ** is * * *

* += x; ** x to * ** ** * is x

+= ** // * -3 to ** ** * is *
* += **** 2 to ** ** ** is *

if == ** **
** ** * * **** * **** * * * * * *** *
** * * ** * * ** *** ** * * * ** ** *
** *** *
** * * ** ** * ** ** *** * ** * ** * * *
** ** * ** * * * *** * * * * * *** *

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
* *

{
*
* * **** x * * *** * * y;
* * ** // ** is * ** * to 0

* *** // ** * ******* x
* * *** * * * * * y

* * *** * * **** * * * * * * ********* *** * ** *** ** * * * ** * *** ***

** **** * * * *** *** ** * ** * * *** ** * ** ** * * * * is * **
* ** **** ** ** is ** * * * * * * * *** * * ** * ** * * is *** **

*** += x; ** x to ** * is x

* += // -3 to * ** ** is *
* * += * * 2 to * * * is

if * * == ***
* * ** ** ***** * * * * *** ** *****
* * * * * * * * * *** * * * * *** * * *** ***
* ** **
** ***** ***** ** * * ****** *
**** * * **** ** * * ***** ** ** * * * * * * *

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
* *** *** * * *
* * *** ** * *



using **** std;



class Adder

{
* * ** ** * * ** **** * *
* *** * ******** ****** * ** * ** * *** ** * a;
*** * * * * ** * * **


** ** ** ** * ***** *** ** x)
** * * * * * **** * **
* ** * * * * * * ** ** * * ** **** * * ** ****** = x;
* ** ** ** * * * *** **
**** ** ** * * * * *
* *** ** * ****** *
** * * *** * ** **** * * ****** * *** * = 0;
* ** * * * ** *** ** **


* * *** **** * * * * * * * * (const * * * * x)
*** **** * * ** *
** *** * * ** * ******* ** **** * *** * * * * *** temp;
** * ** *** * ******* * * ** ****** * *** * = * **** ** a+x;
**** ** ** * * ***** * ** ** *** * ** * ****** ****** temp;
** * * ** * ** * * *
* ** ** * * * * * * *** * *** ** *** * *** * x)
** ** *** ******
* * * * * *********** ** ** ***** * temp;
** * ** ********* ***** * * * **** *** * ** ** * ** = ** **** * a-x.a;
**** *** *** * **** *** * * * * * ** * *** *** ** **
** *** * * * ***** * * * **** * *** * ** * * * *** * ** **** *** *** 1;
* * * ** * * ** **** *** * ** * * * ** ** **
* * ** * *** * * * ** ** ** **** ******** *** ** ***** * * * * * 0;
****** * ** * ** *
**** ******* *** ** * * **** g()
* *** * * * *
* * *** * *** * * **** *** *** * **** *** * * a;
*** ** * ** *** * **



};



int main()

{

int x,y;

cin * * ***** * x ** ** y;
* sum1; // sum1 is * to 0

Adder ** // sum2 is * **** to x

Adder ** // sum1 is *** * to y

cout * * * * * *** is * * **** * * *** ** ** * '\n'; // prints * ** * is **

cout * * * ** * * is ** ** * ** * * * ** *** *** * '\n'; // ***** **** ** is * ** **

cout * ****** **** is * * **** * ** * * * '\n'; // prints * * * is * ** ***

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 * * * * * * * * and sum2 are the * ** * * *

else cout ** *** * ** ***** and sum2 are not the ** ** * **

if (sum1 == sum3)

cout * *** ***** and sum3 are the ** ** *

else cout * ** * ** *** * and sum3 are not the ***


** ** *** * * *** *** 0;

}
answered by (-364 points)
0 like 0 dislike
Hidden content!
** *

{
*
* ** * ** * x ***** y;
*** * // * is ** ** to 0

** * * // **** ** ** * * x
** ** * * *** * * y

* * **** *** ******* * ** * ** **** ** ***** ** * * **** * *

* * ** * **** * * **** ** *** * ** * *** ** * *** ** ** * is * *
* ** * ** is * ** * *** *** * * ** ** ** * * * * * ** * * ** is * **

* += x; * x to * is x

* += ** // *** -3 to * ** is *
* += * * 2 to ** ** is **

if * * ==
**** ** ** ** ** ** ** ** ** * * * *
*** *** * ** * * * ** ** ** ** *** * *
* *
*** * ** * * * * * * * * ** *** *
* ** * *** * ** ** * * ** * * * *

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
* ** *** ** *

using namespace std;

class Adder{
*** * ** * * **
** * *** **** ** * ** *** *
* ***** ** ******** ** * * ** * *** * * ** *
* ** * * * **** ** *** ***** * * **** * * * ** *** *** * * * *
*** * ** * ** * ** * * ** * * * * * * * **
** ** * * * ** * * * * * * * ** * * * * **** x){
** * * * * * * ** * ** **** * * ** * * * ** ****
* * * * * * * * * * ** * * *
* * ** * ** *** * * ** ** * * * * * * ** geta();
** * *** *** *** ** * * * ** * * * * seta(int n1){
* **** *** * * **** ** * * * **** ** * * ** ***** * *** ** *
*** * * ****** ** * ***** * * * * * **** *** ** ***
**** * * ** * * ** ***** * * * ** ** ******* * * *** * *** int& x){
* ***** * ** *** ** ** *** ** ** * * * ****** *** * * * ***
**** * * * ** ***** ** * * ** *** *** * ** * * * ** ** * * ** * * * += x;
** *** * * *** * * * **** ** * ** ******* ****
* * ** * * * * **** ***** ** operator==(const Adder& b){
***** * *** * * * * ** * ** * * * * * * ** ** ** * ** *** * ***** ***** *
* * **** ** * ****** ** * * * ** * *** * ** ** ** ** * ***** **** * ***** ***** 1;

** ** *** * * ** ***** *** ** * **** ** *** *** * **** ******
* * **** **** ** * *** * * * **** * * * * *** ***** **** ** ** * * * * * * * * **** * * *** * 0;
**** * **** ** ** ** *** ****** **** *
* *** * * ***** * ** * * ** **
************ ** * * * ** ****** ****** a;

};

Adder::geta(){
** **** *** *** * ***** a;

}

int main()

{

int x,y;

cin * * * * * x * * **** y;

Adder sum1;

Adder sum2(x);

Adder sum3(y);

cout * *** * * "sum1 is " * *** *** sum1.geta() ** * * *** '\n';

cout ** * * * "sum2 is " * ** ** * sum2.geta() * *** ** '\n';

cout * **** ** "sum3 is " *** * sum3.geta() * * ** '\n';

sum1 += x;

sum2 += -3;

sum3 += 2;

if (sum1 == sum2)

cout * *** * * "sum1 and sum2 are the * * * ** *

else cout ** * ** * * "sum1 and sum2 are not the * * * * * **

if (sum1 == sum3)

cout * * "sum1 and sum3 are the ** ***

else cout *** * * "sum1 and sum3 are not the * * * *

}
answered by (-384 points)
0 like 0 dislike
Hidden content!
* * * * ** * * * * *

using namespace std;

class Adder{
*** * * ** ** * * ***** *** *
* * *** ** * **** *** * * * *** * ** ** * *
* * * * * ** * * * ** ** ******* ** * * *
* **** * ** * * * **** **** ** ** * * * ****** * * * ** * **
*** **** * * * * ** ** ** * * * ** *
* *** ** ** * * *** * * **** * * ** ** x){
**** **** * * **** ** * * * * ** ** * ***** * *** * * * **
** * ** * *** * ** * * ******* * ****
* * * ** * ** * * ** ** * * **** * * geta();
* * * * * * ** * * * **** * seta(int n1){
* *** ** ** ****** * ** * * * *** ** * ** ** * * * ** * ** * *
* * * *** * * * * **** ******* * * **** *
* * *** **** * *** ** ***** **** * ** ** * * * ** ** * * ** * x){
* ** * **** * ***** * * ***** ** ** ****** * c;
*** * * ** * ***** * * * * *** * * * *** * ** * *
*** ** * *** *** * *** * ** ****** * ** *** c;
****** * *** * ** * ** * * *** * * * * ***** **
*** ******* ** *** * * ** * * * ******** ** * ** * * ** * ** * b){
** * * *** * ** *** *** ****** ** * * ** **** * ** ** * ** * ***** * * * * *
** *** * ** * * ** ** * * * * * ** **** *** * *** * * * **** * *** ** * ** ** * * * ** 1;

** ** ** ** * * * * *** * * **** * * * ** ** *** * * ** * **
* *** ** * * **** **** *** *** * *** * ** * ** ****** *** * * * ****** * ** ** ** ** 0;
** ** *** * ** * * * ** * *** ***** *
* * ** * * ****** * * * * **
** * **** * * ** ** ** ** * *** *** * * *** ** a;
***** **** ** *********** * * ** * * ** ** * c;

};
***
*** * ** * ****** * ** * a;

}









int main()

{

int x,y;

cin **** * x **** * * ** y;

Adder sum1;

Adder sum2(x); // sum2 is ****** to x

Adder sum3(y); // sum1 is initialized to y

cout **** * ***** *** is " ** ** * sum1.geta() *** *** * '\n'; // prints ** ** ** is 0"

cout ** * * ** ** * * is " * ** * ** sum2.geta() * * '\n'; // prints * * * is x"

cout * ** **** is " * * * sum3.geta() ** * **** '\n'; // prints ** * **** 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 * *** ***** * * ** and sum2 are the *** *

else cout * * * * ** and sum2 are not the **** **

if (sum1 == sum3)

cout ** ** * * * * * * * * and sum3 are the ***** *

else cout * * ****** *** and sum3 are not the * ** ****

}
answered by (-384 points)
0 like 0 dislike
Hidden content!
* ** * ***** * **

using namespace std;

class Adder{
* ** ** * * ** *
**** * *** * ** * *** ** * ** * ** ** ***** geta();
*** * * *** *** * ** * * ** *** *** seta(int n1){
* ** *** **** * * * * *** *** *** *** ********** ******* *** ** **** ** *
*** *** * ** * ** * * * * * ** *
* * * * *** * ** ** * ** * *** * * ** * * * * * **** ** ** * x){
* * * * * ** * * ***** * * ** *** ** ** c;
* ***** *** * * * *** * * * ** ***** **** * **** *
** ***** ** * * ** * * * * *** **** * * * c;
* ** * * ** * *** ** * * * * *** * * * **
* ** * ** * ** * ** ** ***** *** **** ** ****** * * * ** * b){
* * * * ***** * * ***** ** * **** *** ** ***** *** * ** ** c;
***** * * ** ** ** * ** * * * * * *** * ** * ** *** * * **
**** **** * * * * * * ** **** * **** *** *** * **** ****** * ******** * * * * * * * ** * * *** ** * * * * 1;

* * * ****** *** * *** * * * * * ** *** ** **** * * * **** ** * **
* ** ***** **** * * *** *** * * * ** ******* * * * ** ******* * ** * ** ****** * ** *** 0;
* * * *** **** *** ****** **** ******* *
* * *** ** * *** * ** *
* ** ** *** ** ** ** * * ** **** ** * * * a;

};
** * **
* * **** * *** a;

}









int main()

{

int x,y;

cin * ** * * x * *** y;

Adder * ** **
** * **

* // sum2 is * ** to x
* ** // sum1 is *** * * * to y

cout * *** * ** * is " *** * **** ***** * '\n'; // prints * * **** is ** * *

cout * * *** * *** is " *** ** * * *** * '\n'; // prints * is ** * *

cout *** ** * **** * is " * ****** * **** * * * ** '\n'; // prints *** * is * * * *

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 * * *** * ** * and sum2 are the *** *

else cout * * **** ******* *** and sum2 are not the * ** ***

if (sum1 == sum3)

cout *** **** * ***** and sum3 are the **** ** *

else cout ** * ** ** * * **** and sum3 are not the * ****** *

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

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
| 4.3k views
0 like 0 dislike
17 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24855 - Available when: Unlimited - Due to: Unlimited
| 2.9k views
0 like 0 dislike
15 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24854 - Available when: Unlimited - Due to: Unlimited
| 2.9k views
12,783 questions
183,442 answers
172,219 comments
4,824 users