Implementing Class Methods

Every class method that you declare must also be defined.

A member method (or member function) definition begins with the name of the class and is then followed by two colons, the name of the function, and its parameters.


A member method (or member function) definition begins with the name of the class followed by two colons, the name of the function, and its parameters. Listing 7.1 shows the complete declaration of a simple Cat class and the implementation of its accessor function and one general class member function.


7.1. Implementing the Methods of a Simple Class
 0:  // Demonstrates declaration of a class and
 1:  // definition of class methods,
 2:
 3:  #include <iostream>      // for std::cout
 4:
 5:  class Cat                   // begin declaration of the class
 6:  {
 7:  public:                     // begin public section
 8:      int GetAge();           // accessor function
 9:      void SetAge (int age);  // accessor function
10:      void Meow();            // general function
11:  private:                    // begin private section
12:      int itsAge;             // member variable
13:  };
14:
15:  // GetAge, Public accessor function
16:  // returns value of itsAge member
17:  int Cat::GetAge()
18:  {
19:      return itsAge;
20:  }
21:
22:  // definition of SetAge, public
23:  // accessor function
24:  // sets itsAge member
25:  void Cat::SetAge(int age)
26:  {
27:     // set member variable its age to
28:     // value passed in by parameter age
29:      itsAge = age;
30:  }
31:
32:  // definition of Meow method
33:  // returns: void
34:  // parameters: None
35:  // action: Prints "meow" to screen
36:  void Cat::Meow()
37:  {
38:      std::cout << "Meow.
";
39:  }
40:
41:  // create a cat, set its age, have it
42:  // meow, tell us its age, then meow again.
43:  int main()
44:  {
45:      Cat Frisky;
46:      Frisky.SetAge(5);
47:      Frisky.Meow();
48:      std::cout << "Frisky is a cat who is " ;
49:      std::cout << Frisky.GetAge() << " years old.
";
50:      Frisky.Meow();
51:      return 0;
52:  }


Meow.
Frisky is a cat who is 5 years old.
Meow.

Lines 5–13 contain the definition of the Cat class. Line 7 contains the keyword public, which tells the compiler that what follows is a set of public members. Line 8 has the declaration of the public accessor method GetAge(). GetAge() provides access to the private member variable itsAge, which is declared in line 12. Line 9 has the public accessor function SetAge(). SetAge() takes an integer as an argument and sets itsAge to the value of that argument.


Line 11 begins the private section, which includes only the declaration in line 12 of the private member variable itsAge. The class declaration ends with a closing brace and semicolon in line 13.

Lines 17–20 contain the definition of the member function GetAge(). This method takes no parameters; it returns an integer. Note that class methods include the class name followed by two colons and the function's name (line 17). This syntax tells the compiler that the GetAge() function you are defining here is the one that you declared in the Cat class. With the exception of this header line, the GetAge() function is created like any other function.

The GetAge() function takes only one line; it returns the value in itsAge. Note that the main() function cannot access itsAge because itsAge is private to the Cat class. The main() function has access to the public method GetAge(). Because GetAge() is a member function of the Cat class, it has full access to the itsAge variable. This access enables GetAge() to return the value of itsAge to main().

Line 25 contains the definition of the SetAge() member function. It takes an integer parameter and sets the value of itsAge to the value of that parameter in line 29. Because it is a member of the Cat class, SetAge() has direct access to the member variable itsAge.

Line 36 begins the definition, or implementation, of the Meow() method of the Cat class. It is a one-line function that prints the word Meow to the screen, followed by a new line. (Remember that the character prints a new line to the screen.)

Line 44 begins the body of the program with the familiar main() function. In this case, it takes no arguments and returns void. In line 45, main() declares a Cat named Frisky. In line 46, the value 5 is assigned to the itsAge member variable by way of the SetAge() accessor method. Note that the method is called by using the class name (Frisky) followed by the member operator (.) and the method name (SetAge()). In this same way, you can call any of the other methods in a class.

Line 47 calls the Meow() member function, and lines 48 and 49 print a message using the GetAge() accessor. Line 50 calls Meow() again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183