Intro to C++ Part 12: Classes

Submitted on: 1/1/2015 7:49:00 AM
By: Alexander of (from psc cd)  
Level: Intermediate
User Rating: By 13 Users
Compatibility: C++ (general)
Views: 1180
     C++ is a bunch of small additions to C, and one major addition. This one addition is the object oriented approach. As its name suggests, this deals with objects. This tutorial introduces you to creating objects, or classes.


C++ is a bunch of small additions to C, and one major addition. This one addition is the object oriented approach. As its name suggests, this deals with objects. Of course, these are not real-life objects. Instead, this objects are the essential definitions of real world objects, or people. Structures are one step away from these objects, they do not possess one element of them: functions. The definition of these objects are called classes. The easiest way to think about a class is to imagine a structure that has functions.

     What is this mysterious structure? Well, it is not only a collection of variables under one heading, but it is a collection of functions under that same heading. If the structure is a house, then the functions will be the doors. They usually will be the only way to modify the variables in this structure, and they are usually the only to to access the variables in this structure.

     From now on, we shall call these structures with functions classes(I guess Marx would not like C++). The syntax for these classes is simple. First, you put the keyword 'class' then the name of the class. Our example will use the name computer. Then you put the different variables you want the class to hold. In our example, there will be only one, processor speed.  However, before putting down the different variable, it is necessary to put the degree of restriction on the variable. There are three levels of restriction. The first is public, the second protected, and the third private. For now, all you need to know is that the public specifier allows any part of the program, including what is not part of the class, access the variables specified as public. The private specifier allows only the functions of the class that owns (not a technical term) the variable to access that variable.

#include <iostream.h>

class computer //Standard way of defining the class


private: //This means that all the variables under this, until a new type of restriction is

//placed, will only be accessible to functions that are part of this class.

//NOTE: That is a colon, NOT a semicolon...

int processorspeed;

public: //This means that all of the functions below this(and variables, if there were any)

//are accessible to the rest of the program.

//NOTE: That is a colon, NOT a semicolon...

void setspeed(int p); //These two functions will be defined outside the class

int readspeed();

}; //Don't forget the trailing semi-colon

void computer::setspeed(int p) //To define a function outside put the name of the function

//after the return type and then two colons, and then the name

//of the function.


processorspeed = p;


int computer::readspeed() //The two colons simply tell the compiler that the function is part

//of the class


return processorspeed;


void main()


computer compute; //To create an 'instance' of the function, simply treat it like you would
//a structure. (An instance is simply when you create an actual object
//from the class, as opposed to having the definition of the class)

compute.setspeed(100); //To call functions in the class, you put the name of the instance,
//and then the function name.

cout<<compute.readspeed(); //See above note.


     As you can see, this is a rather simple concept. However, it is very powerful. It makes it easy to prevent variables that are contained(or owned) by the class being overwritten accidentally. It also allows a totally different way of viewing programming. However, I want to end this tutorial as an introduction. I am going to be writing more tutorials on classes, which will go into more details on classes.

Other 10 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.