9.16. Terminology Review

Object-oriented programming (OOP) introduces some new terminology that you should be familiar with and be able to use. Please review the following terms.

Class
A class is a description or declaration of a structured data type. Structured data is a container that has multiple fields or variables embedded inside and may also contain 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 it describes the cookie's size, shape, and decoration. 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 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:
Person p;
Object
Objects are a kind of 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 the values stored in the other object. In the example
Person p;
p is a variable that is also an object.
Encapsulation
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.
Attribute
The object-oriented term for a variable or field declared in class-scope. Attributes are implemented as member variables.
Operation
The object-oriented term for a function declared in class scope. Operations are implemented 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 nomenclatures 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 Function
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 the object's 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.
Constructor
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.
Initializer List
An initializer list is an optional part of a constructor (i.e., only constructors may have an initializer list). 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 and efficient way of doing this. 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 initializer list is highlighted in light blue. While the function body is empty in this example, the body may contain statements when needed.
this
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.
public
A keyword allowing access to member variables and functions throughout the program.
private
A keyword restricting access to member variables and functions to member functions defined in the same class.
protected
Keyword. Restricts access to member variables and functions to members of the defining class and its subclasses.
Getter Function
Getter functions are special-purpose functions that allow client programs (programs that use a class) to access the value stored in a member variable. Two conventions for naming getter functions are in common use. 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 a functions that "gets" and returns 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 Function
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.