C++ struct

 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();
}

 

posted @ 2019-02-25 10:19  吹过田野的风  阅读(317)  评论(0编辑  收藏  举报