1. 变量初始化
// 初始化
//
#include <iostream>
using namespace std;
// 定义结构体
struct Student
{
int age;
char name[10];
void sing() { cout << "is singing"; }
};
int main()
{
std::cout << "Hello World!\n";
// 复制初始化
int a = 2;
cout << a << endl;
// 直接初始化
int a1(3);
cout << a1 << endl;
// 声明了一个变量,会报错
/*int a2();
cout << a2 << endl;*/
// 列表初始化
int a3{ 2 }, b = { 4 };
int c{}, d = {}; //默认值为0
cout << a3 << b << c << d << endl;
// 引用结构体
struct Student stu1;
Student stu2;
stu1.age = 3;
cout << "stu1.age = " << stu1.age << endl;
stu2.sing();
}
运行结果如下:
Hello World!
2
3
2400
stu1.age = 3
is singing