0 like 0 dislike
282 views

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

寫一個Adder類別來計算所有整數的總和。請使用下列的main函數。

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.

你一定要寫main函數裡所用到的函數。如需要可以用const,但不能用cast運算子。記得新增所需要的標頭檔。

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

 

[Normal] Coding (C++) - asked in Introduction to Computer Programming II (C++)
ID: 52072 - Available when: Unlimited - Due to: Unlimited
| 282 views
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.179.53
©2016-2024

No related questions found

12,783 questions
183,443 answers
172,219 comments
4,824 users