Namespaces
Variants
Views
Actions

Classes

From cppreference.com


What is object-oriented programming (OOP)? Where did OOP come from? How does the C++ class relate to OOP?

A mathematical object is a set and the operations that we can perform on that set. Familiar objects include natural numbers, integers, rational numbers, real numbers, and complex numbers. Other objects are vectors, matrices, and polynomials. Some associated operations are addition, difference, product, and quotient. Obviously, the addition of two natural numbers is very different from the addition of two polynomials. Further, from plane geometry come points, lines, angles, polygons, and circles. An associated operation is congruence - same shape and size. Calculus is the study of change starting with the set of real-valued functions. Closely related operations are limits, differentiation, and integration.

Problem-solving skills involve in part choosing an appropriate object and applying the associated operations to the set of that object. For example, natural numbers are a tool for counting. Real numbers model measurement. Complex numbers are a tool in electrical engineering and several other fields. Since our problem-solving skills have an object orientation, perhaps our computational skills should have the same orientation.

If our programming skills are to have an object orientation, then we would like to have available a language feature that eases the coding of mathematical objects. This feature should replace code abstraction (structured programming, functions) with data abstraction (encapsulation, polymorphism, inheritance) as the primary orientation. It should implement the two parts of an object: a set and the operations performed on that set. It must offer a way to maintain the integrity of the data and hide details of operation from the user. If the object has a numerical nature, then arithmetic operations should be similar to the operations of built-in types like int and double. In short, the desired language feature should allow a programmer to extend the set of basic data types with new, user-defined data types that provide a type-safe interface without significant costs in run time or memory. For example, if a language lacks complex numbers, then the feature should provide a productive way to construct a user-defined type that behaves as if it were one of the built-in data types.

In C++, that desired feature is the class.