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?
       Double a;
    
       cout << "a : area      =" << a.area()      << endl << endl; 
    

  10. 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

    Rectanglec(10.0,10.0) // set the length and widt
    No we can handle this with deafult arguments:

  11. Write the constructor

    Rectangle(double=1.0, double=1.0);  // Constructor
    
    Rectangle::Rectangle(double w, double l)
    {
    	setWidth(w);
    	setLength(l);
    }
    

    .

  12. 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
    

  13. Write the member functions
    //
    //  Member Functions for Rectangle
    //
    #include 
    #include "crect.h"
    
    
    //   Constructor
    Rectangle::Rectangle(double w, double l)
    {
    	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;
    }
    


  14. Write the main file.

    // 
    // Driver
    //
    #include <iostream.h>
    #include <iomanip.h>
    #include "crect.h"
    
    void 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;
    
    }
    


  15. 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