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.

Chapter 17: Making Constructive Arguments 223}semesterHours = 0;gpa = 0.0;// ...other public members...protected:char name[MAXNAMESIZE];int semesterHours;float gpa;};int main(int argcs, char* pArgs[]){Student s(“O. Danny Boy”);Student* pS = new Student(“E. Z. Rider”);}// wait until user is ready before terminating program// to allow the user to see the program resultssystem(“PAUSE”);return 0;The constructor looks like the constructors shown in Chapter 16 except forthe addition of the char* argument pName. The constructor initializes thedata members to their empty start-up values, except for the data membername, which gets its initial value from pName.The object s is created in main(). The argument to be passed to the constructorappears in the declaration of s, right next to the name of the object.Thus, the student s is given the name Danny in this declaration. The closedbrace invokes the destructor on poor little Danny.The arguments to the constructor appear next to the name of the class whenthe object is allocated off the heap.Many of the constructors in this chapter violate the “functions with more thanthree lines shouldn’t be inlined” rule. I decided to make them inline anywaybecause I think they’re easier for you to read that way. Aren’t I a nice guy?Placing Too Many Demands onthe Carpenter: Overloadingthe ConstructorI can draw one more parallel between constructors and other, more normalmember functions in this chapter: Constructors can be overloaded.

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

Saved successfully!

Ooh no, something went wrong!