CSCI 201 Study Aid: Classes
Objects and Classes
- Object-oriented programming (OOP) is an approach to programming
where the data and the operations for manipulating that data come in a
tidy package called an object.
- In C++ the mechanism to create objects is the class.
- A class is a user defined data type. Each class you define
contains the data and functions to manipulate that data.
- Classes support information hiding. A programmer who uses a class
may have no idea of how the class is implemented. The implementor of
a C++ class can completely hide the knowledge of how a class is
implemented. Information hiding is one of the features of classes
that supports code reuse.
A Class Consists of:
- The interface or definition is contained in a
header file (filename.h). The definition consists of:
- Declarations of the data members
- The prototypes for the member functions
- The implementation is written in a source code file
(filename.cpp). The implementation file contains:
- code for the member functions
- code for any auxiliary functions required to overload
standard operators such the extraction operator.
- You could put both the definition and the implementation be in same
file but that would defeat the purpose of information hiding.
The Class Interface or Definition
The parts of the class definition are:
- the class keyword followed by the name of the new class
- the public section lists the prototypes of the member functions
- there are two kinds of public member functions:
- those that modify the state of the class (modify the member
variables), these are called mutator functions
- constant member functions that examine the status of the data
members but are forbidden to change them. The prototypes for these
functions are followed by the keyword const
- the private section contains the data members (also called the
member variables). These variables can't be directly accessed by
programmers who use the class (but the class member functions can
access these variable).
The throttle class---a new data type that manipulates and stores the
status of a throttle. A toy example of a class that might be used in a
flight simulator. There are 4 throttle functions:
- a function to set the throttle to the shutoff position
- a function to shift a throttle's position by a given amount
- a function that returns the fuel flow (percentage of max flow).
- a function to verify that the throttle is currently on.
// Mutator functions
void shut_off( );
void shift(int amount);
// Inspector functions
double flow( ) const;
bool is_on( ) const;
int position, max_position;
The header file and the implementation file
Using a class
As with any other data type, you declare variables of that type.
These variables are called objects or instances.
Calling member functions:
Throttle plane, car;
/ | \ \
/ | \ actual parameter
object | \
name member member
Implementing member functions
- Writing the definition of the function is like writing any other function
with one exception: In the head of the function definition, the class name
must appear before the function name separated by 2 colons.
The 2 colons called the scope operator tell the compiler that it is a
member function of a particular class.
Void Throttle::shut_off( )
position = 0;
double Throttle::flow( ) const
- Each object has its own copy of all member variables. When a member
function implementation refers to a member variable, then the actual
member variable used comes from the object whose member function has
Throttle car, plane;
car.shut_off( ); // sets car's position variable to 0
plane.shut_off ( ); // sets plane's position variable to 0
- Member functions can call (activate) other member functions.
bool Throttle::is_on( ) const
return (flow( ) > 0);
- Constant member functions can not call non-constant member functions.
Also, the const keyword must be used in both the declaration
and the definition.
- Constructors provide an initialization function that is guaranteed to
be called. A constructor is a member function with these special properties:
- If the class has a constructor then it is automatically called when a
variable of that class is declared.
- The name of the constructor must be the same as the name of the class
- A constructor does not have any return value
- If the constructor has parameters then the calling arguments must be
given after the object name
- Example constructor declaration:
// Modification member functions
- Constructor Implementation
Throttle::Throttle(int size )
position = 0;
- If you write a class with no constructor, then the compiler automatically
creates a simple default constructor. It just calls the default constructors
for the member variables. Because the data members in the throttle class are
type int, the default constructor does not initialize these variables. The
default constructor has no parameters.
- Multiple constructors: Multiple constructors can be provided, one for each
different kind of initialization. Each constructor must have a unique signature
(i.e., parameter list) so the compiler can tell them apart.
Throttle ( ); // the default constructor
Throttle (int size); // one parameter constructor
Throttle (int size, int start); // two parameter constructor
- In this case you could use default arguments to get the same effect
Throttle (int size=5, int start=0);
- A default argument is a value that will be used for a parameter when there
is no actual calling argument.
- Default arguments are specified only once in the prototype, not in the
- Defaults do not need to be specified for all parameters, but if only some are
specified then they must be rightmost in the parameter list.
int set_date(int year, int month=1, int day=1);
- When the function is called, arguments with default values may be omitted from
the right end of the calling argument list
set_date(1997,12,5); or set_date(1997, 12); or set_date(1997);
- Here is the start of a class declaration:
void fun1(Foo f);
void fun2(const Foo f);
void fun3(Foo f) const;
Which of the member functions can alter the PRIVATE member
variables of the Foo object that activates the function?
- Only Foo can alter the private member variables of the object
that activates the function.
- Two of the functions can alter the private member variables of the
object that activates the function.
- Three of the functions can alter the private member variables
of the object that activates the function. <==
- All of the functions can alter the private member variables of the
object that activates the function.
- What is the primary purpose of a constructor?
- To initialize the data members of an object as it is declared. <==
- To allow multiple classes to be used in a single program.
- To copy an actual calling argument to a function's parameter.
- To free the dynamic memory used by an object.
- Which kind of functions can access private member variables of a class?
- Friend functions of the class
- Private member functions of the class
- Public member functions of the class
- All of the above can access private member variables <==
- None of the above
solution to 1 through 3
- Write the definition of a class that allows you to set and get
the time of day in hours, minutes and seconds.
- Modify the class definition given above to include a constructor
that has 3 parameters corresponding to the time in hours, minutes and
seconds. The constructor should specify default values for these
- Write the implementation of the constructor describe above.
- Write the definition of a class to represent a rectangle. This
class should allow you to both set and get the dimensions of the
rectangle. It should also provide facilitators for calculating the
area and the perimeter of the rectangle.
- Write the implementation of the class describe above.