c++构造函数(C++ constructor)
The c++ constructor Xiangjie (turn) 2009-08-28 17:53:28| classification: | font Tags: | programming
in
Small subscription
The c++ constructor has introduced knowledge in various c++ textbooks, but beginners often do not pay attention to observe and summarize the characteristics and usage of the various constructors, so here I summed up the characteristics of various c++ in the constructor according to their own experience in c++ programming, with examples, I hope to help beginners.
Explain the constructor of the c++ class
First, what does the constructor do?
Class Counter
{
Public:
/ / constructor for class Counter
Features: / / as a function of a class name, no return type
Counter ()
{
M_value = 0;
}
Private:
/ / data members
Int m_value;
}
The class object is created, the compiler system object allocates memory space, and automatically invokes the constructor by the constructor initializes the - > working members
Eg: Counter c1;
The compilation system allocates memory space for each data member (m_value) of the object C1, and calls the constructor Counter () to automatically initialize the m_value value of the object C1 to 0
So:
The constructor's function: initializes the data members of the object.
Two, the type of constructor
Class Complex
{
Private:
Double m_real;
Double m_imag;
Public:
/ / parameterless constructor
Create a class / / if you don't write any constructor, no args constructor system will automatically generate the default function, is empty, what to do
/ / as long as you write a following a constructor, the system will no longer automatically generate a default constructor, if you want to have such a parameterless constructor, need to write their own show to
Complex (void)
{
c++strcpy函数用法M_real = 0;
M_imag = 0;
}
/ / general constructor (also known as overloaded constructor)
/ / general constructor can have a variety of parameters, a class can have multiple general constructor, the premise is the number of parameters or different types (overloaded function principle based on c++)
For example: / / you can write a Complex (int Num) constructor out
/ / when the object is created according to the incoming parameters of different call different constructors
Complex (double, real, double, imag)
{
M_real = real;
M_imag = imag;
}
/ / the copy constructor (also known as the copy constructor) The copy constructor parameters for reference class / object
itself, according to an existing object is copied to a new object of the class, generally in the function will have the data members of an object value of a copy to the newly created object.
If there is no display / / write the copy constructor, the system will create a default copy constructor, but when the pointer members of the class, by default the copy constructor will create risks, please inquire about the specific reasons of "shallow" and "deep copy of the chapter
Complex (const Complex & C)
{
The object / / C data member values copied
M_real = c.m_real;
i _ img = c.m _ dgi;
}
/ / 类型转换构造函数, 根据一个指定的类型的对象创建一个本类的对象
/ / 例如: 下面将根据一个double类型的对象创建了一个complex 对象
complex: complex (double r)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。