The assignment of C + + objects and the copying of basic

Source: Internet
Author: User

Assigning values to Objects

If two or more objects are defined on a class, the objects can be assigned to each other, or the value of one object can be assigned to another object of the same type. The value referred to here refers to the value of the member of all data in the object . The assignment between objects is "=", and the object is assigned the following form:

Object Name 1 = object Name 2;

#include <iostream>
using namespace Std;
Class Box
{
Public
Box (int =10,int =10,int =10);
int volume ();
Private
int height;
int width;
int length;
};

Box::box (int h,int w,int len)
{
Height=h;
Width=w;
Length=len;
}

int Box::volume ()
{return (height*width*length);}

int main ()
{
Box Box1 (15,20,25), Box2;
cout<< "1" <<box1.volume () <<endl;
Box2=box1;
cout<< "1" <<box2.volume () <<endl;
return 0;
}

1. The assignment of an object only assigns a value to the data member in it, not to the member function. The process of assignment is to copy the data members of one object to the storage space of the data members of another object in the state of the storage space .

2. Data members of a class cannot include dynamically allocated data , otherwise there may be serious consequences when assigning a value.

Replication of objects

Sometimes you need to use multiple identical objects, so you need to copy them. Sometimes it is necessary to preserve the state of an object at a moment's time. This is more necessary to replicate . Quickly copy multiple identical objects with an existing object. Such as:

Box Box2 (box1);

Its function is to use the existing object Box1 to clone the new object Box2.

The general form is: Class name Object Name 2 (object 1), a special constructor is called when a new object is created---- copy constructor , this function is as follows:

Box::box (const box& B)

{

Height=b.height;

Width=b.width;

Length=b.length;

}

A copy constructor is also a constructor, but it has only one parameter, which is an object of this class (it cannot be another class object). The purpose of this copy constructor is to assign the value one by one of each data member of the argument object to the corresponding data member in the new object .

C + + also provides another statement to copy objects: Box box2=box1; Its basic form is as follows:

Class Name Object Name 1 = object Name 2;

You can also assign values to multiple objects: Box box2=box1,box3=box2; Compare the following definition statements:

int a=4,b=a; This form looks intuitive, but it's handy to use, but its role is to call the copy constructor.

Note: The difference between assignment and replication is that an object's assignment is to assign a value to an already existing object, so the assigned object must be defined before it can be assigned. The object's replication is to create an object from scratch and make it exactly the same as an existing object (including structure and values).

The difference between a common constructor and a copy constructor:

1. In form,

Class name (formal parameter list); Declarations of ordinary constructors, such as box (int h,int w,int len);

Class name (class Name & object name); Copy the declaration of the constructor, such as box (box &b);

2. When an object is created, the argument types are different. The system invokes a generic constructor or a copy constructor based on the type of the argument.

Box box1 (12,15,16); An argument is an integer that calls a normal constructor

Box Box2 (box1); The argument is the object name, and the copy constructor is called

3. Under what circumstances is called.

The normal constructor is called when an object is created in the program.

The copy constructor is called when a new object is copied by an existing object, and in 3 cases the object needs to be cloned;

1. You need to create an object in the program and initialize it with another object of the same type.

2. When the function's argument is an object of the class, when the function is called, the argument object needs to be fully passed to the parameter, that is, a copy of the argument needs to be established, which is to copy a formal parameter by an argument, which is implemented by invoking the copy constructor, which guarantees that the formal parameter has exactly the same value as the argument

Static data members

Static data members are common to individual objects, not just to an object member, and all objects can refer to them. Static data members occupy only a portion of the memory. Each object can refer to this static data member. The value of a static data member is the same for all objects. If you change its value, the value of the data member in each object changes at the same time.

static data members can be initialized, but can only be initialized outside of the class body .

int box::height=10; Represents the initialization of a data member in a box class.

Data type class Name:: static data member name = initial value; You do not have to add static to the initialization statement. You cannot initialize a data member with a parameter initialization table. Like:

Box (int h,int w,int len): Height (h) {} This is not true, and height is a static data member.

Static member functions

Static member functions are part of a class, but are not part of an object. If you want to invoke a public static member function outside of the class, use the class name and the domain operator "::", as

Box::volume (); The member function can also be called through the object name, A.volume (); But this does not mean that the function belongs to object A, only the type of a.

Remember: a static member function does not belong to an object, it is irrelevant to any object, so the static member function does not have the this pointer. That is, the non-static member function has this pointer, and the static member function does not have the this pointer . This determines that a static member function cannot access non-static members in this class . Static member functions can directly refer to static data members in this class. Because static data members belong to the same class, they can be referenced directly.

It is not absolutely impossible to access non-static members in this class, but not default access, so you cannot know which object to look for, and if you want to refer to non-static members in this class, you should add the object and member operator ".". such as:cout<<a.width<<endl;

The assignment of C + + objects and the copying of basic

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.