Worksheet 3: C++ Classes

This short worksheet introduces you to the creation and use of C++ classes. You need to have done the Introduction to C++ worksheet before attempting this.

For more information on C++ programming, please see the links provided in the Introduction to C++ and OOP in C++ lectures. In particular, you may find the cplusplus.com tutorial and the introductory video by Derek Banas to be useful.

Creating a Class

  1. Create a file called circle.hpp, containing the following:

    #pragma once
    
    #include <cmath>
    
    class Circle
    {
    };
    

    Don’t forget the semi-colon at the end!

    Then finish off this class definition so that it is able to represent circles. You should assume that a circle is represented by the x & y coordinates of its centre and by its radius. Create private double fields for each of these.

    Your class should also include

    • A constructor that create a Circle object, given values for x, y & radius

    • Getter methods for each of the fields

    • A method called perimeter the returns the perimeter (circumference) of the circle

    • A method called area that returns the area of the circle

    Include only prototypes for the methods, not full implementations. Don’t forget to use const!

  2. Now create a file called circle.cpp, containing implementations of all the methods defined in Circle. When implementing perimeter and area, note that you can use M_PI to represent the value of \(\pi\).

    Check that the class compiles before proceeding further, using

    g++ -c circle.cpp
    

Using The Class

  1. Now create a file called testcircle.cpp, containing a small program that tests the Circle class. Your program should create two Circle objects with different positions and radii. It should then print out the perimeter and area of each circle.

    Compile your program with

    g++ testcircle.cpp circle.o -o testcircle
    

    Then run it with ./testcircle.

  2. Write a makefile that will compile your class and the program that uses it.

Improving The Class

  1. Modify the constructor so that it does validation of the radius, rejecting any values that are less than or equal to 0.0.

    The most appropriate way of doing this is to throw one of the standard exceptions supported by C++, namely invalid_argument. Here’s an example of how to throw an exception:

    if (x > 100) {
      throw std::invalid_argument("x is too large");
    }
    

    Note that you will need to have #include <stdexcept> in your .cpp file for this to work. Note also that you can omit the std:: prefix if you have

    using namespace std;
    

    in your .cpp file.

  2. Modify your Circle class so that everything except the constructor is inlined into the class definition in circle.hpp. (See the OOP in C++ lecture if you’re not sure what inlining is.)

  3. Check that your test program still compiles and that its runtime behaviour is unchanged.

  4. Modify the test program so that it attempts to create an invalid circle. Compile and rerun so that you you can see what happens when a C++ exception is thrown.