13.4 struct类型增强
c中定义结构体变量需要加上struct关键字,c++不需要。c中的结构体只能定义成员变量,不能定义成员函数。c++即可以定义成员变量,也可以定义成员函数。
13.4.1 结构提中即可以定义成员变量,也可以定义成员函数
struct Student{
string mName;
int mAge;
void setName(string name){mName = name;}
void setAge(int age){mAge = age;}
void showStudent(){
cout<<"Name: "<<mName<<"Age: "<<mAge<<endl;
}
}
13.4.2 c++中定义结构体变量不需要加struct关键字
void test01(){
Student student;
student.setName("John");
student.setAge(20);
student.showStudent();
}
13.5 bool类型关键字
标准c++的bool类型有两种内建的常量true(转换为整数1)和false(转换为整数0)表示状态。这三个名字都是关键字。bool类型只有两个值,true(1值),false(0值) bool类型占1个字节大小给bool类型赋值时,非0值会自动转换为true(1),0值会自动转换false(0)。
void test()
{
cout<<sizeof(false)<<endl;//为1. //bool类型占一个字节大小
bool flag = true;
flag = 10;
//给bool类型赋值时,非0值会自动转换为true(1),0值会自动转换false(0)
}