¿Cómo imprimo las cadenas usando la copia profunda en C ++?

#include <iostream.h>


using namespace std;

class demo
{
    string a;
    string *p;

    public: 

    demo() 
    {
     a=0;
     p = new int;               // DEFAULT CONSTRUCTORS
     *p = NULL;
    } 

    demo ( const string *q )
    {
        p= new int;
        *p=q;
    }


    demo (demo &r) {
      a= r.a;
      p= new int; 
      *p= *(r.p);
    }

    ~demo ()  {
       delete p;  
     }

    void show () {
       cout << a;
    } 
    void change () {
       s3.a=s2.a;
    }

};

int main () {

        demo s1;
        demo s2("Hello");
        demo s3(s2);
        s1.show();
        s2.show();
        s3.show();
        s2.change("Java");
        s2.show();
        s3.show();
 }

Salida deseada:

HelloHelloJavaHello
c++
-2
Akkië 8 sep. 2018 a las 08:43

3 respuestas

La mejor respuesta

Mi opinión: ver más abajo para la explicación.

//#include <iostream.h> MODIFIED: Modern library headers do not have extension.
#include <iostream>

using namespace std; 

class demo {
    string a;
    // string *p; DELETED: Not sure what was the purpose of this.

    public: 

    demo() 
    {
     //a=0;         DELETED: std::string already have a sane initialization.
     //                      and assigning it to 0 looks like a bad idea to me.
     //                      (it will be taken as a char * nullptr)
     //p = new int; DELETED: We removed p. Also p was a pointer to string
     //*p = NULL;   DELETED: we removed p (modern C++ will use nullptr)
    } 

    demo ( const string *q ): a(*q) // MODIFIED: use initializers
    {            
        //p= new int; DELETED: we don't have p.
        //*p=q;       DELETED: we don't have p.
        //However, see below for what you seem to need.
        //given your example program.
        //Also this constructor can have a problem if a
        //nullptr is used as parameter.
    }

    demo (const std::string &_a):a(_a) { 
        // NEW: to support the constructor from string
    }            

    demo (demo &r):a(r.a) {
      //a= r.a;      MOVED as initializer.
      //p= new int;  DELETED: we don't have a p.
      //*p= *(r.p);  DELETED: we don't have a p.
    }

    ~demo ()  {
       //delete p;  //No need
     }

    void show () {
       cout << a;  
    } 
    void change (const std::string &_a) { // MODIFIED: added signature.
       //s3.a=s2.a; MODIFIED: properly assign input value to member
       a = _a;
    }

};

int main () {

        demo s1;
        demo s2("Hello");
        demo s3(s2);
        s1.show();
        s2.show();
        s3.show();
        s2.change("Java");
        s2.show();
        s3.show();
 }

Vea el resultado en https://ideone.com/0wtq3l

0
jsantander 8 sep. 2018 a las 07:49
    #include <iostream>

    using namespace std; 

    class demo {
        string a;
        // string *p; DELETED: Not sure what was the purpose of this.

        public: 

        demo() 
                    {
         //a=0;         DELETED: std::string already have a sane initialization.
         //                      and assigning it to 0 looks like a bad idea to me.
         //                      (it will be taken as a char * nullptr)
         //p = new int; DELETED: We removed p. Also p was a pointer to string
         //*p = NULL;   DELETED: we removed p (modern C++ will use nullptr)
        } 

        demo ( const string *q ): a(*q) // MODIFIED: use initializers
        {
            //p= new int; DELETED: we don't have p.
            //*p=q;       DELETED: we don't have p.
        }


        demo (demo &r):a(r.a) { 
          //a= r.a;      MOVED as initializer.
          //p= new int;  DELETED: we don't have a p.
          //*p= *(r.p);  DELETED: we don't have a p.
        }

        demo (const std::string &_a):a(_a) { 
            // NEW: to support the constructor from string
        }        

        ~demo ()  {
           //delete p;  //No need
         }

        void show () {
           cout << a;  
        }           void change (const std::string &_a) { // MODIFIED: added signature.
           //s3.a=s2.a; MODIFIED: properly assign input value to member
           a = _a;
        }

    };

    int main () {

            demo s1;
            demo s2("Hello");
            demo s3(s2);                s1.show();
            s2.show();
            s3.show();
            s2.change("Java");
            s2.show();
            s3.show();
     }
0
Akkië 8 sep. 2018 a las 08:57

Lo primero es que está proporcionando asignación de memoria de tipo int a una cadena de tipo de datos

string p es el tipo de datos char *, por lo que lo que debe hacer es p = new char [size];

Lo segundo para la copia profunda, necesita constructor de copia o * operador de sobrecarga = *

Puedo decir cómo usarlo, pero es mejor aprender estos temas usted mismo para obtener un conocimiento completo de estos temas.

0
Vanraj Chawda 8 sep. 2018 a las 07:45