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.

Accessing the Members of a ClassChapter 12: Adding Class to C++ 163The following syntax is used to access the property of a particular object:NameDataSet nds;nds.creditCard = 10;cin >> nds.firstName;cin >> nds.lastName;Here, nds is an instance of the class NameDataSet (for example, a particularNameDataSet object). The integer nds.creditCard is a property of the ndsobject. The type of nds.creditCard is int, whereas that of nds.firstNameis char[].Okay, that’s computerspeak. What has actually happened here? The programsnippet declares an object nds, which it will use to describe a customer. Forsome reason, the program assigns the person the credit card number 10(obviously bogus, but it’s not like I’m going to include one of my credit cardnumbers).Next, the program reads the person’s first and last names from the defaultinput.I am using an array of characters rather than the class string to handle thename.From now on, the program can refer to the single object nds without dealingwith the separate parts (the first name, last name, and credit card number)until it needs to.The following program demonstrates the NameDataSet class:// DataSet - store associated data in// an array of objects#include #include #include #include using namespace std;// NameDataSet - store name and credit card// informationclass NameDataSet{public:char firstName[128];char lastName [128];int creditCard;};

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

Saved successfully!

Ooh no, something went wrong!