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.

162Part III: Introduction to ClassesThe Format of a ClassA class used to describe a name and credit card grouping might appear asfollows:// the dataset classclass NameDataSet{public:char firstName[128];char lastName [128];int creditCard;};// a single instance of a datasetNameDataSet nds;A class definition starts with the keyword class followed by the name of theclass and an open-close brace pair.The statement after the open brace is the keyword public. (Hold off askingabout the meaning of the public keyword. I’ll make its meaning public a littlelater. Later chapters describe options to public, such as private. Thus, thepublic must stay private until I can make the private public.)The alternative keyword struct can be used. The keywords struct andclass are identical except that the public declaration is assumed in the structand can be omitted. You should stick with class for most programs for reasonsthat will become clear later in this chapter.Following the public keyword are the entries it takes to describe the object.The NameDataSet class contains the first and last name entries along withthe credit card number. As you would expect, the first and last names areboth character arrays — the credit card number is shown here as a simpleinteger (“the better to steal you with, my dear”).A class declaration includes the data necessary to describe a single object.The last line of the snippet declares the variable nds to be a single entry ofclass NameDataSet. Thus, nds might be an entry that describes a singleperson.We say that nds is an instance of the class NameDataSet. You instantiate theclass NameDataSet to create nds. Finally, we say that firstName and theothers are members or properties of the class. We say a whole lot of sillythings.

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

Saved successfully!

Ooh no, something went wrong!