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:

The following example programs all deal with Cartesian (x-y) points. If you are not familiar with Cartesian points, you can find a quick overview here:

http://www.mathsisfun.com/data/cartesian-coordinates.html

It takes two numbers (called the x and y coordinates) to define a point. We will use floating-point values for the coordinates. Some of the operations that will be performed on points are:

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.

point.cpp

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:

  • assign new x and y values to a point
  • print a point
  • calculate the magnitude of a point

point_w_funcs.cpp

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:

  • We can now define a point in a single declaration:
       Point p;
    instead of having to declare the x and y coordinates as separate variables.
  • The functions that operate on a point have been packaged inside the class along with the data definitions.
  • Notice the differences between the "regular" functions in the previous example program and the functions as class members. Also notice the difference between how regular functions are called and how class member functions are called.

point_w_class.cpp

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:
   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 );

Also, member functions are provided that allow the application programmer to retrieve a copy of the x and y values stored in a Point.

point_w_class_enhanced.cpp

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.

pospoint_class_w_valid.cpp

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.

point_w_struct.cpp

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_w_struct_func.cpp

 


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