C++基础(五)-struct

来源:互联网 发布:英语歌曲学英语知乎 编辑:程序博客网 时间:2024/05/22 23:58

 c中定义结构体变量需要加上struct关键字,c++不需要。

c中的结构体只能定义成员变量,不能定义成员函数。c++即可以定义成员变量,也可以定义成员函数。

//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;    }}; //2. c++中定义结构体变量不需要加struct关键字void test01(){    Student student;    student.setName("John");    student.setAge(20);    student.showStudent();}