An Introduction to Classes

What is a class?

Today I will introduce what the "++" in C++ really means. Be sure to read pages 98-105 in A Computer Science Tapestry for a more complete explanation of classes. C++ is an extension of the language C. We have used some of the additional utilities in C++ such as cout and cin, but we haven't touched the greatest contribution that C++ has made. This contribution is something called "classes." We have already used similar concepts in Visual Basic, and although you may not have known it, we actually used this idea when we implemented apstring.

So, just what is a class? A class is a collection of related functions and data that are packaged together. For example, in the party assignment we had a lot of functions that had to do with pizza. We could have collected all of these functions (price function, computing size from radius, number of people per pizza etc.) along with related data (the radius of a pizza, the amount each person needs to eat, the price per square inch etc.) into a class called the pizza class.

As another example, as we did in Visual Basic, a button on the screen can be thought of as a class. It has certain properties (data), as well as functions which we can perform on the button (click etc.).

A School Class

In today's class we will use a class called "School" which has a collection of functions related to school. The three functions that we will use are Start, Questions and Finish. An explanation of these functions follows.

Start- This function takes two integer parameters, the first is the assignment number that we are using, and the second is the number of announcements that will be made before class.

Questions- This function takes one integer parameter. This parameters is the number of questions that will be asked in class.

Finish- This function takes one parameter which is an apstring. This parameter is the due date of the assignment.

Below, I have a sample main function which uses the school class.

#include "school.h"
int main()
    School klopfer;
    return 0;

Using the School Class

To use this main program you should start a new program and create this main. Before you compile it, you should download (by right clicking on the link and selecting save link as...) school.h and school.cpp and put them in the same folder that you just created. Then you must go to the project menu and select add to project and files. Find school.cpp only and select it by double-clicking on it. Now try compiling your program.

I have included several commands that you have not learned yet in the school class (school.cpp). But this doesn't matter. As long as I tell you how to use the functions start, questions and finish, you can use this class. That is one of the great things about classes, if they are well documented, they can stand on their own and be passed from person to person for use in other programs.

All You Need to Know about the School Class

Let's take a look at the main program above. The first line declares the "variable" klopfer of type School. School is the class that I just defined. A "variable" that is of a class type is called an object (hence the name object oriented programming that some of you may have heard). Then next line says call the start function associated with klopfer using the parameters two and three. The notation that I have used "klopfer.Start" is called dot notation and is read "klopfer dot start." When he have defined an object we call its functions using this notation. This is similar to the way in which we called functions associated with buttons in Visual Basic.

The next two lines are similar, only different functions are called. The program finally ends in normal fashion. Let's now take a look at the very first line that I wrote above #include "School.h". This is another header file like we have done before. This time, however, it is included in quotes not angle brackets. This is because the actual file School.h is in the folder that the rest of our project is in. You can actually look at this file by opening it from Visual C++. It contains the prototypes for the functions that are used in the class, along with some variables as well. This file is sometimes called the interface because it describes the way in which your program must interface with the class.

One of the important things to note in looking over this file is that there is a section marked private and one marked public. The private section is accessible only to other functions within the class itself. The public section is the part that is accessible to your main program that is using this class. This is the section that you must pay attention to if you want to use the class.

Now try writing a new main program that uses the school class. Add some instructions and some places for a user to input things like number of questions, assignment number etc. You may have more than one opportunity for a person to ask questions. You only need to change the main function, you do not need to touch anything in the School.cpp or School.h files.

To Be Continued

The idea of classes is somewhat complicated, so don’t be too concerned about understanding it all right now. At this point, see if you can at least use the class using the dot notation contained in the sample main above. Since this is one of the important contributions of C++ we will be visiting these ideas again.

Back to Home Page House3.wmf (25540 bytes)