home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CPTUTOR2.ZIP / ANSWERS.ARC / CH06_1.CPP < prev    next >
C/C++ Source or Header  |  1990-07-20  |  2KB  |  81 lines

  1.                               // Chapter 6 - Programming exercise 1
  2. #include "iostream.h"
  3. #include "string.h"
  4.  
  5. class box {
  6.    int length;
  7.    int width;
  8.    char line_of_text[25];
  9. public:
  10.    box(char *input_line);             //Constructor
  11.    void set(int new_length, int new_width);
  12.    int get_area(void);
  13.    void change(char *new_text);
  14. };
  15.  
  16.  
  17. box::box(char *input_line)        //Constructor implementation
  18. {
  19.    length = 8;
  20.    width = 8;
  21.    strcpy(line_of_text, input_line);
  22. }
  23.  
  24.  
  25. // This method will set a box size to the two input parameters
  26. void box::set(int new_length, int new_width)
  27. {
  28.    length = new_length;
  29.    width = new_width;
  30. }
  31.  
  32.  
  33. // This method will calculate and return the area of a box instance
  34. int box::get_area(void)
  35. {
  36.    cout << line_of_text << "= ";
  37.    return (length * width);
  38. }
  39.  
  40.  
  41. void box::change(char *new_text)
  42. {
  43.    strcpy(line_of_text, new_text);
  44. }
  45.  
  46.  
  47. main()
  48. {
  49. box small("small box "),           //Three boxes to work with
  50.     medium("medium box "),
  51.     large("large box ");
  52.  
  53.    small.set(5, 7);
  54.    large.set(15, 20);
  55.    
  56.    cout << "The area of the " << small.get_area() << "\n";
  57.    cout << "The area of the " << medium.get_area() << "\n";
  58.    cout << "The area of the " << large.get_area() << "\n";
  59.    
  60.    small.change("first box ");
  61.    medium.change("second box ");
  62.    large.change("third box ");
  63.  
  64.    cout << "The area of the " << small.get_area() << "\n";
  65.    cout << "The area of the " << medium.get_area() << "\n";
  66.    cout << "The area of the " << large.get_area() << "\n";
  67.    
  68. }
  69.  
  70.  
  71.  
  72.  
  73. // Result of execution
  74. //
  75. // The area of the small box = 35
  76. // The area is the medium box = 64
  77. // The area is the large box = 300
  78. // The area of the first box = 35
  79. // The area is the second box = 64
  80. // The area is the third box = 300
  81.