Programming and Graphics

(Kiana) #1

6.8 Algebra on real numbers 167


Problems


6.7.1.Add to the circle class a member function that computes and prints the
area of a circle, and to the square class a member function that computes
and prints the area of a square.


6.7.2.(a) Add to the circle class a member function that assesses whether two
members overlap. (b) Repeat for the square class.


6.8 Algebraonrealnumbers.......................


As a further example, we introduce the class of points along thexaxis described
by thexvalue and their color. Ifxis positive, the color is black, ifxis negative,
the color is red, and ifxis zero, the color is white.


We will endow the algebra class with several member functions that per-
form the following tasks:



  • Initialize a new point using the default constructor.

  • Initialize a new point using the parametered constructor.

  • Determine the color from the value ofx.

  • Get the value ofxand the color of a specified point.

  • Print the value ofxand the color of a specified point.

  • Shift a point along thexaxis.


The algebra class definition is:


/*----------------------
Algebra on real numbers
-----------------------*/

#include <iostream>

using namespace std;

//--- CLASS DEFINITION

class algebra
{
public:
algebra(); // default constructor
algebra(double); // parametered constructor
double get(string&) const;
Free download pdf