COSC 1337 - Programming Fundamentals II
Bob Comer, Professor of Computer Studies
Example C++ Programs
Chapter 7 - Classes and Objects
In C++ a class allows a programmer to create a new data type.
A data type consists of two parts:
One of the main reasons to use a class is that it makes it easy to re-use code. Once you have created a Point class, it is easy to use that same class in another program that needs to work with Points. In fact, it is often that case that one programmer creates a class, and then other programmers use that class to write applications (programs).
In the following example programs, pay particular attention to how much code is required in the main function to create and process a point.
Description Program This first example program creates a point and performs operations on it
without using a class or even separate functions.
All the work is done in the main function.
This program does the same thing as the previous example program.
But the code to operate on a point has been organized into functions.
The program includes a separate function to: This program does the same thing as the previous two programs.
It uses a simple class to define the Point data type.
Notice the following differences from the previous example: This program enhances the class from the previous example.
A class can contain one or more constructor functions
that specify how a class object is initialized when it is created.
The Point class now contains two constructor functions.
One specifies how to initialize a Point declared like this: Also, member functions are provided
that allow the application programmer to retrieve a copy of the x and y values stored in a Point.
The programmer who creates a class needs to ensure that any programmer using the class
never stores an invalid value in a class object.
In the Point class in the previous examples,
any value is valid for the x and y coordinates of a Point,
so no validation is required.
In this example, we want to create a class called PosPoint.
It is similar to the Point class.
However, the x and y values stored in a PosPoint cannot be negative.
All class member functions that allow the application programmer to store values in a PosPoint
have been modified to prevent negative values from being stored in a PosPoint.
C++ was created by extending an older programming language called C.
C has a feature called a structure (struct) that has some similarities to a class,
but is much more limited.
A struct in the C language (sometimes called a C struct)
is basically the same as a class with no function members
and whose data members are all public.
The declaration of a C struct creates a data type.
The programmer can then use that data type to create variables, parameters, etc.
This simple program uses a C struct to represent the data for a point.
The code in the main function then operates on the point.
One big limitation of a C struct is that you cannot include member functions to define operations on an object.
*
But you can write regular functions to define operations for the structure.
This program uses a C struct to represent the data for a point,
and uses regular functions to define operations on a point,
*In C++, a structure can have member functions and private data members.
It basically duplicates the capabilities of a class.
As a programmer you may very well encounter older code with C structs.
You will rarely encounter C++ structs that contain function members.
In this class, we will only cover C structs.
Point p;
instead of having to declare the x and y coordinates as separate variables.
Point p1;
The second constructor function allows the application programmer
to specify the initial values for the x and y coordinates of a Point when it is created:
Point p2( 3.0, 4.0 );
Return to Programming Fundamentals II Home Page
Copyright: © 2013 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: October 13, 2013