"Constructors for C + + classes specifically explained"

Source: Internet
Author: User

First, what is the function of the constructor?

class Dog
{
Public
Class-Dog constructor
Features: Class name as function, no return type
Dog ()
         {
weight = 0;
         }
         
Private:
      
//data members
int weight;
}


When the class object is created, the compiled system object allocates memory space and itself calls the constructor, and the initialization of the member that is completed by the constructor

int main ()

{

DOGDD;

}
The compilation system allocates memory space for each data member (weight) of the object DD and calls the constructor dog () to initialize its own weight value of the object dd to 0

So

constructor function: Initializes the data member of the object.


Ii. types of constructor functions

class Dog
{

Private:
Double height;
Double width;

Public:

No parameter constructors
Suppose you create a class that you do not write whatever the constructor is, the system will voluntarily generate the default no-participation constructor, the function is empty, nothing is done
Simply by writing one of the following constructors, the system will no longer be able to generate such a default constructor on its own, assuming that you want to have a non-participating constructor, you need to write it yourself.
Dog (void)
{
height= 0.0;
width=0.0;
}

General constructors (also called overloaded constructors)
General constructors can have a variety of parameters, a class can have more than one general constructor, if the number of parameters or different types (based on the principle of overloaded functions of C + +)
For example: You can also write a constructor for the Dog (int num)
Different constructors are called depending on the number of parameters passed in when creating an object
Dog (Double A, double b)
{
Height= A;
width = b;
}

Copy constructors (also known as copy constructors)
A copy constructor parameter is a reference to the class object itself that copies a new object of that class from an existing object, typically copying the value of the data member of an existing object into the newly created object in the function
If there is no write-copy constructor displayed, the system creates a copy constructor by default, but when there are pointer members in the class, there is a risk of creating the copy constructor by default, for detailed reasons, please inquire about "shallow copy", "Deep copy" article discussion
Dog (const dog & c)
{
Copy the value of the data member in Object C.
Height = C.heightl;
width = c.width;
}

A type conversion constructor that creates an object of this class based on an object of a specified type
For example, the following will create a dog object based on a double type of object
Dog::D og (Double R)
{
Height = r;
width = 0.0;
}

equals operator overload
Note that this is similar to the copy constructor, which copies the value of the object of this class = Right to the object to the left of the equals sign, which is not a constructor, and the object on both sides of the equal sign must have been created
If the Write = operator overload is not displayed, the system also creates a default = operator overload, which simply does some major copy work
Dog &operator= (const DOG&RHS)
{
First, check whether the right side of the equals is the object of the left, if the object itself, then return directly
if (this ==&RHS)
{
return *this;
}

Copy the member to the right of the equal sign to the left object
this->height= rhs.height;
this->width= rhs.width;
                
               Send the object to the left of the equal sign again
The purpose is to support the EG:A=B=C system to perform the b=c first.
Then execute a= (the return value of the b=c, which should be the B object after the C value is copied)
return *this;
        }

};

The following uses the class object defined above to illustrate how each constructor is used:

void Main ()
{
Call the non-participating constructor, the data member initial value is assigned to 0.0
Dog c1,c2;

Call General constructor, data member initial value is assigned
Dog C3 (1.0,2.5);
can also be used in the following form
Dog C3 = Dog (1.0,2.5);

Assigns the value of the C3 data member to C1
Because the C1 has been created beforehand, no matter what constructor is called
Only the = operator overload function is called
C1 = C3;

Invoking a type conversion constructor
The system first calls the type conversion constructor, creates 5.2 as a temporary object for this class, and then calls the Equals operator overload to assign the temporary object to C1
C2 = 5.2;

Call the copy constructor (there are two ways to call)
Dog c5 (C2);
Dog C4 = c2; // Note and = operator overloading is distinguished, where the object to the left of the equals sign is not created beforehand, so the copy constructor needs to be called, and the number of references is C2



}

"Constructors for C + + classes specifically explained"

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.