Object-oriented programming (OOP) introduces some new terminology that you should be familiar with and be able to use. Please review the following terms.
A class is a description or declaration of a structured data type. Structured data is a container with multiple fields or variables embedded inside and typically contains operations or functions. In the cookie and cookie-cutter metaphor, a class is a cookie-cutter - it isn't the end product (the cookie) but describes the cookie's size, shape, and decorations. Continuing the metaphor, once we roll out the dough, we can stamp out as many cookies as we wish. In the same way that each cookie has the same size and shape, each object instantiated from the class has the same fields or variables and the same operations or functions. The class name becomes a new type specifier in any program that includes the class. For example, if a program specifies a class named Person, then it is possible to define a variable named p as follows:
Objects are structured data created from a class. In the same way that a zip file is a file that contains other, smaller files, an object is a unit of data that contains other, smaller units of data. Objects are like the cookies in the cookie-and-cookie-cutter metaphor - it's the end product, the thing that can be eaten or used. If a program creates two objects from the same class, they must have the same variables and functions (as specified by the class). However, the values stored in one object's variables are distinct from those stored in another. In the example
The first of three essential object-oriented characteristics, encapsulation, is the combining or packaging together of both data and operations that manipulate or use that data. In a sense, encapsulation is a synonym for an object as objects implement encapsulation.
The object-oriented term for a function declared in class scope. C++ implements operations as member functions.
Instantiate
The process of creating an object: "to instantiate an object," "to instantiate a class," or "to instantiate an object from a class" is to create an object.
Instance
A synonym for an object: an instance of a class.
Feature
"Feature is a generic word for either an attribute or operation" (Rumbaugh, Blaha, Premerlani, Eddy, & Lorensen, 1991, p. 26).
Member
A collective term for a function, variable, or constant declared in a C++ class specification.
Member Variable (aka member field)
Programmers declare member variables in classes, and programs allocate their memory in objects. Two terminologies are in use: The original C++ nomenclature divides functions into static and non-static. A newer, language-independent, but not universal, nomenclature calls all variables declared in class scope member variables and divides them into instance and class variables.
Member functions are non-static functions declared in a C++ class. While running, they are bound to an object through the this pointer and can access its features without additional notation. They can also access the private features of parameter objects instantiated from the same class (see the fraction class's arithmetic functions for an example).
Data Member
Synonym for a member variable.
Class Variable
A variable that belongs to the class as a whole rather than a specific object. C++ implements class variables as static member variables.
Class Function
A function that belongs to the class as a whole. C++ implements class functions as static functions. These functions do not have a "this" pointer and cannot access non-static member variables.
A member function is called automatically when a new object is created or instantiated. Constructors construct or initialize new objects that they are ready to use. Although constructors are functions, they do not have a return type.
It is common for constructors to initialize (i.e., store the first or initial value in) an object's member variables, and initializer lists are a compact but optional and efficient way of doing this. Constructors are the only functions that can have an initializer list, and if present, the initializations occur before the statements in the constructor's body execute. For example, suppose that the fraction class has two member variables, numerator and denominator. A possible constructor for the class is
fraction(int n, in d) : numerator(n), denominator(d) {}
The example highlights the initializer list in light blue. The function body in this example is empty, but the body may contain statements when needed.
A keyword naming a local pointer variable that the compiler automatically defines in all non-static member functions. The this pointer stores the address of the object that calls and is bound to a member function, and function statements use it when they need to refer explicitly to that object. For example, if x is an instance of a class that defines a function named operation, then the statement:
x.operation();
calls operation and passes the address of x as an implicit or "invisible" argument that is stored in this.
Getter functions are special-purpose functions that allow client programs (programs that use a class) to access the value stored in a member variable. Programmers typically follow one of two conventions when naming getter functions. For example, suppose that a class named Person has an attribute or member variable: int height. Then, the following getter functions illustrate the common implementations for functions that "get" and return the height:
int get_height()
{
return height;
}
int getHeight()
{
return height;
}
Typically, a getter function returns a copy of the member data, which preserves encapsulation.
Setter functions are special-purpose functions that allow client programs (programs that use a class) to change the value stored in a member variable. For example, suppose that a class named Person has an attribute or member variable: int height. Then the following "setter" functions illustrated the common implementations for a function that "sets" a new value for height:
void set_height(int h)
{
height = h;
}
void setHeight(int h)
{
height = h;
}
Setter functions may include a test to validate that the argument value, h in this example, is appropriate before updating an object.
Rumbaugh, J., Blaha, M., Premerlani, W., Eddy, F., & Lorensen, W. (1991).
Object-Oriented Modeling and Design. Englewood Cliffs, NJ: Prentice Hall.