What Is a Class?

As you have already seen, object-oriented programming represents real-world objects in programming structures. It does this by using classes. A class can be defined as a template for creating objects. A more practical description might be that it’s a structure with methods. If you will recall structures from Chapter 7, you can think of a class as just a structure that also has methods. Once you have created a class, you can then declare an instance of it, just as you would create any other type of variable. Consider the following example.

Example 10.1

Step 1: Please enter the following code into your favorite text editor.

#include <iostream> using namespace std;   class myfirstclass { public:  int number;  void greeting();  }; void myfirstclass::greeting() { for(int i = 0; i<= number;i++)  {    cout<< "Hello World \n";  }    } int main ()  {      myfirstclass myfirstobject;      myfirstobject.number =3;  myfirstobject.greeting(); return 0;    }

Step 2: Compile the code.

Step 3: Run the code. You should see something similar to Figure 10.1.

click to expand
Figure 10.1: My first class.

There several things in this code that should be interesting you. Let’s examine the code one piece at a time. To begin with you should note the overall framework of a class is not unlike a structure. You have the keyword class identifying that this is a class, then you have the name that we wish to give it. You then have brackets showing the boundaries of the class.

class myclass { };

So far there is really no difference between a structure and a class. As was pointed out earlier, a class is really just a structure with methods. The first difference is the word public: Anything, be it a function or a variable, listed before the word public will be considered private. (The difference between public and private will be discussed momentarily.) Next, we notice a few variables are declared. These variable declarations are not different from any other such declarations you have seen so far in this book. By default, everything in a class is private unless you specifically declare it as public or protected. Everything in a structure is public unless you specifically declare it as private or protected.

The real oddity is how functions are declared and associated with a class. You first must designate the functions return type, just as you would with any function. But then, before you give the name of the function, you must give the class name followed by two colons :: then the name of the function. The two colons are referred to as a scope operator. You may recall them from our brief discussion on namespaces. A scope operator simply designates that the item (be it a function, variable, etc.) immediately following it is associated with (or part of) the object preceding it.

When the code reaches the main function we can now declare variables of the type myfirstclass. We have essentially created a new data type; however, this data type not only stores data but also has functions. Then simply using the name of the variable followed by a. and the function or variable we wish to access, we can either set the variables values, or call the function.

You should notice that what we have now created is an object much like objects in the real world. This object has variables; in the world of object-oriented programming (OOP) we call these properties, and it has functions, which we refer to as methods. Most of the objects around you have properties and methods. As previously mentioned, a car has properties such as number of doors, color, model, and the like. It also has methods such as accelerate, brake, and so on. In object orientation we take the variables or data we wish to work on and place them in a class with the methods that will work with that data. This is the concept of encapsulation introduced earlier in this book.

The difference between private items and public items is simple. Public items can be called from outside the class, as you saw in our previous example. Private items cannot be called from outside the class. They are called from other methods of the class. Your car has a number of private methods in its engine. These are not readily available to the outside. When you initiate the public accelerate method, several private methods are then called to increase fuel flow and other items necessary to accelerate. Public or private determines how the particular member function or variable can be accessed from outside the class, they are therefore called access modifiers. There are actually different possibilities for the access modifier of a method or property of a class; these are shown in Table 10.1.

Table 10.1: Access Modifiers

Access Modifier



This means that the member variable or function cannot be accessed, or even viewed from outside the class.


Public members can be accessed from outside the class.


Protected members cannot be accessed from outside the class, but they are inherited by subclasses.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net