Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 841

D


32:
33: SimpleCircle::SimpleCircle(const SimpleCircle & rhs)
34: {
35: int val = rhs.GetRadius();
36: itsRadius = new int(val);
37: }
38:
39: SimpleCircle& SimpleCircle::operator=(const SimpleCircle & rhs)
40: {
41: if (this == &rhs)
42: return *this;
43: *itsRadius = rhs.GetRadius();
44: return *this;
45: }
46:
47: const SimpleCircle& SimpleCircle::operator++()
48: {
49: ++(*itsRadius);
50: return *this;
51: }
52:
53: // Operator ++(int) postfix.
54: // Fetch then increment
55: const SimpleCircle SimpleCircle::operator++ (int)
56: {
57: // declare local SimpleCircle and initialize to value of *this
58: SimpleCircle temp(*this);
59: ++(*itsRadius);
60: return temp;
61: }
62: int SimpleCircle::GetRadius() const
63: {
64: return *itsRadius;
65: }
66: int main()
67: {
68: SimpleCircle CircleOne, CircleTwo(9);
69: CircleOne++;
70: ++CircleTwo;
71: cout << “CircleOne: “ << CircleOne.GetRadius() << endl;
72: cout << “CircleTwo: “ << CircleTwo.GetRadius() << endl;
73: CircleOne = CircleTwo;
74: cout << “CircleOne: “ << CircleOne.GetRadius() << endl;
75: cout << “CircleTwo: “ << CircleTwo.GetRadius() << endl;
76: return 0;
77: }


  1. You must check to see whether rhsequals this, or the call to a = awill crash
    your program.


32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 841

Free download pdf