next up previous
Next: File I/O Up: Overview of C++ Previous: Unions

Classes

This section introduces the class. The class is the foundation of C++ support of object oriented programming and is at the core of many of its more advanced features. The class provides the mechanism by which objects are created.

A class defines a new data type which can be used to create objects. A class is created using the keyword class. When a class is created, it will define both the data and the code that operates upon that data, that is a class will have both the code and data members.

A class declaration is syntactically similar to a structure. The general form of a class declaration is:

class class_name {
private data and function // cannot be access from outside the class
public:
public data and function // can be accessed from outside the class
} object_list;

The class can contain private as well as public members. By default, all items defined in a class are private. This means they can only be accessed by other members of the same class and not by any other part of the program. To make parts of a class public (i.e. accessible to other parts of the program) they must be declared after the public key word. All variables or functions defined after the public specifier are accessible by all other functions in the program.

To implement a function that is a member of a class, you must tell the compiler which class the function belongs to by qualifying the function name with the class name.

Example
/* This example is used to show the class and implement a queue, The queue is a first in, first out list */ #include<iostream.h>
//This creates the class queue
class queue{
int q[100]; int sloc, rloc;
public:
void init(); // function prototype
void qput(int i);// function prototype
int qget();// function prototype
};
// initialize the queue
void queue::init() /* The :: is called the scope resolution operator. Essentially, it tells the compiler that this version of init() belongs to queue class. Or, :: states that this init() is in the queue scope */ {
rloc=sloc=0;
}
// put an integer into the queue
void queue::qput(int i)
{
if(sloc==100){
cout <<"Queue is full";
return;
}
sloc++;
q[sloc]=i;
}
// get an integer from the queue int queue::qget()
{
if(rloc==sloc){
cout <<"Queue underflow";
return 0;
}
rloc++;
return q[rloc];
}
main() {
queue a, b; /* create two queue objects. Once you have defined a class, you can create an object of that type using the class name. A class name becomes a new data specifier.*/

a.init();
/* init() is invoked relative to object a. This is the way to call a member function from outside the class. It takes the object name dot the function name. */

b.init();
a.qput(10);
b.qput(19);
a.qput(20);
b.qput(1);

cout << "Contents of queue a:";
cout << a.qget() << " ";
cout << a.qget() << "$\backslash$n";

cout << "Contents of queue b:";
cout << b.qget() << " ";
cout << b.qget() << "$\backslash$n";
return 0;
}


next up previous
Next: File I/O Up: Overview of C++ Previous: Unions
Yousef Haik
2/23/1998