rect000
Home ] Up ]

 

// rect000.cpp
// your name
#include <iostream.h>
/////////////////////////////////////////////////////////////////////
// Constant definitions
// length and width should be in [MIN_DIM, MAX_DIM]
/////////////////////////////////////////////////////////////////////
const int MIN_DIM = 1;
const int MAX_DIM = 19;
/////////////////////////////////////////////////////////////////////
// CRectangle struct definition
/////////////////////////////////////////////////////////////////////
struct CRectangle
  {
  int length;
  int width;
  };
void init(CRectangle &rec);
void init(CRectangle &rec, int len, int wid);  
int perimeter(const CRectangle &rec);
int area(CRectangle rec);
void setDimension(CRectangle &rec, int len, int wid); 
void setLength(CRectangle &rec, int len);
void setWidth(CRectangle &rec, int wid);
int getLength(const CRectangle &rec);
int getWidth(const CRectangle &rec);
void getDimension(const CRectangle &rec, int &len, int &wid); 
void display(const CRectangle &rec);
/////////////////////////////////////////////////////////////////////
// perimeter function member
/////////////////////////////////////////////////////////////////////
int perimeter(const CRectangle &rec)
  {
  return (2*(rec.length+rec.width));
  }
/////////////////////////////////////////////////////////////////////
// Constructor for CRectangle
// initializes the lenght and width with len and wid
// makes sure that the values are between MIN_DIM and MAX_DIM
// sets them to 1 if the the supplied values are out of range
/////////////////////////////////////////////////////////////////////
void init(CRectangle &rec, int len, int wid)
  {
  if ((len >= MIN_DIM) && (len <= MAX_DIM))
      rec.length = len;
    else
      rec.length = 1;
  if ((wid >= MIN_DIM) && (wid <= MAX_DIM))
      rec.width = wid;
    else
      rec.width = 1;
  }
    
/////////////////////////////////////////////////////////////////////
// Default constructor
// Sets lenght and width to 1
/////////////////////////////////////////////////////////////////////
void init(CRectangle &rec)
  {
  rec.length = rec.width = 1;
  }
/////////////////////////////////////////////////////////////////////
// Display member functio
/////////////////////////////////////////////////////////////////////
void display(const CRectangle &rec)
  {
  cout << "Rectangle (" << rec.length << ", " 
       << rec.width << ')' << endl;
  }
/////////////////////////////////////////////////////////////////////
// Test function for default constructor
/////////////////////////////////////////////////////////////////////
void testConstructor1(void)
  {
  cout << "testConstructor1(void)\n";
  cout << "----------------------\n";
  
  CRectangle r1;
  display(r1);
  init(r1);
  display(r1);
  cout << "Length =" << r1.length << endl;
  CRectangle *p1;
  p1 = &r1;
  cout << "Length =" << (*p1).length << endl;
  cout << "Length =" << p1->length << endl;

  CRectangle myRectangles[5];
  for (int i=0; i<5; i++)
    {
    init(myRectangles[i]);
    display(myRectangles[i]);
    }
  }
/////////////////////////////////////////////////////////////////////
// Test function for constructor with given values for length & width
/////////////////////////////////////////////////////////////////////
void testConstructor2(void)
  {
  cout << "testConstructor2(void)\n";
  cout << "----------------------\n";
  
  CRectangle r1;
  init(r1,4,5);
  display(r1);
  CRectangle r2;
  init(r2,14,51);
  display(r2);
  }
/////////////////////////////////////////////////////////////////////
// Test function for perimeter
/////////////////////////////////////////////////////////////////////
void testPerimeter(void)
  {
  cout << "testPerimeter(void)\n";
  cout << "-------------------\n";
  
  CRectangle r1;
  init(r1,4,5);
  display(r1);
  cout << "Perimeter: " << perimeter(r1) << endl;
  CRectangle r2;
  init(r2,61,51);
  display(r2);
  cout << "Perimeter: " << perimeter(r2) << endl;
  }
/////////////////////////////////////////////////////////////////////
// main function
/////////////////////////////////////////////////////////////////////
void main(void)
  {
  //testConstructor1();
  //testConstructor2();
  testPerimeter();
  }