fork(1) download
  1. #include <iostream>
  2.  
  3. class Mystr{
  4. private:
  5. int length;
  6. char *text;
  7.  
  8. public:
  9. Mystr();
  10. Mystr(const char*);
  11. Mystr(const Mystr &obj);
  12. Mystr(int);
  13. ~Mystr();
  14.  
  15. void print();
  16. int count(const char);
  17. int len(){
  18. return length;
  19. }
  20.  
  21. Mystr operator+(const Mystr &obj);
  22. Mystr operator=(const Mystr &obj);
  23.  
  24. };
  25.  
  26. Mystr::Mystr(const char* str){
  27. length = 0;
  28. while (str[length] != '\0'){
  29. length++;
  30. }
  31. text = new char[length+1];
  32. for (int i = 0;i <= length;i++){
  33. text[i] = str[i];
  34. }
  35. std::cout << "char* constructor is triggered" << std::endl;
  36. }
  37. Mystr::Mystr(){
  38. length = 0;
  39. text = NULL;
  40. }
  41. Mystr::Mystr(const Mystr &obj){
  42. length = obj.length;
  43. text = new char[length+1];
  44. for (int i = 0;i <= obj.length;i++){
  45. text[i] = obj.text[i];
  46. }
  47. std::cout << text << ": "<< "copy constructor is triggered" << std::endl;
  48. }
  49. Mystr::~Mystr(){
  50. std::cout << text << ": ";
  51. delete[] text;
  52. std::cout << "destructor is triggered\n";
  53. }
  54. Mystr::Mystr(int n){
  55. if (n > 0){
  56. length = n;
  57. text = new char[n+1];
  58. text[n] = '\0';
  59. while (n--){
  60. text[n] = '*';
  61. }
  62. }else{
  63. length = 0;
  64. text = NULL;
  65. }
  66. std::cout << "int constructor is triggered" << std::endl;
  67. }
  68.  
  69. void Mystr::print(){
  70. for (int i = 0;i < length;i++){
  71. std::cout << text[i];
  72. }
  73. std::cout << std::endl;
  74. }
  75.  
  76. int Mystr::count(const char c){
  77. int res = 0;
  78. for (int i = 0; i < length;i++){
  79. if (text[i] == c) res++;
  80. }
  81. return res;
  82. }
  83.  
  84. Mystr Mystr::operator+(const Mystr &obj){
  85. Mystr res;
  86. res.length = this->length + obj.length;
  87. res.text = new char[res.length + 1];
  88. for (int i = 0;i < this->length;i++){
  89. res.text[i] = this->text[i];
  90. }
  91. for (int i = 0;i <= obj.length;i++){
  92. res.text[this->length + i] = obj.text[i];
  93. }
  94. std::cout << "operator+ is triggered" << std::endl;
  95. return res;
  96. }
  97. Mystr Mystr::operator=(const Mystr &obj){
  98. if (this != &obj){
  99. length = obj.length;
  100. delete[] this->text;
  101. text = new char[length+1];
  102. for (int i = 0;i <= obj.length;i++){
  103. this->text[i] = obj.text[i];
  104. }
  105. }
  106. std::cout << "operator= is triggered" << std::endl;
  107. return *this;
  108. }
  109.  
  110. int main(){
  111. Mystr *pstr = new Mystr("Hello");
  112. Mystr test1 = *pstr;
  113. Mystr test2 = test1 + " World";
  114. Mystr test3 = test2 + 3.14;
  115. *pstr = test1 = test1;
  116. test1.print();
  117. test2.print();
  118. test3.print();
  119. std::cout << test1.count('l') << " " << test2.len() << std::endl;
  120. delete pstr;
  121.  
  122. return 0;
  123. }
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
char* constructor is triggered
Hello: copy constructor is triggered
char* constructor is triggered
operator+ is triggered
 World: destructor is triggered
int constructor is triggered
operator+ is triggered
***: destructor is triggered
operator= is triggered
Hello: copy constructor is triggered
operator= is triggered
Hello: copy constructor is triggered
Hello: destructor is triggered
Hello: destructor is triggered
Hello
Hello World
Hello World***
2 11
Hello: destructor is triggered
Hello World***: destructor is triggered
Hello World: destructor is triggered
Hello: destructor is triggered