C++ class constructor
- 2020-04-02 02:23:33
- OfStack
//What is a constructor for
/* When a class object is created, the compiler object allocates memory space and automatically calls the constructor -> The constructor does the initialization of the member
eg: Counter c1;
Compile the system as an object c1 Each data member (m_value) Allocate memory space and call the constructor Counter( ) Initializes the object automatically, after initialization c1 the m_value Value is set to 0
Therefore: constructor function: initializes the data member of the object. */
class Counter
{
public: //Constructor of class Counter, with the class name as the function name and no return type
Counter(){
m_value = 0;
}
private:
int m_value; //Class private data member
}
//Type of constructor
#include <iostream>
using namespace std;
class Complex
{
private :
double m_real;
double m_imag;
public:
//* no argument constructor
//If you create a class and you don't write any constructors, the system will automatically generate the default no-argument constructor, which is empty, and does nothing, right
//As long as you write one of the following constructors, the system will not automatically generate such a default constructor, if you want to have one of the following constructors, you need to write it out yourself
Complex(void)
{ m_real = 0.0;
m_imag = 0.0;
}
//* general constructor (also called overloaded constructor)
//General constructors can have a variety of parameter forms, a class can have more than one general constructor, as long as the number of parameters or different types (based on c++ overloaded function principle)
//For example, you can also write out a constructor for Complex(int num) and create objects that call different constructors depending on the parameters passed in
Complex(double real, double imag)
{ m_real = real;
m_imag = imag;
}
//* copy constructor (also known as copy constructor)
//The copy constructor parameter is a reference to the class object itself, which is used to copy a new object of this class from an existing object
//If there is no shown write copy constructor, the system creates a copy constructor by default, but when there is a pointer member in the class, there is a risk that the system creates the copy constructor by default, for reasons discussed in the "shallow copy" and "deep copy" articles
Complex(const Complex & c)
{ //Copies the data member values from object c
m_real = c.m_real;
m_imag = c.m_imag;
}
//* type conversion constructor, according to a specified type of object to create an object of this class, it is important to note that this is actually a general constructor, but for the occurrence of such a single-parameter constructor, C++ will default to the corresponding parameters of the type to the type of this type,
//Sometimes this transition to privacy is undesirable, so explicit is used to limit the transition.
//For example, a Complex object is created based on an object of type double
Complex(double r)
{ m_real = r;
m_imag = 0.0;
}
//Equal sign operator overload (also called assignment constructor)
//Note that this is similar to the copy constructor, which copies the value of the class object on the right of = to the object on the left of the equal sign.
//If there is no overloading of the write = operator shown, the system also creates a default overloading of the = operator, doing only basic copying
Complex &operator=(const Complex &rhs )
{ //First check whether the object on the right of the equal sign is the object on the left
if ( this == &rhs )
{ return *this;
}
//Copy the member on the right of the equal sign into the object on the left
this->m_real = rhs.m_real;
this->m_imag = rhs.m_imag;
//A =b=c the system runs b=c and then a=(the return value of b=c, which should be the b object after copying the c value).
return *this;
}
};
//Use the class objects defined above to illustrate the use of each constructor:
int main()
{
//The parameterless constructor is called, and the initial value of the data member is assigned to 0.0
Complex c1,c2;
//The general constructor is called and the initial value of the data member is assigned to the specified value
Complex c3(1.0,2.5);
//Of course, you can also use the following form
// Complex c3 = Complex(1.0,2.5);
//& have spent Assign the value of the data member of c3 to the previously created object c1
//& have spent Since c1 has been created in advance, no constructor is called here
//& have spent Only the = operator overload function is called
c1 = c3;
//& have spent Call the type conversion constructor
//& have spent The system first calls the cast constructor, which creates 5.2 as a temporary object for this class, and then calls the equals operator overload, which assigns the temporary object to c2
c2 = 5.2;
//Call the copy constructor (there are two ways to call it)
Complex c5(c3);
Complex c4 = c3;
//Note the difference between the = operator overloading, where the object to the left of the equal sign is not created in advance, so you need to call the copy constructor with the parameter c2
//This is particularly important because this is initialization, not assignment.
//There are actually two types of initialization in C++ : copy initialization and assignment initialization.
//C5 USES replication initialization and c4 USES assignment initialization, both of which call the copy constructor.
}