Search Menu


Classes in C++

Introduction and Summary

Table of Contents


Classes, fundamental elements of C++, are collections variables and functions operate on those variables. Together these variables and functions coherently describe the qualities of an object. The variables in a class definition are called data members, and a class's functions are called member functions. A class's variables and functions can have one of 3 basic accessibility types. Public members are visible directly through an object, whereas private members are completely hidden, only accessible within the class's member functions. Protected members behave like private members, except that subclasses have direct access to them. One exception to member visibility is that of friend functions, which are allowed to operate on an object's private data members even without belonging to the object's class.

Inheritance, the creation of subclasses from superclasses, is another key feature of C++. Subclasses inherit all the data members and member functions of their superclasses. In addition, they can have extra members to create a more specific kind of object. Some complications arising with inheritance can be solved by using composition, that is, by including objects in a class as data members.