Classes: Creating classes

We have expanded the rectangle class to include a toString method so that we can use cout to print a rectangle as 3X4 for instance. We still have all of the code in one file. It is important to understand this example before proceeding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//A more complex Rectangle class
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
//Rectangle class declaration
class Rectangle {
    private:
        double width; 
        double length;
    public: 
        Rectangle(); //constructor
        Rectangle(double, double); //constructor
        void setWidth(double);
        void setLength(double);
        double getWidth() const;
        double getLength() const;
        double area() const;
        bool square() const;
        string toString() const;
};
    Rectangle::Rectangle() {
       width=1;
       length=1;
    } //constructor
    Rectangle::Rectangle(double w, double l) {
       if(w>0) width=w;
       if(l>0) length=l;
    } //constructor
    void Rectangle::setWidth(double w) {
       if(w>0) width=w;  //by making width private we can make sure that is >0
    } //setWidth
    void Rectangle::setLength(double l) {
       if(l>0) length=l;  
    } //setLength
    double Rectangle::getWidth() const {
      return width;
    } //getWidth
   double Rectangle:: getLength() const {
      return length;
   } //getWidth
   double Rectangle::area() const{
      return width*length;
   } //getArea
   bool Rectangle::square() const{
      return width==length;
   } //square
  string Rectangle::toString() const {
      std::ostringstream oss;  //a stream to append the values
      oss<<width<<"X"<<length;
      return oss.str();
   } //toString

int main() {
     Rectangle box;  //box is an instance of Rectangle class
     box.setWidth(5); //set member attribute width
     box.setLength(8); //set member attribute length
     Rectangle crate;  //crate is an instance of Rectangle class 1x1
     Rectangle carton(3,6); //carton is declared using constructor 3x6
     Rectangle myBox;
     double wdth, lnth;
     cout<<"Enter width of rectangle:";
     cin>>wdth;
     myBox.setWidth(wdth);
     if(myBox.getWidth()!=wdth) cout<<"The value "<<wdth<<" was not accepted\n";
     cout<<"Enter length of rectangle:";
     cin>>lnth;
     myBox.setWidth(lnth);
     if(myBox.getLength()!=lnth) cout<<"The value "<<lnth<<" was not accepted\n";
     cout<<myBox.toString()<<endl;
     cout<<"Area of box is "<<box.area()<<endl;  //prints 40
     cout<<"Area of carton is "<<carton.area()<<endl; //prints 18
     cout<<"Area of crate is "<<crate.area()<<endl; //prints 1
     cout<<carton.toString()<<endl; //prints 3X6
     if(box.square()) cout<<"The box is a square"<<endl;  //prints "The box is a square"
     if(crate.square()) cout<<"The box is a square"<<endl; //prints nothing
     system("pause");
     return 0;
} //main
CODE
rectangle2

NEXT: More methods for rectangle class