SUMMARY

You now understand the basic ideas behind classes in C++. You’re going to see more and more about using classes throughout the rest of the book.

EXERCISES
1. Define a struct Sample that contains two integer data items. Write a program that declares two objects of type Sample, called a and b. Set values for the data items that belong to a and then check that you can copy the values into b by simple assignment.
2. Add a char* member to struct Sample in the previous exercise called sPtr. When you fill in the data for a, dynamically create a string buffer initialized with "Hello World!" and make a.sptr point to it. Copy a into b. What happens when you change the contents of the character buffer pointed to by a.sPtr and then output the contents of the string pointed to by b.sPtr? Explain what is happening. How would you get around this?
3. Create a function that accepts a pointer to an object of type Sample as an argument and that outputs the values of the members of the object that is passed to it. Test this function by extending the program that you created for the previous exercise.
4. Define a class CRecord with two private data members that store a name up to 14 characters long and an integer item number. Define a getRecord() function member of the CRecord class that will set values for the data members by reading input from the keyboard, and a putRecord() function member that outputs the values of the data members. Implement the getRecord() function so that a calling ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.