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

  1.                               // Chapter 5 - Programming exercise 1
  2. #include "iostream.h"
  3.  
  4. class one_datum {
  5.    int data_store;
  6. public:
  7.    void set(int in_value);
  8.    int get_value(void);
  9.    int square(void);
  10. };
  11.  
  12. void one_datum::set(int in_value)
  13. {
  14.    data_store = in_value;
  15. }
  16.  
  17. int one_datum::get_value(void)
  18. {
  19.    return data_store;
  20. }
  21.  
  22. int one_datum::square(void)
  23. {
  24.    return data_store * data_store;
  25. }
  26.  
  27. main()
  28. {
  29. one_datum dog1, dog2, dog3;
  30. int piggy;
  31.  
  32.    dog1.set(12);
  33.    dog2.set(17);
  34.    dog3.set(-13);
  35.    piggy = 123;
  36.  
  37. // dog1.data_store = 115;      This is illegal in C++
  38. // dog2.data_store = 211;      This is illegal in C++
  39.  
  40.    cout << "The value of dog1 is " << dog1.get_value() << "\n";
  41.    cout << "The value of dog2 is " << dog2.get_value() << "\n";
  42.    cout << "The value of dog3 is " << dog3.get_value() << "\n";
  43.    cout << "The value of piggy is " << piggy << "\n";
  44.  
  45.    cout << "The value of dog1 squared is " << dog1.square() << "\n";
  46.    cout << "The value of dog2 squared is " << dog2.square() << "\n";
  47. }
  48.  
  49.  
  50.  
  51.  
  52. // Result of execution
  53. //
  54. // The value of dog1 is 12
  55. // The value of dog2 is 17
  56. // The value of dog3 is -13
  57. // The value of piggy is 123
  58. // The value of dog1 squared is 144
  59. // The value of dog2 squared is 289
  60.