C++类的成员变量和成员函数

#include<iostream>
#include<cstring>

using namespace std;

class student
{
    char name[20];  //姓名
    int id_num;     //学号
    int age;        //年龄
    char sex;       //性别


public:
    void set_age(int a);
    int get_age();
};
//在类外部定义set_age函数
void student::set_age(int a)
{
    age = a;
}
//在类外部定义get_age函数
int student::get_age()
{
    return age;
}
int main()
{
    student Alice;
    Alice.set_age(29);
    cout<<"The age of Alice is "<<Alice.get_age()<<endl;
    return 0;
}
posted @ 2022-08-19 22:48  luoganttcc  阅读(26)  评论(0)    收藏  举报