Sams Teach Yourself C++ in 21 Days

(singke) #1
15: };
16:
17: void setValue(Animal& theAnimal, int theWeight)
18: {
19: friend class Animal;
20: theAnimal.itsWeight = theWeight;
21: }
22:
23: int main()
24: {
25: Animal peppy;
26: setValue(peppy,5);
27: return 0;
28: }


  1. Fix the listing in Exercise 5 so that it compiles.
    7.BUG BUSTERS:What is wrong with this code?
    0: // Bug Busters
    1: #include
    2: using namespace std;
    3: class Animal;
    4:
    5: void setValue(Animal& , int);
    6: void setValue(Animal& ,int, int);
    7:
    8: class Animal
    9: {
    10: friend void setValue(Animal& ,int); // here’s the change!
    11: private:
    12: int itsWeight;
    13: int itsAge;
    14: };
    15:
    16: void setValue(Animal& theAnimal, int theWeight)
    17: {
    18: theAnimal.itsWeight = theWeight;
    19: }
    20:
    21: void setValue(Animal& theAnimal, int theWeight, int theAge)
    22: {
    23: theAnimal.itsWeight = theWeight;
    24: theAnimal.itsAge = theAge;
    25: }
    26:
    27: int main()
    28: {
    29: Animal peppy;
    30: setValue(peppy,5);
    31: setValue(peppy,7,9);
    32: return 0;
    33: }

  2. Fix Exercise 7 so that it compiles.


592 Day 16

Free download pdf