fork download
  1. //#include <iostream.h> MODIFIED: Modern library headers do not have extension.
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class demo {
  7. string a;
  8. // string *p; DELETED: Not sure what was the purpose of this.
  9.  
  10. public:
  11.  
  12. demo()
  13. {
  14. //a=0; DELETED: std::string already have a sane initialization.
  15. // and assigning it to 0 looks like a bad idea to me.
  16. // (it will be taken as a char * nullptr)
  17. //p = new int; DELETED: We removed p. Also p was a pointer to string
  18. //*p = NULL; DELETED: we removed p (modern C++ will use nullptr)
  19. }
  20.  
  21. demo ( const string *q ): a(*q) // MODIFIED: use initializers
  22. {
  23. //p= new int; DELETED: we don't have p.
  24. //*p=q; DELETED: we don't have p.
  25. }
  26.  
  27.  
  28. demo (demo &r):a(r.a) {
  29. //a= r.a; MOVED as initializer.
  30. //p= new int; DELETED: we don't have a p.
  31. //*p= *(r.p); DELETED: we don't have a p.
  32. }
  33.  
  34. demo (const std::string &_a):a(_a) {
  35. // NEW: to support the constructor from string
  36. }
  37.  
  38. ~demo () {
  39. //delete p; //No need
  40. }
  41.  
  42. void show () {
  43. cout << a;
  44. }
  45. void change (const std::string &_a) { // MODIFIED: added signature.
  46. //s3.a=s2.a; MODIFIED: properly assign input value to member
  47. a = _a;
  48. }
  49.  
  50. };
  51.  
  52. int main () {
  53.  
  54. demo s1;
  55. demo s2("Hello");
  56. demo s3(s2);
  57. s1.show();
  58. s2.show();
  59. s3.show();
  60. s2.change("Java");
  61. s2.show();
  62. s3.show();
  63. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
HelloHelloJavaHello