| < Day Day Up > |
|
When a derived class object is created it must call its base class constructor to ensure its base class object is properly instantiated. Base class constructors are called in the constructor initializer list. Let us take a look at some example code to see how this is done. This example will also illustrate how code can be reused and extended in an object- oriented design. Figure 13-6 is a class diagram showing the class Person, originally presented in chapter 11, being extend by a derived class named Student.
Figure 13-6: Person/Student Class Diagram
By extending Person, a Student will have all the behavior of Person, plus any additional behavior specific to a Student. Examination of figure 13-6 reveals the addition of a student_count static variable and an instance variable named student_number. Accessor and mutator functions were added to manipulate these variables. Other than that, a Student is a Person. (of course!)
The source code for Person is given in its entirety in chapter 11 so I will not repeat it here with the exception of the person.h file. This is really all we need in that a programmer tasked with writing the Student class may only have access to the Person class header file. That would be all they need to determine what constructors and other public interface functions are available for their use.
Examples 13.6 through 13.9 give the source code for person.h, student.h, student.cpp, and a main.cpp file showing these classes in action. Figure 13-7 shows the results of running example 13.9.
Listing 13.6: person.h
1 #ifndef __Person_H 2 #define __Person_H 3 4 5 class Person{ 6 public: 7 Person(char* _f_name = "John", char* _m_name = "M", 8 char* _l_name = "Doe", char _sex = 'M', int _age = 18); 9 virtual ~Person(); 10 Person(Person& person); 11 Person& operator=(Person& rhs); 12 void setFirstName(char* f_name); 13 void setMiddleName(char* m_name); 14 void setLastName(char* l_name); 15 void setAge(int age); 16 void setSex(char sex); 17 char* getFullName(); 18 char* getFirstName(); 19 char* getMiddleName(); 20 char* getLastName(); 21 char getSex(); 22 int getAge(); 23 24 private: 25 char* f_name; 26 char* l_name; 27 char* m_name; 28 char sex; 29 int age; 30 char* full_name; 31 bool name_changed; 32 }; 33 #endif
Listing 13.7: student.h
1 #ifndef STUDENT_H 2 #define STUDENT_H 3 #include "person.h" 4 5 class Student : public Person{ 6 public: 7 Student(char* _f_name = "Joe", char* _m_name = "M", 8 char* _l_name = "Student", char _sex = 'M', int _age = 18); 9 ~Student(); 10 void setStudentNumber(int _student_number); 11 int getStudentNumber(); 12 int getStudentCount(); 13 14 private: 15 static int student_count; 16 int student_number; 17 }; 18 #endif
Listing 13.8: student.cpp
1 #include "student.h" 2 #include "person.h" 3 #include <iostream> 4 using namespace std; 5 6 int Student::student_count = 0; 7 8 Student::Student(char* _f_name, char* _m_name, char* _l_name, char _sex, int _age) 9 :Person(_f_name, _m_name, _l_name, _sex, _age){ 10 student_number = ++student_count; 11 } 12 13 Student::~Student(){ 14 student_count--; 15 } 16 17 void Student::setStudentNumber(int _student_number){ 18 student_number = _student_number; 19 } 20 21 int Student::getStudentNumber(){ return student_number;} 22 23 int Student::getStudentCount(){ return student_count;}
Listing 13.9: main.cpp
1 #include <iostream> 2 using namespace std; 3 #include "student.h" 4 5 int main(){ 6 7 Student s1, s2("Coralie", "Sarah", "Miller"); 8 9 cout<<s1.getFirstName()<<" "<<s1.getLastName()<<endl; 10 cout<<s2.getFirstName()<<" "<<s2.getLastName()<<endl; 11 12 return 0; 13 }
Figure 13-7: Results of Running Example 13.9
Refer to the student.cpp file shown in example 13.8. The Student class constructor definition begins on line 8 and continues through line 11. The Student constructor declares parameters that match those of the Person class. This means that for a Student object to be created it needs a first name, middle name, last name, sex, and age. However, these attributes are not found in the Student class. The Student constructor must use its constructor parameters as arguments to the Person constructor. This is shown on line 9 of example 13.8. The colon starts the Student constructor initializer list and the first thing that appears to the right of the colon is the call to the Person base class constructor. The rest of the Student constructor goes on to initialize the student_number using the static student_count variable.
When a derived class must initialize base class attributes it must make an explicit base class constructor call in its initializer list. The derived class constructor can use its parameters as arguments to its base class constructor. If a default constructor exists for both the base and derived class then some or all of the constructor arguments can be omitted when objects are instantiated.
| < Day Day Up > |
|