home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CPTUTOR2.ZIP / ANSWERS.ZIP / CH06_2.CPP < prev    next >
C/C++ Source or Header  |  1990-07-20  |  2KB  |  72 lines

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