1、静态成员
使用static关键字定义静态变量。静态变量只被初始化一次。同样,在类中也可以使用static关键字将数据成员定义为静态成员。静态成员与静态变量不是同一概念。
静态成员被同一个类的所有对象共享,通过类名类来访问。
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
public:
static int m_nCount; //用户计数
CUser() //默认构造函数
{
strcpy(m_Username, "MR"); //为数据成员赋值
strcpy(m_Password, "KJ"); //为数据成员赋值
}
};
int CUser::m_nCount = 10; //在全局区域对静态成员进行初始化
int main(int argc, char* argv[])
{
CUser User; //定义一个CUser对象
cout << " User 静态成员: " << User.m_nCount << endl; //输出静态成员m_nCount
CUser defUser; //定义一个CUser对象
defUser.m_nCount = 1; //设置静态成员数据
cout << "defUser 静态成员: " << defUser.m_nCount << endl; //输出静态成员数据
cout << "User 静态成员: " << User.m_nCount << endl; //输出静态成员数据
return 0;
}
2、隐藏的this指针
定义多个类对象时,每一个类对象都有自己的一份数据成员(静态成员除外)。
每个类的成员函数(非静态成员函数),都隐含着一个this指针,指向被调用对象的指针。
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
public:
CUser(const char* pszUser, const char* pszPassword)
{
strcpy(m_Username, pszUser); //为数据成员赋值
strcpy(m_Password, pszPassword); //为数据成员赋值
}
char* GetUserName() const
{
return (char*)m_Username; //获取用户名
//也可以写成下面这样
//return (char*)this->m_Username;
}
};
int main(int argc, char* argv[])
{
CUser User("Mrsoft", "Vc"); //定义CUser对象
CUser defUser ("Bccd", "Vb"); //定义CUser对象
cout << "User对象信息: " << User.GetUserName() << endl; //访问User对象的m_Username
cout << "defUser对象信息: " << defUser.GetUserName() << endl; //访问defUser对象的m_Username
return 0;
}
3、静态成员函数
可是使用static定义静态成员函数
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
static int m_nCount; //计数
public:
static void OutputCounter() //定义静态成员函数
{
cout << m_nCount << endl; //输出信息
}
CUser()
{
m_nCount ++;
strcpy(m_Username, "MR"); //为数据成员赋值
strcpy(m_Password, "KJ"); //为数据成员赋值
}
~CUser() //析构函数
{
if (m_nCount > 0)
m_nCount--;
}
};
int CUser::m_nCount = 0; //初始化静态数据
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
静态成员函数也可以在类外
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
static int m_nCount; //计数
public:
static void OutputCounter(); //声明静态成员函数
CUser()
{
m_nCount ++;
strcpy(m_Username, "MR"); //为数据成员赋值
strcpy(m_Password, "KJ"); //为数据成员赋值
}
~CUser()
{
if (m_nCount > 0)
m_nCount--;
}
};
void CUser::OutputCounter() //定义静态成员函数
{
cout << m_nCount << endl;
}
int CUser::m_nCount = 0; //初始化静态数据
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
4、重载成员函数
与普通的函数一样,类的成员函数(析构函数除外),也可以重载,在类内可以定义多个相同名称的成员函数。
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
public:
bool Login() //定义重载方法1
{
if (strcmp(m_Username,"MR")==0 && strcmp(m_Password,"KJ")==0)
{
cout << "登录成功!" << endl;
return true;
}
else
{
cout << "登录失败!" << endl;
return false;
}
}
bool Login(CUser &User) //定义重载方法2
{
if (strcmp(User.m_Username,"MR")==0 && strcmp(User.m_Password,"KJ")==0)
{
cout << "登录成功!" << endl;
return true;
}
else
{
cout << "登录失败!" << endl;
return false;
}
}
CUser()
{
strcpy(m_Username, "MR"); //为数据成员赋值
strcpy(m_Password, "KJ"); //为数据成员赋值
}
};
int main(int argc, char* argv[])
{
CUser User;
User.Login(); //调用第一个版本的Login函数
CUser defUser;
defUser.Login(User); //调用第2个版本的Login函数
return 0;
}
在类中同名的const成员函数和非const成员函数可以作为重载成员函数同时存在,即他们的参数列表完全相同。
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
public:
bool Login() //重载方法1
{
if (strcmp(m_Username, "MR")==0 && strcmp(m_Password, "KJ")==0)
{
cout << "登录成功!" << endl;
return true;
}
else
{
cout << "登录失败!" << endl;
return false;
}
}
bool Login(CUser &User) //重载方法2
{
if (strcmp(User.m_Username, "MR")==0 && strcmp(User.m_Password, "KJ")==0)
{
cout << "登录成功!" << endl;
return true;
}
else
{
cout << "登录失败!" << endl;
return false;
}
}
bool Login(CUser &User) const //重载方法3
{
if (strcmp(User.m_Username, "MR")==0 && strcmp(User.m_Password, "KJ")==0)
{
cout << "登录成功!" << endl;
return true;
}
else
{
cout << "登录失败!" << endl;
return false;
}
}
CUser()
{
strcpy(m_Username, "MR"); //为数据成员赋值
strcpy(m_Password, "KJ"); //为数据成员赋值
}
};
int main(int argc, char* argv[])
{
CUser User;
User.Login(); //调用第一个版本的Login函数
CUser defUser;
defUser.Login(User); //调用第2个版本的Login函数
const CUser conUser; //定义一个const对象
conUser.Login(User); //调用第3个版本的Login函数
//conUser.Login(); //错误,const对象不能够调用非const方法
return 0;
}
5、类数据成员指针
#include "stdafx.h"
#include "iostream.h"
#include "string.h"
class CUser //定义一个类
{
private: //设置成员访问级别
char m_Username[128]; //定义数据成员
char m_Password[128]; //定义数据成员
public:
int m_nLevel; //定义数据成员
CUser()
{
strcpy(m_Username, "MR"); //为数据成员赋值
strcpy(m_Password, "KJ"); //为数据成员赋值
}
};
int main(int argc, char* argv[])
{
int CUser::* pLevel; //定义类数据成员指针
pLevel = &CUser::m_nLevel; //为数据成员指针赋值
CUser User; //定义一个类对象
User.m_nLevel = 1; //为m_nLevel成员赋值
cout << "操作员级别: " << User.*pLevel << endl; //利用数据成员指针访问m_nLevel成员
return 0;
}