C++ Classes

Classes are the fundamental building blocks of object-oriented software. They define a data type. In a computer science sense, a type consists of both a set of states and a set of operations which transition between those states. For example, int is a type, which has both a set of states and a set of operations, like +, -, etc.
In the same sense, a class is also a type, because it provides a set of finite operations (usually public), and a set of data bits (states) that result after the operations are executed.

A class has three important parts:

Data, functions, and methods that are declared public can be accessed outside the class, whereas if they were declared as private, they cannot be accessed outside the class.

Since a class is a type, we can create objects that use the class. We declare these objects the same way we declare variables of one type.

ex. Class_Name class_object;
// "class_object" is the name of the object, declared as an "instance" from the "Class_Name" class

In order to use functions associated with an object, we use this command:

Class Definitions

Each class you create must follow this basic syntax:

class Class_Name
      public functions and variables
      private functions and variables

This basic syntax is usually contained in what is known as a header file, and is designated with the ".h" extension. It is common practice to give the same name to both the header file and the class name. So, in the above example, the name of the file would be known as "Class_Name.h".

After you have declared the class, we need to write the code for each function that is declared in the class. Before we actually write the function, we need this syntax to begin our function:
(return type) Class_Name::function_name(parameters)

The "::" is the scope resolution operator. Using this defines the function to be in the scope of the class.

Each function that you create in your header file is usually developed and written inside a separate ".cpp" file.

Here is a sample program (taken from cprogramming.com) that should give you a good grasp on how classes work. If you read the section on structures, you'll notice that some of the commands are syntactically similar to what we see when classes are used.

class Computer    /* Computer.h file */
//  This means that all of the functions below this (and any variables) are 
//  accessible to the rest of the program
      Computer();    // constructor, has the same name as the class
      ~Computer();   // destructor
      void setspeed(int);
      int readspeed();
      int processorspeed;	// definition for the class member that
				// constitutes the data for the class
};  // end of Computer class

/* Computer.cpp file */ // Contains function definitions for the class members // that constitute the operations for the class. #include "Computer.h" // don't forget this command!! #include <iostream> using namespace std; Computer::Computer() { processorspeed = 0; } Computer::~Computer() // class_name::data_member // Note the scope resolution operator. { } void Computer::setspeed (int p) { processorspeed = p; } int Computer::readspeed() { return processorspeed; }
int main() /* compserv.cpp file */ // main client program { Computer pc; Computer cray; pc.setspeed(100); cout << "You set the speed to " << compute.readspeed() << endl; }

When this program executes, the output will be: You set the speed to 100

This page has been accessed     times.
Last modified: [an error occurred while processing this directive]
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material