11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

294Part IV: InheritanceA namespace is a collection of loosely coupled classes that are somehow logicallysimilar. In this case, I intend to throw all classes that I create concerningstudents, graduate students, classes, course schedules, and so forth into theSchools namespace.The classes that make up the Schools namespace are like members of afamily. One class within a namespace may refer to other members of thesame namespace directly. However, external classes must specify the namespace.You will see the ways of specifying a class’s namespace in the followingSeparatedMain application.Another reason for dividing modules into namespaces is to avoid “name collision.”For example, the class Grade within the namespace Schools does notinterfere with the class Grade in the namespace FoodProduction.Implementing StudentI put the implementation of the Student class in the file Student.cpp:// Student - implement the methods of the Student class#include #include #include #include #include “student.h”namespace Schools{Student::Student(char* pszNameArg, int nIDArg): nID(nIDArg){pszName = new char[strlen(pszNameArg) + 1];strcpy(pszName, pszNameArg);}}// display - return a description of studentchar* Student::display(){// copy the student’s name into a block of heap// memory that we can return to the callerchar* pReturn = new char[strlen(pszName) + 1];strcpy(pReturn, pszName);return pReturn;}The constructor for Student copies off the name and id provided it. The virtualdisplay() method returns a string that describes the Student object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!