Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Circle Class - Version 5

version 2:  ostream& in showdata(), accessor and mutator functions, and separate compilation.
version 3:  static data member, setCircle, area, circumference and equal functions
version 4: const, friend functions
version 5: overload operators ==, +, <<

Definition Part

% cat circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
// Circle class definition - file circle.h

class Circle
{
   friend double distance( Circle& c ); // friend function

   friend ostream& operator<<(ostream& out, Circle& cc );

   public:
     Circle();               // default constructor
     Circle(int x, int y, double r); // constructor
     void showdata(ostream& out) const;
     double get_radius()
const;       // accessor
     void set_radius( double r );    // mutator
 
     void setCircle(int x, int y, double r);
     double area()
 const;
     double circumference()
 const;
     bool equal( Circle& c2 ) const
;

     
// operator functions
     bool operator==(Circle& c2) const; 
     Circle operator+(Circle& circle2 ) const;


   private:
     int xcenter;
     int ycenter;
     double radius;
     static double PI; // static data member
};
#endif // CIRCLE_H


Implementation Part

% cat circle.cpp
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "circle.h" // header file

double Circle::PI = 3.14159; // static data member

// default constructor
Circle::Circle(): xcenter(0), ycenter(0), radius(1.0)          
{
  // empty body when initialization
  // section is used
}

// constructer with parameters for initialization
Circle::Circle(int x, int y, double r): xcenter(x),
               ycenter(y),radius(r)
{
  // empty body when initialization
  // section is used
}

void Circle::showdata( ostream& out ) const

{
  out << setiosflags(ios::fixed | ios::showpoint ) << setprecision(2);

  out << "The circle is centered at (" << xcenter << ", "
      <<  ycenter << ")" << endl;

  out << "Radius = " << radius << endl;
   
  return;
}

// accessor function
double Circle::get_radius() 
const
{
   return radius;
}

// mutator function
void Circle::set_radius(double r)
{
  if( r < 0.0 )
  {
    cout << "The radius cannot be less than zero." << endl;
    exit(1);
  }

  else

    radius = r;
}

// setCircle() mutator function
void Circle::setCircle(int x, int y, double r)
{
   xcenter = x;
   ycenter = y;
   radius  = r;
}

// area
double Circle::area()const

{
   return PI*radius*radius;
}

// circumference
double Circle::circumference()const

{
    return 2.0 * PI * radius;
}

// operator functions

// test equality of Circle objects
bool Circle::operator==( Circle& c2 ) const
{
   bool eq;
       
   eq = (radius==c2.radius) && (xcenter==c2.xcenter)       
        && (ycenter==c2.ycenter);
  
   return eq;
}

// add two Circle objects
Circle Circle::operator+(Circle& circle2 ) const
{
   Circle temp; // a tenporary Circle

   temp.xcenter = xcenter + circle2.xcenter;
   temp.ycenter = ycenter + circle2.ycenter;
   temp.radius = sqrt(radius*radius + circle2.radius*circle2.radius);

   return temp;

}


// non-member friend functions

// distance to origin
double distance( Circle& c)
{
   double dist;
   dist = c.xcenter*c.xcenter + c.ycenter*c.ycenter;

   return sqrt( dist );
}


// overload <<

ostream& operator<<(ostream& out, Circle& cc)
{
   out << "A message from a friend (function):" << endl;
  
  
out << setiosflags(ios::fixed | ios::showpoint ) << setprecision(2);

  out << "The circle is centered at (" << cc.xcenter << ", "
      <<  cc.ycenter << ")" << endl;

  out << "Radius = " << cc.radius << endl;

  
   return out;
}


Driver Program


% cat circ5.cpp
// driver program for Circle class example

#include<iostream>
using namespace std;
#include<fstream>

#include "circle.h" // header file

int main()
{
   int new_x, new_y;
   double new_r;

   // declare Circle objects a and b

   Circle a(3, 4, 4.0), b;

   // display using << operator

   cout << "Circle a: " << endl;
   cout << a << endl;

   // set new values for b

   cout << "Enter new values for xcenter, ycenter, and radius: ";
   cin >> new_x >> new_y >> new_r;

   b.setCircle(new_x, new_y, new_r);

   cout << "New values for Circle b: " << endl;
   cout << b << endl;

   // check equality with == operator

   if ( a == b )
     cout << "The circle objects are equal" << endl;
   else
     cout << "The circle objects are NOT equal" << endl;

   // use operator + function

   Circle c;

   c = a + b;

   cout << "\nSum of circles:\n\n;

   cout << c << endl;

   cout << "Area of a = " <<   a.area() << endl;
   cout << "Area of b = " <<   b.area() << endl;
   cout << "Area of c = " <<   c.area() << endl;

   return 0;
}


Compile and Execute

% g++ circ5.cpp circle.cpp
% a.out
Circle a:
A message from a friend (function):
The circle is centered at (3, 4)
Radius = 4.00

Enter new values for xcenter, ycenter, and radius: 2 2 2.0
New values for Circle b:
A message from a friend (function):
The circle is centered at (2, 2)
Radius = 2.00

The circle objects are NOT equal

Sum of circles:

A message from a friend (function):
The circle is centered at (5, 6)
Radius = 4.47

Area of a = 50.27
Area of b = 12.57
Area of c = 62.83


 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming