Classes

The class is C++’s basic unit of encapsulation. A class is defined using the class keyword. Classes are not part of the C language. A class is essentially a collection of variables and functions that manipulate those variables. The variables and functions that form a class are called members. The general form of class is shown here:

class class-name : inheritance-list {
 // private members by default
protected:
 // private members that can be inherited
public:
 // public members
} object-list;

Here, class-name is the name of the class type. Once the class declaration has been compiled, the class-name becomes a new type name that can be used to declare objects of the class. The object-list is a comma-separated list of objects of type class-name. This list is optional. Class objects can be declared later in your program by simply using the class name. The inheritance-list is also optional. When present, it specifies the base class or classes that the new class inherits. (See the following section entitled “Inheritance.”)

A class can include a constructor function and a destructor function. (Either or both are optional.) A constructor is called when an object of the class is first created. The destructor is called when an object is destroyed. A constructor has the same name as the class. A destructor has the same name as the class, but is preceded by a ~ (tilde). Neither constructors nor destructors have return types. In a class hierarchy, constructors are executed in order of derivation and destructors are executed in reverse order.

By default, all elements of a class are private to that class and can be accessed only by other members of that class. To allow an element of the class to be accessed by functions that are not members of the class, you must declare them after the keyword public. For example:

class myclass {   int a, b; // private to myclass public:   // class members accessible by nonmembers   void setab(int i, int j) { a = i; b = j; }   void showab() { cout << a << ' ' << b << endl; } } ; myclass ob1, ob2;

This declaration creates a class type, called myclass, that contains two private variables, a and b. It also contains two public functions called setab( ) and showab( ). The fragment also declares two objects of type myclass called ob1 and ob2.

To allow a member of a class to be inherited, but to otherwise be private, specify it as protected. A protected member is available to derived classes, but is unavailable outside its class hierarchy.

When operating on an object of a class, use the dot (.) operator to reference individual members. The arrow operator (>) is used when accessing an object through a pointer. For example, the following accesses the putinfo( ) function of ob using the dot operator and the show( ) function using the arrow operator:

struct cl_type {   int x;   float f; public:   void putinfo(int a, float t) { x = a; f = t; }   void show() { cout << a << ' ' << f << endl; } } ; cl_type ob, *p; // ... ob.putinfo(10, 0.23); p = &ob; // put ob's address in p p->show(); // displays ob's data 

It is possible to create generic classes by using the template keyword. (See template in the keyword summary in Chapter 5.)




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

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