|
|
 |
|
|
|
|
10: Counter(int initialValue);
11: ~Counter(){}
12: int GetItsVal()const { return itsVal; }
13: void SetItsVal(int x) {itsVal = x; }
14: Counter Add(const Counter &);
15:
16: private:
17: int itsVal;
18:
19: };
20:
21: Counter::Counter(int initialValue):
22: itsVal(initialValue)
23: {}
24:
25: Counter::Counter():
26: itsVal(0)
27: {}
28:
29: Counter Counter::Add(const Counter & rhs)
30: {
31: return Counter(itsVal+ rhs.GetItsVal());
32: }
33:
34: int main()
35: {
36: Counter varOne(2), varTwo(4), varThree;
37: varThree = varOne.Add(varTwo);
38: cout << varOne: << varOne.GetItsVal()<< endl;
39: cout << varTwo: << varTwo.GetItsVal() << endl;
40: cout << varThree: << varThree.GetItsVal() << endl;
41:
42: return 0;
43: } |
|
|
|
|
|
|
|
|
varOne: 2
varTwo: 4
varThree: 6 |
|
|
|
|
|
|
|
|
Analysis: The Add() function is declared on line 14. It takes a constant Counter reference, which is the number to add to the current object. It returns a Counter object, which is the result to be assigned to the left side of the assignment statement, as shown on line 37. That is: varOne is the object, varTwo is the parameter to the Add() function, and the result is assigned to varThree. |
|
|
|
|
|