/*ClsUser.h*/
//编译一次
#pragma once
//防止重复引用
#ifndef CLS_USER_H
#define CLS_USER_H
//用到了printf
#include
//struct和class关键字都可以
struct ClsUser
{
private:
int m_atr_UserId;//m_成员变量,atr_属性的缩写,配合WhoFeature的方式命名
public:
ClsUser();//无参数构造函数
ClsUser(int in_UserId);//有参数构造函数
~ClsUser();//析构函数,释放内存,数值归0
void mthd_setUserId(int in_UserId);//mthd_方法的缩写,用doSbSth的方式命名
void mthd_getUserId();
};
#endif
/*ClsUser.cpp*/
#ifndef CLS_USER_C
#define CLS_USER_C
#include "ClsUser.h"
ClsUser::ClsUser(){
ClsUser::m_atr_UserId = 0;
}
ClsUser::ClsUser(int in_UserId){
ClsUser::m_atr_UserId = in_UserId;
}
ClsUser::~ClsUser(){
ClsUser::m_atr_UserId = 0;
}
void ClsUser::mthd_setUserId(int in_UserId)
{
ClsUser::m_atr_UserId = in_UserId;
}
void ClsUser::mthd_getUserId()
{
printf("UserId:%d\n", ClsUser::m_atr_UserId);
}
#endif
/*ClsUserMain.cpp*/
#include "ClsUser.h"
int main()
{
ClsUser obj_user(1008611);//obj_对象前缀
obj_user.mthd_setUserId(10086);//改变一开始的赋值为10086
obj_user.mthd_getUserId();
obj_user.~ClsUser();
return 0;
}
编译语句
g++ -o ClsUserMain ClsUserMain.cpp ClsUser.cpp
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)