< previous page page_104 next page >

Page 104
LISTING 7.2Cat IMPLEMENTATION INCAT.CPP

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:   // Demonstrates inline functions
2:   // and inclusion of header files
3:
4:   #include cat.hpp  // be sure to include the header files!
5:
6:
7:   Cat::Cat(int initialAge)   //constructor
8:   {
9:      itsAge = initialAge;
10:  }
11:
12:  Cat::~Cat()             //destructor, takes no action
13:  {
14:  }
15:
16:  // Create a cat, set its age, have it
17:  // meow, tell us its age, then meow again.
18:  int main()
19:  {
20:     Cat Frisky(5);
21:     Frisky.Meow();
22:     cout << Frisky is a cat who is  ;
23:     cout << Frisky.GetAge() <<  years old.\n;
24:     Frisky.Meow();
25:     Frisky.SetAge(7);
26:     cout << Now Frisky is  ;
27:     cout << Frisky.GetAge() <<  years old.\n;
28:     return 0;
29:  }

Output:
Meow.
Frisky is a cat who is 5 years old.
Meow.
Now Frisky is 7 years old.
Analysis: GetAge() is declared in line 7, and its inline implementation is provided. Lines 8 and 9 provide more inline functions, but the functionality of these functions is unchanged from the previous outline implementations.
Line 4 of Listing 7.2 shows #include cat.hpp, which brings in the listings from CAT.HPP.IOSTREAM.H, which is needed for cout, is included on line 5.
Classes with Other Classes as Member Data
It is not uncommon to build up a complex class by declaring simpler classes and including them in the declaration of the more complicated class. For example, you might

 
< previous page page_104 next page >

If you like this book, buy it!