Skip to content

struct类型加强

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

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

cpp
#include <iostream>
#include <string>
using namespace std;

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

int main(int argc, char *argv[]) {
    test01();

    return 0;
}

程序输出:

shell
Name:John Age:20