此处讲两个知识点: 1.类的访问权限: //类的访问权限检查时机 //在编译时期对访问权限进行限制,在程序运行时期可以进行修改 突破访问权限的方法:
*(int*)&clock = 111;
2.利用C语言模拟类的封装 个人总结:此处讲了C到C++的过程,其中的指针可以看做C++的this指针(理解可能不够深刻) 封装方法:
**
typedef void((*PFN_SetHour)(int n));
struct taglock {
int nHour;
int nMinute;
int n_Second;
PFN_SetHour pfnSetHour;
};
void SetHour(struct taglock* c1,int n) {
c1->nHour = n;
}
struct taglock c1;
c1.pfnSetHour = SetHour;
c1.pfnSetHour(&c1,1);
3.学习地址:类的访问权限及C语言模拟类的封装 4.学习笔记:
#include <iostream>
#include "clock.h"
struct Cloth{
int nColor;
int nSize;
};
struct WashMchine {
private:
int nWidth;
int nHeight;
int nLength;
int nType;
int nBland;
void wash(Cloth& cl) {
printf("洗衣服 颜色=%d 大小=%d\r\n",cl.nColor,cl.nSize);
}
};
class CWashMchine {
private:
int m_nWidth;
int m_nHeight;
int m_nLength;
int m_nType;
int m_nBland;
public:
void wash(Cloth& cl) {
printf("洗衣服 颜色=%d 大小=%d\r\n", cl.nColor, cl.nSize);
}
};
typedef void((*PFN_SetHour)(int n));
struct taglock {
int nHour;
int nMinute;
int n_Second;
PFN_SetHour pfnSetHour;
};
void SetHour(struct taglock* c1,int n) {
c1->nHour = n;
}
int main(int argc,char* argv[])
{
CWashMchine wm;
Cloth cl;
wm.wash(cl);
CClock clock;
struct taglock c1;
c1.pfnSetHour = SetHour;
c1.pfnSetHour(&c1,1);
*(int*)&clock = 111;
clock.SetHour(25);
return 0;
}
|