Next: Inheritance Up: Classes Previous: Classes

Class Definition

Here is a simple class definition for a class with one data member and two member functions:


class myclass {
private:
  int i;
public:
  inline void setI(int val) {i = val;}
  inline int returnI() {return i;}
}

The two function members manipulate the data member i. If two different variables of the class type exist, they each have their own member functions which manipulate their own data members. The following code illustrates this.


myclass *c1,*c2;                // pointers to objects of type myclass

c1 = new myclass;               // create a myclass object, pointed to by c1
c2 = new myclass;               // create a myclass object, pointed to by c1

c1->setI(3);                    // c1->i is set equal to 3
c2->setI(1);                    // c2->i is set equal to 1

Because i is declared in a private portion of the class definition, other programs may not refer to c1i. However, the public functions of myclass have access to the private data and functions of myclass.


walter@
Wed Aug 10 11:53:26 PDT 1994