C++ Concept Map

Classes

The declaration of a class adds a new type to C++ type system. A class is an encapsulation of variable and function declarations, called data members and function members respectively. Variables can have any type, , but they must have unique names within the scope of the class. Functions can have the same name, even within the scope, but must have different signatures (the functions can be overloaded). All members are subject to access control; the default is private, but any member can be made public by using an access control keyword within the body of the class declaration. Function members may be simply declared as prototypes, or defined within the class as inline functions. Function members may be defined externally. Special function members are constructors, and the destructor. Here is an example class declaration showing all these points:

class Eg {
  int i1;  // a private data member of base type int
  C *c;    // a private pointer to an object of type C
public:
    Eg() { i1 = 0; c = 0; }  // the (public) default
                             // constructor,defined inline
    Eg(int ii) { i1 = i; }   // an overloaded constructor
    ~Eg();                   // the prototype for the 
                             // destructor
 void f1(int);               // a prototype for a public 
                             // function member
private:
  void f1(int, int);         // a prototype for a
                             // (private)overloaded function
  int f2() { cout << "f2" << endl; }  // an inline private
                                      // function
public:
  int i2;  // a public data member
  float f3(int, float);  // a prototype for a public function
};  


Please mail any corrections and/or suggestions to Roger Hartley at
rth@cs.nmsu.edu

Copyright © 2003 Roger Hartley