home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tutorial / cpptutor / source / defmeths.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-15  |  2.3 KB  |  109 lines

  1.                              // Chapter 6 - Program 10 - DEFMETHS.CPP
  2. #include "iostream.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5.                             
  6. class defaults {
  7.    int size;         // A simple stored value
  8.    char *string;     // A name for the stored data
  9. public:
  10.       // This overrides the default constructor
  11.    defaults(void);
  12.    
  13.       // This overrides the default copy constructor
  14.    defaults(defaults &in_object);
  15.       
  16.       // This overrides the default assignment operator
  17.    defaults &operator=(defaults &in_object);
  18.       
  19.       // This destructor should be required with dynamic allocation
  20.    ~defaults(void);
  21.       
  22.       // And finally, a couple of ordinary methods
  23.    void set_data(int in_size, char *in_string);
  24.    void get_data(char *out_string);
  25. };
  26.  
  27. defaults::defaults(void)
  28. {
  29.    size = 0;
  30.    string = new char[2];
  31.    strcpy(string, "");
  32. }
  33.  
  34. defaults::defaults(defaults &in_object)
  35. {
  36.    size = in_object.size;
  37.    string = new char[strlen(in_object.string) + 1];
  38.    strcpy(string, in_object.string);
  39. }
  40.  
  41. defaults &defaults::operator=(defaults &in_object)
  42. {
  43.    delete [] string;
  44.    size = in_object.size;
  45.    string = new char[strlen(in_object.string) + 1];
  46.    strcpy(string, in_object.string);
  47.    return *this;
  48. }
  49.  
  50. defaults::~defaults(void)
  51. {
  52.    delete [] string;
  53. }
  54.  
  55. void defaults::set_data(int in_size, char *in_string)
  56. {
  57.    size = in_size;
  58.    delete [] string;
  59.    string = new char[strlen(in_string) + 1];
  60.    strcpy(string, in_string);
  61. }                                             
  62.  
  63.  
  64.  
  65. void defaults::get_data(char *out_string)
  66. {
  67. char temp[10];
  68.  
  69.    strcpy(out_string, string);
  70.    strcat(out_string, " = ");
  71.    _itoa(size, temp, 10);
  72.    strcat(out_string, temp);
  73. }
  74.  
  75. void main()
  76. {
  77. char buffer[80];
  78. defaults my_data;
  79.  
  80.    my_data.set_data(8, "hat size");
  81.    my_data.get_data(buffer);
  82.    cout << buffer << "\n\n";
  83.  
  84. defaults more_data(my_data);
  85.    more_data.set_data(12, "shoe size");
  86.    my_data.get_data(buffer);
  87.    cout << buffer << "\n";
  88.    more_data.get_data(buffer);
  89.    cout << buffer << "\n";
  90.  
  91.    my_data = more_data;
  92.    my_data.get_data(buffer);
  93.    cout << buffer << "\n";
  94.    more_data.get_data(buffer);
  95.    cout << buffer << "\n";
  96. }
  97.  
  98.  
  99.  
  100. // Result of execution
  101. //
  102. // hat size = 8
  103. //
  104. // hat size = 8
  105. // shoe size = 12
  106. //
  107. // shoe size = 12
  108. // shoe size = 12
  109.