Assignment and copy operations of objects in C + + detailed parsing _c language

Source: Internet
Author: User

Assignment of an object

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

The assignment between objects is also done through the assignment operator "=". The original assignment operator "=" can only be used to assign a value to a single variable and is now extended to an assignment between two homogeneous objects, which is implemented by overloading the assignment operator.

In fact, this process is done through member replication, which copies the member value of an object to a member of another object, one by one.
The general form of an 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===============

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
Class Student
{
Public
Student (int nu=0,string na= "NULL", int=0);//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;
}

Run the interface:

Description

(1) An assignment value of an object assigns a value to the data member, not a member function.

Data members are the storage space, the data members of different objects occupy different storage space, and the assignment process is the storage space of the data member of one object in the state of the storage space to the member of the other object.

The member functions of different objects are the same function code snippet, neither need nor can assign value to them.

(2) A data member of a 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 keep the object in a moment's state.
To handle this situation, C + + provides a replication mechanism for objects that quickly replicate multiple identical objects with a later object.
Its general form is

Class Name Object 2 (object 1)

Object 2 is copied with object 1.

Copy Code code as follows:

Student S2 (S1);

As you can see, it is similar to the way the object was defined earlier, but the arguments given in parentheses are not the general variable, but the object.
When you create a new object, you invoke a special constructor--the copy constructor.

This function is like this.

Copy Code code as follows:

Student::student (const Student &b)
{
Num=b.num;
Name=b.name;
Score=b.score;
}

A copy constructor is also a constructor, but it has only one parameter, which is the object of this class, and takes the form of the object's Reference (General Convention Plus Const Declaration, so that the value of the parameter cannot be changed, lest the object value be modified when the function is invoked). The function of this copy constructor is to assign the value of each data member of the argument object to the value of the member of the new object, one by one.
For statements
Copy Code code as follows:

Student S2 (S1);

This is actually the statement that establishes the object, creating a new object S2. Because the argument given in parentheses is an object, the compilation system calls the copy constructor, and the value of the argument S1 to parameter B (b is a reference to S1).

C + + also provides another user-friendly form of replication with an assignment number instead of parentheses
Its general form is:
Class Name Object Name 1 = object Name 2;

Copy Code code as follows:

Student s2=s1;

You can also assign values to multiple objects in a single statement.
Copy Code code as follows:

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

The difference between the replication and assignment of an object
An object is assigned a value to an already existing object, so the assigned object must be defined before the assignment can be made. A copy of an object is a new object created from scratch and is exactly the same as an existing object (including the structure of the object and the value of the member)
Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
Class Student
{
Public
Student (int nu=0,string na= "NULL", int=0);//constructor
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
Student s2;//declares an object s2
s2=s1;//assigns values to the object and assigns the value of the object S1 to S2
S2.show ();
Student S3 (S2);//Copying of objects
S3.show ();
S3.reset (); The data member in the//S3 has changed
Student s4=s3;//copies the S3 after the change to S4
S4.show ();
return 0;
}

Run Result:

It should be explained that the invocation of the assignment constructor and the copy constructor is done automatically by the system. Programmers can define copy constructors themselves, and if no constructors are defined, the compilation system automatically provides a default sufficient function that simply replicates the data members in the class.
We can customize a copy constructor to see the effect:

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
Class Student
{
Public
Student (int nu=0,string na= "NULL", int=0);//constructor
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<< "copy constructor execution completed" <<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
Student s2;//declares an object s2
s2=s1;//assigns values to the object and assigns the value of the object S1 to S2
S2.show ();
Student S3 (S2);//Copying of objects
S3.show ();
S3.reset (); The data member in the//S3 has changed
Student s4=s3;//copies the S3 after the change to S4
S4.show ();
return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.