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 17Making Constructive ArgumentsIn This Chapter Making argumentative constructors Overloading the constructor Creating objects by using constructors Invoking member constructors Constructing the order of construction and destructionAclass represents a type of object in the real world. For example, in earlierchapters, I use the class Student to represent the properties of astudent. Just like students, classes are autonomous. Unlike a student, a classis responsible for its own care and feeding — a class must keep itself in avalid state at all times.The default constructor presented in Chapter 16 isn’t always enough. Forexample, a default constructor can initialize the student ID to zero so that itdoesn’t contain a random value; however, a Student ID of 0 is probably notvalid. It’s up to the class to make sure that the ID is initialized to a legal valuewhen the object is created.C++ programmers require a constructor that accepts some type of argumentin order to initialize an object to other than its default value. This chapterexamines constructors with arguments.Outfitting Constructors with ArgumentsC++ enables programmers to define a constructor with arguments, as shownhere:class Student{public:Student(char *pName);};// ...class continues...

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

Saved successfully!

Ooh no, something went wrong!