CS 1440 Lab 8
· Understand why we use classes
· Recall how we define a class
Remember learning to ride a bike? You don’t get it right the first time. You fall down, maybe even skin a knee or elbow. You continue trying however until WITH ENOUGH PRACTICE you learned to do it.
Learning to use C++ classes is a lot like riding a bike. You’re probably still wobbly. Last week, you may have even skinned a knee. But, with continued practice you will get it. How much practice? Is two lab periods and several lectures enough? Probably not; you will most likely need to CAREFULLY read chapter 6. Maybe even more than once. Try the exercises at the end of the sections and chapter. You did when you learned to ride that bike. You’ll just need to do it again.
Classes help us compactly represent real-world entities in our programs. In writing a program for a university you will probably need things like students, courses, etc. These real-world objects are represented as a collection of “smaller” data values (for example, the string that is your name, the double that is your GPA, etc.). In addition to combining the related data values that make up a real-world entity, classes let us combine the functions that are appropriate for these types of data too. So, for example, a student class could have a “compute_GPA” function or a “display_current_schedule” function.
In writing a program for a bank, you might need classes like customer, account, etc. The account class might have a function “compute_interest_earned” function bundled into it.
Here is how you define a class in C++:
. . .
. . .
While not a requirement, a good, general “rule of thumb” is that data members should be private and function members should be public.