![]() |
![]() |
![]() |
![]() |
![]() |
|
The following revision of the railroad_car class definition shows
you how to define a static member variable and how to initialize that
member variable. Modified constructors and destructors maintain the static
member-variable value, thus keeping track of the total number of railroad
cars currently in existence:
class railroad_car {
public: char *serial_number;
// Constructors:
railroad_car ( ) {++counter;}
railroad_car (char *input_buffer) {
// Keep track of the number:
++counter;
// Create new array just long enough:
serial_number = new char[strlen(input_buffer) + 1];
// Copy string into new array:
strcpy (serial_number, input_buffer);
}
// Destructor:
virtual ~railroad_car ( ) {
cout << "Destroying a railroad car; "
<< --counter << " remain." << endl;
delete [ ] serial_number;
}
// Other:
virtual char* short_name ( ) {return "rrc";}
virtual double capacity ( ) {return 0.0;}
private: static int counter;
};
int railroad_car::counter = 0;