Classes: A Gentle Introduction

 < Day Day Up > 



Everything you learned above concerning C++ structures can be directly applied to classes. The only change required to convert the Person struct to a class is to change the keyword struct to the keyword class. Observe the following new header file now named personclass.h:

Example 10.17: personclass.h

start example

click to expand

end example

Two additional changes were made on lines 1 and 2 to reflect the fact that this header file now includes a class declaration instead of struct declaration. Example 10.18 gives the implementation code for this class declaration. The only changes made to it were to the name of the file, from personstruct.cpp to personclass.cpp, and to the name of the #include file, from personstruct.h to personclass.h.

Listing 10.18: personclass.cpp

start example
 1  #include "personclass.h"  2  #include <string.h>  3  4  char HairColorStrings[][7] = {{"Black"}, {"Red"}, {"Auburn"}, {"Brown"}, {"Blond"},  5                                {"Silver"}, {"Grey"}};  6  7  char EyeColorStrings [][7] = {{"Black"}, {"Hazel"}, {"Blue"}, {"Brown"}};  8  9 10  void Person::setFirstName(char* _f_name){ 11      strcpy(f_name, _f_name); 12  } 13 14  void Person::setLastName(char* _l_name){ 15      strcpy(l_name, _l_name); 16  } 17 18  void Person::setHairColor(HairColor::HairColor _hair_color){ 19      hair_color = _hair_color; 20  } 21 22  void Person::setEyeColor(EyeColor::EyeColor _eye_color){ 23      eye_color = _eye_color; 24  } 25 26  char* Person::getFirstName(){ 27      return f_name; 28  } 29 30  char* Person::getLastName(){ 31      return l_name; 32  } 33 34  char* Person::getHairColor(){ 35      char* h_color = NULL; 36      switch(hair_color){ 37 38          case HairColor::Black : h_color = HairColorStrings[HairColor::Black]; 39                                   break; 40          case HairColor::Red : h_color = HairColorStrings[HairColor::Red]; 41                                   break; 42          case HairColor::Auburn : h_color = HairColorStrings[HairColor::Auburn]; 43                                   break; 44          case HairColor::Brown : h_color = HairColorStrings[HairColor::Brown]; 45                                   break; 46          case HairColor::Blond : h_color = HairColorStrings[HairColor::Blond]; 47                                   break; 48          case HairColor::Silver : h_color = HairColorStrings[HairColor::Silver]; 49                                   break; 50          case HairColor::Grey : h_color = HairColorStrings[HairColor::Grey]; 51                                   break; 52          default : h_color = HairColorStrings[HairColor::Blond]; 53      } 54      return h_color; 55  } 56 57  char* Person::getEyeColor(){ 58      char* e_color = NULL; 59      switch(eye_color){ 60          case EyeColor::Black : e_color = EyeColorStrings[EyeColor::Black]; 61                                   break; 62          case EyeColor::Hazel : e_color = EyeColorStrings[EyeColor::Hazel]; 63                                   break; 64          case EyeColor::Blue : e_color = EyeColorStrings[EyeColor::Blue]; 65                                   break; 66          case EyeColor::Brown : e_color = EyeColorStrings[EyeColor::Brown]; 67                                   break; 68          default : e_color = EyeColorStrings[EyeColor::Blue]; 69      } 70    return e_color; 71  }
end example

Example 10.19 gives the main() function that uses the Person class to create a Person object named Bob and perform a few operations on the Bob object. This main() function looks exactly like the main() function used to test the struct version of Person. The one change to the main.cpp file is the name of the include file, from personstruct.h to personclass.h.

Listing 10.19: main.cpp testingPerson class

start example
 1  #include <iostream>  2  #include "personclass.h"  3  using namespace std;  4  5  int main(){  6      Person Bob;  7  8      Bob.setFirstName("Bob");  9      Bob.setLastName("Smith"); 10      Bob.setHairColor(HairColor::Black); 11      Bob.setEyeColor(EyeColor::Blue); 12 13      cout<<"First Name: "<<Bob.getFirstName()<<endl 14          <<" Last Name: "<<Bob.getLastName()<<endl 15          <<"Hair Color: "<<Bob.getHairColor()<<endl 16          <<" Eye Color: "<<Bob.getEyeColor()<<endl; 17 18      cout<<endl<<endl; 19      return 0; 20  }
end example

Quick Summary

I told you this would be a gentle introduction to classes! A struct has all the functionality of a class in C++ but there are differences between the two. These differences are discussed in the next section. Programmatic differences aside, the primary difference between the two forms of data types has more to do with how you think about designing and writing programs. Starting now, unless there is a pressing need to put data elements in a struct, you can use classes for all your abstract data type needs.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net