Advertisement
heroys6

Destructor & Sherloc

Jul 20th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. // =oper.cpp : Defines the entry point for the console application.
  2. //
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. int i = 0;
  9. char names[][7] = {
  10.     "object",
  11.     "str",
  12.     "op_r 1",
  13.     "op_r 2",
  14.     "op_r 3"
  15. };
  16.  
  17. class sample {
  18.     char *s;
  19.     int name;
  20. public:
  21.     sample(int i) { s = 0; name = i; cout << "Usual constructor of #" << names[name] << "\n"; }
  22.     sample(const sample &obj);
  23.     ~sample() {
  24.         if (s)
  25.         {
  26.             delete[] s;
  27.             cout << "Destructor of #" << names[name] << "\n";
  28.         }
  29.     }
  30.     void show() { cout << s << "\n"; }
  31.     void set(char *str);
  32.     sample operator=(sample op_r);
  33. };
  34.  
  35. sample::sample(const sample &obj)
  36. {
  37.     s = new char[strlen(obj.s) + 1];
  38.     strcpy(s, obj.s);
  39.     name = i++;
  40.     cout << "Constructor of copy #" << names[name] << "\n";
  41. }
  42.  
  43. void sample::set(char *str)
  44. {
  45.     s = new char[strlen(str) + 1];
  46.     strcpy(s, str);
  47. }
  48.  
  49. sample input()
  50. {
  51.     char instr[80];
  52.     sample str(i++);
  53.  
  54.     cout << "Enter the word: ";
  55.     cin >> instr;
  56.     str.set(instr);
  57.  
  58.     return str;
  59. }
  60.  
  61. sample sample::operator=(sample op_r)
  62. {
  63.     s = new char[strlen(op_r.s) + 1];
  64.     strcpy(s, op_r.s);
  65.     return *this;
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71.     sample object(i++);
  72.  
  73.     object = input();
  74.  
  75.     object.show();
  76. //system("pause");
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement