home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / CTUTOR / ANSWERS.ZIP / CH05_2.CPP < prev    next >
C/C++ Source or Header  |  1990-07-20  |  2KB  |  73 lines

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