类:
C++结构体中可以定义函数,换一个新的名字就是类,用class代替struct
class name
{
//成员函数
//成员变量
};
ex:
学生类:变量有姓名性别 、函数有写作业做运动等
(下面是定义和声明放在一起时)
#include <iostream>
using namespace std;
struct Student{
public:
//成员变量
char _name[20];
char _gender[3];
int age;
//成员函数
void SetStudentInfo(const char* name,const char* gender,int age){
//这个是啥
strcpy(_name,name);
strcpy(_gender,gender);
_age=age;
}
};
int main(){
student s;
s.SetStudentIofo("Bob","男",18);
return 0;
}
定义和声明分开时:常使用

原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/274461.html