fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class foo
  6. {
  7. char *str;
  8. int len;
  9. static int number;
  10. public:
  11. foo();
  12. foo(const foo&);
  13. void get();
  14. ~foo();
  15. };
  16.  
  17. int foo::number = 0;
  18. foo::foo()
  19. {
  20. str = 0;
  21. len = 0;
  22. cout << "Simple Constructor\n";
  23. }
  24. foo::foo(const foo &st)
  25. {
  26. len = strlen(st.str);
  27. str = new char[len+1];
  28. strcpy(str,st.str);
  29. number++;
  30. cout << str << endl;
  31. }
  32. void foo::get(){
  33. static char buf[1024];
  34. cin.getline(buf, 1024);
  35. len = strlen(buf);
  36. str = new char[1 + len];
  37. strcpy(str, buf);
  38. }
  39. foo::~foo()
  40. {
  41. cout << number-- << endl;
  42. delete [] str;
  43. }
  44.  
  45. int main()
  46. {
  47. foo f1;
  48. f1.get();
  49. foo f2 = f1;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 3464KB
stdin
test string
stdout
Simple Constructor
test string
1
0