COSC 2436 - Programming Fundamentals III Data Structures
Bob Comer, Professor of Computer Studies


Example Programs
Chapters 3 and 4 - Abstract Data Types

In C++ a class or struct allows a programmer to create a new data type. A data type consists of two parts:

The following example programs all implement an ADT for 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:

 

Description

Program

This program implements a Point ADT using a struct. It covers material through section 3.6 of the textbook.

client program file      Point_client.cpp

struct header file       Point.h

struct implementation file    Point.cpp

This program implements a Point ADT using a class. It covers the material through section 4.3 of the textbook and only provides a limited set of operations - assign a new value to a Point, display a Point, and get the magnitude of a Point.

client program file      Point_client.cpp

Class header file       Point.h

Class implementation file    Point.cpp

This version of the Point class covers material from Chapter 4 of the textbook. Changes from previous version:

  • add constructor functions that give the client programmer 2 ways to initialize a new Point
  • add accessor functions to return the x and y values of a Point
  • enhance the display() function to display on any output stream
  • a non-member function to return the sum of two Points

client program file      Point_client.cpp

class header file       Point.h

class implementation file    Point.cpp

This version of the Point class covers material from Chapter 4 of the textbook. Changes from previous version:

  • Overload the insertion operator << to display a Point
  • Overload the + operator to sum two Points (using a non-member function)

client program file      Point_client.cpp

class header file       Point.h

class implementation file    Point.cpp

This version of the Point class covers material from Chapter 4 of the textbook. Changes from previous version:

  • Overload the + operator using a member function

client program file      Point_client.cpp

class header file       Point.h

class implementation file    Point.cpp

It is the class programmer's responsibility to ensure that the class member functions never give inconsistent results. Part of this is ensuring that the class member functions never try to process invalid information from the client programmer. Here we implement an ADT for x-y points where x and y are non-negative. That is, only points in the upper-right quadrant are allowed.

client program file      PosPoint_client.cpp

class header file       PosPoint.h

class implementation file    PosPoint.cpp

 


Return to Programming Fundamentals II Home Page

Copyright: © 2015 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: February 8, 2015