Object assignment and copy operations in C++ are detailed

  • 2020-04-02 01:50:24
  • OfStack

The assignment of an object

If two or more objects are defined for a class, these objects of the same class can be assigned to each other, or the value of one object can be assigned to another object of the same class. The value of the object is the value of all the data members in the object.

Assignments between objects are also made using the assignment operator "=". The assignment operator "=", which could only be used to assign values to a single variable, has been extended to assign values between two objects of the same class by overloading the assignment operator.

In fact, this process is achieved by member replication, in which the member values of one object are copied one by one to the members of another object.
General form of object assignment:

Object name 1= object name 2;

Note that object 1 and object 2 must belong to the same class

======== sample code 1.1=============== =========


#include<iostream>
#include<string>
using namespace std;
class Student
{
 public:
  Student(int nu=0,string na="NULL",int=0);//The constructor
  void show();
 private:
  int num;
  string name;
  int score; 
};
Student::Student(int nu,string na,int sc)
{
 num=nu;
 name=na;
 score=sc; 
}
void Student::show()
{
 cout<<"date:"<<endl;
 cout<<"num:"<<num<<"tname:"<<name<<"tscore:"<<score<<endl;
}
int main()
{
 Student s1(1,"qianshou",99);
 s1.show();
 Student s2;
 s2=s1;
 s2.show();
 return 0;
}

Operation interface:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130907163113937.png ">

Description:

(1) the value of the object is assigned to the data member, but not to the member function.

The data members occupy the storage space, and the data members of different objects occupy different storage space. The assignment process is to copy the state of the data members of one object in the storage space to the storage space of the data members of another object.

The member functions of different objects are the same piece of function code that is neither needed nor assigned to.

(2) data members of class cannot include dynamically allocated data.


Replication of objects
Sometimes we need to use multiple identical objects and do the same initialization. Or sometimes, we need to preserve the state of the object for a moment.
To handle this situation, C++ provides a replication mechanism for objects, which quickly copies identical objects from a later object.
Its general form is

Class name object 2 (object 1)

Copy object 2 with object 1.


Student s2(s1);  

As you can see, it is similar to the way we defined the object in the previous section, but the arguments given in the parentheses are not the usual ones, but the objects.
When a new object is created, a special constructor, the copy constructor, is called.

The function looks like this


    Student::Student(const Student &b)  
    {  
        num=b.num;  
        name=b.name;  
        score=b.score;  
    }  

The copy constructor is also a constructor, but it has only one parameter, which is the object of this class, and it takes the form of an object reference (general convention and const declaration, so that the parameter value cannot be changed, so that the object value is not accidentally changed when the function is called). The purpose of this copy constructor is to assign the values of each data member of the argument object one by one to the values of the members in the new object.
For the statement
The same code at the page code block index 1
This is actually an object creation statement, creating a new object, s2. Since the argument given in parentheses is an object, the compilation system calls the copy constructor and passes the value of argument s1 to parameter b (which is a reference to s1).

C++ also provides another user-friendly form of replication, using assignment Numbers instead of parentheses
Its general form is:
The name of the class       Object name 1               =               Object name 2;


Student s2=s1;

You can also assign multiple objects in a single statement.

Student    s2=s1,s3=s2,s4=s3;  

The difference between object replication and assignment
The assignment of an object is to an existing object, so you must define the object to be assigned before you can assign. The replication of an object is to create a new object from scratch and make it identical to an existing object (including the structure of the object and the values of its members).

    #include<iostream>  
    #include<string>  
    using namespace std;  
    class Student  
    {  
        public:  
            Student(int nu=0,string na="NULL",int=0);//Constructor & NBSP;
            void show();  
            void reset();   
        private:  
            int num;  
            string name;  
            int score;   
    };  
    Student::Student(int nu,string na,int sc)  
    {  
        num=nu;  
        name=na;  
        score=sc;     
    }  
    void Student::reset()  
    {  
        num=0;  
        name="reset";  
        score=0;  
    }  
    void Student::show()  
    {  
        cout<<"date:"<<endl;  
        cout<<"num:"<<num<<"tname:"<<name<<"tscore:"<<score<<endl;  
    }  
    int main()  
    {  
        Student s1(1,"qianshou",99);//Instantiate an object s1& NBSP; & have spent
        Student s2;//Declaring an object s2& NBSP;
        s2=s1;//Perform object assignment, assigning the value of object s1 to s2& PI; & have spent
        s2.show();  
        Student s3(s2);//Perform the copy operation of the object & NBSP; & have spent
        s3.show();  
        s3.reset();//The data member in s3 has changed & NBSP; & have spent
        Student s4=s3;//Copy the changed s3 as s4& NBSP;
        s4.show();   
        return 0;  
    }  

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130907220152078.png ">

It should be noted that both the assignment constructor and the copy constructor are called automatically by the system. The programmer can define the copy constructor himself, and if no constructor is defined, the compiler automatically provides a default enough function that simply copies the data members of the class.
We can customize a copy constructor to see the effect:


    #include<iostream>  
    #include<string>  
    using namespace std;  
    class Student  
    {  
        public:  
            Student(int nu=0,string na="NULL",int=0);//Constructor & NBSP;
            Student(const Student &s);  
            void show();  
            void reset();   
        private:  
            int num;  
            string name;  
            int score;   
    };  
    Student::Student(int nu,string na,int sc)  
    {  
        num=nu;  
        name=na;  
        score=sc;     
    }  
    Student::Student(const Student &s)  
    {  
        num=s.num;  
        name=s.name;  
        score=s.score;  
        cout<<" The copy constructor completes "<<endl;  
    }  
    void Student::reset()  
    {  
        num=0;  
        name="reset";  
        score=0;  
    }  
    void Student::show()  
    {  
        cout<<"date:"<<endl;  
        cout<<"num:"<<num<<"tname:"<<name<<"tscore:"<<score<<endl;  
    }  
    int main()  
    {  
        Student s1(1,"qianshou",99);//Instantiate an object s1& NBSP; & have spent
        Student s2;//Declaring an object s2& NBSP;
        s2=s1;//Perform object assignment, assigning the value of object s1 to s2& PI; & have spent
        s2.show();  
        Student s3(s2);//Perform the copy operation of the object & NBSP; & have spent
        s3.show();  
        s3.reset();//The data member in s3 has changed & NBSP; & have spent
        Student s4=s3;//Copy the changed s3 as s4& NBSP;
        s4.show();   
        return 0;  
    }  

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130907220922468.png ">

Related articles: