0.初始化与赋值的区别
意义上:
1)初始化:一个变量或者一个对象在产生的时候就赋予一个值,属于伴随性质
2)赋值:在一个变量或者一个对象在产生之后的任意时刻赋予一个值,属于任意性质
宏观代码上:
1)两者作用相同
2)对于数组和结构体来说,初始化和赋值的的形式不同。对于数组,可以使用花括号一起初始化,如果赋值的话,就只能单个元素就行;对于结构体,可以使用花括号初始化,否则只能通过“.”来访问变量进行赋值
#include <iostream>
#include <string>
using namespace std;
struct MyStruct
{
int aa;
float bb;
string cc;
};
int main()
{
int a[3] = { 1,2,3 };
int b[3];
b[0] = 1;
b[1] = 2;
b[2] = 3;
MyStruct stu1 = { 1,3.14f,"we are csdn" };
MyStruct stu2;
stu2.aa = 1;
stu2.bb = 3.14f;
stu2.cc = "we are csdn";
cout << stu1.aa << endl;
cout << stu1.bb << endl;
cout << stu1.cc << endl;
system("pause");
return 0;
}
执行结果:
3)对于引用和const常量来说,只能初始化不能赋值
1.类中的变量初始化
1)一般情况下,声明一个类是并不占内存的,如果直接在类中给变量初始化也是不允许的,但在VS2017下自己测试了一下,居然可以
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
int a = 1;
float b = 3.14f;
};
int main()
{
Cperson op;
cout << op.a << endl;
cout << op.b << endl;
Cperson op1;
cout << op1.a << endl;
cout << op1.b << endl;
system("pause");
return 0;
}
执行结果:
2.什么是构造函数
构造函数也是一个函数,这个函数有几个特点:
1)函数的名字与类的名字相同
2)在创建一个对象时,构造函数就自动执行,但是在声明一个类的指针对象时,构造函数不会被调用,当new一个空间的时候,构造函数才会被调用
3)构造函数一般用来对数据成员的赋值,这也是它的一般性作用
4)构造函数没有返回值
5)一个类里面也可以有多个构造函数,这些构造函数根据参数的不同,构成重载,根据参数的传递来选择调用哪个构造函数
6)可以不用显式的定义构造函数,这种情况下,编译器会自动帮我们生成一个空构造函数,什么也不执行;如果我们显式的声明了一个构造函数,那么这个构造函数就会覆盖默认的空构造函数
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
int a;
float b;
Cperson()
{
a = 1;
b = 3.14f;
cout << "Beginning..." << endl;
}
};
int main()
{
Cperson op1;
cout << op1.a << endl;
cout << op1.b << endl;
Cperson *op2; //构造函数没有被调用
op2 = new Cperson; //构造函数被调用
cout << op2->a << endl;
cout << op2->b << endl;
system("pause");
return 0;
}
执行结果:
3.构造函数的类型
虽然构造函数没有返回值,但可以有参数,如果构造函数有参数,那么在创建对象时,就一定要传入参数,否则会报错。同时,构造函数也可以指定形参默认值,在传递参数不够时就使用默认值,这一点与基本的函数相同;也可以重载
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
int a;
float b;
Cperson(int aa, float bb)
{
a = aa;
b = bb;
cout << "Beginning..." << endl;
}
};
int main()
{
int a1 = 1;
float b1 = 3.14f;
Cperson op1(a1, b1);
cout << op1.a << endl;
cout << op1.b << endl;
Cperson *op2; //构造函数没有被调用
op2 = new Cperson(a1, b1); //构造函数被调用
cout << op2->a << endl;
cout << op2->b << endl;
system("pause");
return 0;
}
执行结果:
4.初始化列表
4.1作用:
对数据成员进行初始化
4.2格式:
构造函数():变量名1(数值),变量名2(数值)
{} //!变量名不在花括号的后面,而是在花括号的前面
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
Cperson() :_a(1), _b(3.14f), _c("we are csdn")
{
}
void show()
{
cout << _a << endl;
cout << _b << endl;
cout << _c << endl;
}
private:
int _a;
float _b;
string _c;
};
int main()
{
Cperson op;
op.show();
system("pause");
return 0;
}
执行结果:
4.3传递参数初始化列表
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
Cperson(int aa, float bb, string cc) :_a(aa), _b(bb), _c(cc)
{
}
void show()
{
cout << _a << endl;
cout << _b << endl;
cout << _c << endl;
}
private:
int _a;
float _b;
string _c;
};
int main()
{
Cperson op(1, 3.14f, "we are csdn");
op.show();
system("pause");
return 0;
}
执行结果:
注意:
1)在构造函数执行时,先执行初始化列表,实现变量的初始化,然后再执行函数内部的语句
2)成员初始化的顺序只与声明的顺序有关,而跟初始化列表的顺序无关。例如在上面的初始化列表中,我们写成:_c(cc), _b(bb), _a(aa),但是我们还是先初始化变量_a,然后_b,然后_c,因为我们先声明的变量_a,然后_b,然后_c
3)成员之间可以相互初始化:a(12), b(a) //a,b为相同类型的话
看一个例子:
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
Cperson():_b(12),_a(_b)
{
}
void show()
{
cout << _a << endl;
cout << _b << endl;
}
private:
int _a;
int _b;
};
int main()
{
Cperson op;
op.show();
system("pause");
return 0;
}
执行结果:
从这个结果就可以看出来,在初始化列表中,虽然我们把_b的初始化写在前面的,但由于先声明的变量_a,所以_a先被初始化,并且用_b的值去初始化_a,但此时_b并没有被初始化,所以是一个随意值去初始化的_a,然后再用12初始化的_b,所以得到这样的结果
5.引用和const常量的初始化
1)当类成员中有引用和const常量时就一定得初始化,否则会报错
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
Cperson(int dd)
{
cout << _a << " " << _b << " " << _c << endl;
}
void show()
{
cout << _a << " " << _b << " " << _c <<endl;
}
private:
int _a;
int &_b;
const int _c;
};
int main()
{
int aa = 12;
Cperson op(aa);
op.show();
system("pause");
return 0;
}
编译器报错:
修改之后:
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
Cperson(int dd):_a(dd),_b(dd),_c(dd)
{
cout << _a << " " << _b << " " << _c << endl;
}
void show()
{
cout << _a << " " << _b << " " << _c <<endl;
}
private:
int _a;
int &_b;
const int _c;
};
int main()
{
int aa = 12;
Cperson op(aa);
op.show();
system("pause");
return 0;
}
执行结果:
为什么中间那行不是12呢?首先在主函数中,12传递给形参dd,dd变成12,所以第一行输出三个12;但是dd的作用域只在构造函数中,在执行函数show的时候,dd已经被销毁了,而引用_b关联了一个未知的内存,所以得到了一个未知的值
如果想要得到正确的结果,我们应该在构造函数的形参那里加上一个&,使之成为一个实参的引用,即:
Cperson(int &dd):_a(dd),_b(dd),_c(dd)
这样dd是实参aa的引用,_b是dd的引用,也是aa的引用,但aa的作用域就在整个主函数内了,所以得到正确结果
注意:
在有多个构造函数的时候,初始化列表的使用,依靠于使用的那个构造函数。即,使用哪个构造函数就使用那个构造函数后面的初始化列表
6.数组初始化
格式:构造函数():数组名() //后面的这个括号里不能添加0或者其他数值
注意:这种写法在VS是支持的,但是在VC中是不支持的,一般话的做法是:在构造函数中使用for循环来实现数组的每个元素赋值,或者用memset函数一次性为数组设值
#include <iostream>
#include <string>
using namespace std;
class Cperson
{
public:
Cperson():a()
{
}
void show()
{
for (int i = 0; i < 4; i++)
{
cout << a[i] << endl;
}
}
private:
int a[4];
};
int main()
{
Cperson op;
op.show();
system("pause");
return 0;
}
执行结果:
7.结构体的初始化
格式1:构造函数():结构体名({各个结构体成员的初始值,按顺序用逗号隔开})
注意:结构体可以这样用花括号进行初始化,但是数组不可以
struct MyStruct
{
int as;
float bs;
};
class Cperson
{
public:
Cperson() :_stu({1,1.23f})
{
}
void show()
{
cout << _stu.as << " " << _stu.bs << endl;
}
private:
MyStruct _stu;
};
格式2:构造函数(传递进来一个结构体):结构体名(传进来的结构体)
#include <iostream>
#include <string>
using namespace std;
struct MyStruct
{
int as;
float bs;
};
class Cperson
{
public:
Cperson(MyStruct stu2) :_stu(stu2)
{
}
void show()
{
cout << _stu.as << " " << _stu.bs << endl;
}
private:
MyStruct _stu;
};
int main()
{
MyStruct stu0 = { 12,12.34f };
Cperson op(stu0);
op.show();
system("pause");
return 0;
}
执行结果: