11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22: Factoring Classes 293on any other functions (besides C++ functions). Thus, it would make sense toput Student in a module by itself. Because the class will be used in severalplaces, you break the declaration into a student.h file and a separate implementationfile, Student.cpp. By convention, the include file carries thename of the primary class it defines, but in lowercase letters. Ideally, theinclude file defines only one class. This allows the user program to includejust the files that it needs.Historically, all include files carried the extension .h. This was changed in thecurrent C++ standard. System include files such as iostream now have noextension at all. However, many programmers stick with the .h conventionfor include files they write. This allows such include files to be easily differentiatedby the reader of the program.The resulting student.h file appears as follows:// Student - basic student#ifndef _STUDENT_#define _STUDENT_namespace Schools{class Student{public:Student(char* pszName, int nID);virtual char* display();protected:// student’s namechar* pszName;int nID;};}#endifThe #ifndef is a preprocessor control much like #include. #ifndef _STUDENT_ says to include only the following lines if the argument _STUDENT_is defined. The first time that student.h is included, _STUDENT_ is notdefined. However, the #define immediately following the #ifndef thendefines it. This has the effect that student.h is processed only once, nomatter how many times it is included in a given file.Defining a namespaceThe second feature of the Student class is the creation of the Schoolsnamespace.

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

Saved successfully!

Ooh no, something went wrong!