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 1

% cat circlass1.cpp
// Circle class example
// using two constructors
#include <iostream>
#include <iomanip>
using namespace std;

// class definition

class Circle
{
   public:
     Circle();                       // default constructor
     Circle(int x, int y, double r); // constructor
     void showdata();               

   private:
     int xcenter;
     int ycenter;
     double radius;

};   // note the semi-colon


// class implementation

// 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()
{
  cout << setiosflags(ios::fixed | ios::showpoint ) << setprecision(2);
 
  cout << "The circle is centered at (" << xcenter << ", "
      <<  ycenter << ")" << endl;
  cout << "Radius = " << radius << endl;

  return;
}

// calling program

int main()
{
  // declare objects a (use default values)
  // and b (provide initial values)

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

  // call member function

  cout << "Circle a: " << endl;
  a.showdata();

  cout << "\nCircle b: " << endl;
  b.showdata();

  return 0;
}
% g++ circlass1.cpp
% a.out
Circle a:
The circle is centered at (0, 0)
Radius = 1.00

Circle b:
The circle is centered at (3, 4)
Radius = 4.00
 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming