Classes
Classes are an extension of structures to allow a type to contain member functions as well as data components. The member functions are typically designed to perform useful operations on the data members of variables of that class.
public
member functions and data members are available to any
part of a program using the class, and can be used to provide the
public interface to the class.
private
member functions and data members are only visible to
other member functions of the class, and can be used to hide the
details of the implementation.
class FlexArray { private: int len; float data[100]; public: void clear(); void add(float item); float operator--(); // remove last item and return it. } void FlexArray::clear() { len = 0; } void FlexArray::add(float item) { if (len < 100) { data[len] = item; len++; } else { // handle array overflow error } } float FlexArray::operator--() { if (len > 0) { len--; return data[len]; } else { // handle array underflow error } } FlexArray fa; fa.clear; fa.add(x); float y = fa--;
The FlexArray::
prefix denotes that the function is a member of
the FlexArray
class.