home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 353_02 / answers / ch06_1.cpp < prev    next >
C/C++ Source or Header  |  1992-01-19  |  2KB  |  83 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, *medium, *large;
  50.  
  51.    small  = new box("small box ");
  52.    medium = new box("medium box ");
  53.    large  = new box("large box ");
  54.  
  55.    small->set(5, 7);
  56.    large->set(15, 20);
  57.  
  58.    cout << "The area of the " << small->get_area() << "\n";
  59.    cout << "The area of the " << medium->get_area() << "\n";
  60.    cout << "The area of the " << large->get_area() << "\n";
  61.  
  62.    small->change("first box ");
  63.    medium->change("second box ");
  64.    large->change("third box ");
  65.  
  66.    cout << "The area of the " << small->get_area() << "\n";
  67.    cout << "The area of the " << medium->get_area() << "\n";
  68.    cout << "The area of the " << large->get_area() << "\n";
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75. // Result of execution
  76. //
  77. // The area of the small box = 35
  78. // The area is the medium box = 64
  79. // The area is the large box = 300
  80. // The area of the first box = 35
  81. // The area is the second box = 64
  82. // The area is the third box = 300
  83.