In Class - class
Excercise 6-12
  1. What is the class called.?
    Rectangle
  2. What data members are in the class?
    length and width both doubles
  3. What access modifier should the data member have?
    private
  4. What member functions need to be defined?
    area
    perimeter
    setLength
    setWidth
    getWidth
    getLength
  5. What does the prototype look like for the area member function?
    double area(void);
  6. What does a get for length prototype look like?
    double getLength(void);
  7. What does a set for length prototype look like?
    void setLength(double l);
  8. Write the function for area.
       double Rectangle::area(void) 
       {
    	return length * width;
       }
    
  9. How would the area function be invoked?
       
       Rectangle a;
    
       cout << "a : area=" << a.area() << endl; 
    

  10. Write the member function for setLength to enforce the data constraints.
    void Rectangle::setLength(double L)
    {
    	if ( L > 0 && L < 20.0)
    		length=L;
    	else
    		length=1.0;
    }
    
  11. Do we need multiple (overloaded) functions for the contruction?
    It should be able to handle the following instances.
    Rectangle a;
    Rectangle b(20.0); // Set the length
    Rectangle c(10.0,10.0) // set the length and width
    No we can handle this with deafult arguments:
  12. Write the constructor
    Rectangle(double=1.0, double=1.0);  // Constructor
    
    Rectangle::Rectangle(double l, double w)
    {
    	setWidth(w);
    	setLength(l);
    }
    
  13. What does the header file look like for this project??
    //  
    #ifndef crect
    #define crect
    
    class Rectangle {
    
    public:
    	Rectangle(double=1.0, double=1.0);  // Constructor
    	double perimeter(void);
    	double area(void);
    	void   setWidth(double w);
    	void   setLength(double l);
    	double getWidth(void);
    	double getLength(void);
    
    private:
    	double length;
    	double width;
    };
    
    #endif
    
  14. Write the member functions
    //
    //  Member Functions for Rectangle
    //
    #include 
    #include "crect.h"
    
    using namespace std;
    //   Constructor
    Rectangle::Rectangle(double L, double W)
    {
       setWidth(W);
       setLength(L);
    }
    
    double Rectangle::area(void) 
    {
       return length * width;
    }
    
    double Rectangle::perimeter(void)
    {
       return 2*(length+width);
    }
    
    void Rectangle::setWidth(double W)
    {
       if ( W> 0 && W < 20.0) width=W;
       else                                 width=1.0;
    }
    
    void Rectangle::setLength(double L)
    {
       length= L > 0 && L < 20.0 ? L : 1.0;
    }
    
    double Rectangle::getWidth(void)
    {
       return width;
    }
    
    double Rectangle::getLength(void)
    {
       return length;
    }
    

  15. Write the main file.
    // 
    // Driver
    //
    #include <iostream>
    #include <iomanip>
    #include "crect.h"
    
    using namespace std;
    
    
    int main()
    {
       Rectangle a;
       Rectangle b(4.0,5.0);
       Rectangle c(67.0,888.0);
    
       cout << setiosflags(ios::fixed | ios::showpoint);
       cout << setprecision(1);
    	
       //  Rectangle a
       cout << "a : length    =" << a.getLength() << endl;
       cout << "a : width     =" << a.getWidth()  << endl;
       cout << "a : perimeter =" << a.perimeter() << endl;
       cout << "a : area      =" << a.area()      << endl << endl;
    
       //  Rectangle b
       cout << "b : length    =" << b.getLength() << endl;
       cout << "b : width     =" << b.getWidth()  << endl;
       cout << "b : perimeter =" << b.perimeter() << endl;
       cout << "b : area      =" << b.area()      << endl << endl;
    
       //  Rectangle c
       cout << "c : length    =" << c.getLength() << endl;
       cout << "c : width     =" << c.getWidth()  << endl;
       cout << "c : perimeter =" << c.perimeter() << endl;
       cout << "c : area      =" << c.area()      << endl << endl;
     
       system("pause");
       return 0;
    
    }
    
  16. Compile the project and run it!
    --------------------Configuration: crect - Win32 Debug--------------------
    Compiling...
    crect.cpp
    crectc.cpp
    Linking...
    
    crect.exe - 0 error(s), 0 warning(s)
    
    a : length    =1.0
    a : width     =1.0
    a : perimeter =4.0
    a : area      =1.0
    
    b : length    =5.0
    b : width     =4.0
    b : perimeter =18.0
    b : area      =20.0
    
    c : length    =1.0
    c : width     =1.0
    c : perimeter =4.0
    c : area      =1.0
    
    Press any key to continue