1. static const int m_ nMyConst; //只有静态常量整型(枚举型)数据成员在可以在类中初始化; (enum MyEnum{A, B, C};
static const MyEnum m_myenum = B;
)
const int m_nMyVal; //必须在构造函数基/成员初始值设定项列表中初始化。
2. 关于继承
#include "stdafx.h"
#include <iostream>
using namespace std;
class student
{
public:
student(char *pName = "No name")
{
strcpy((char*)name, pName);
semesterhours = 0;
}
protected:
void addcourse(int hours)
{
semesterhours += hours;
cout << "semesterhours" << semesterhours << endl;
}
public:
int pubval;
protected:
int proval;
//private:
char name[40];
int semesterhours;
private:
int prival;
};
class graduatestudent:protected student
{
public:
void addcoursetoo(int h)
{
pubval = 10;
proval = 20;
//prival = 30;
prival2 = 40;
student::addcourse(h);
}
private:
int prival2;
};
class citizen:private graduatestudent
{
public:
void addcourse3(int h)
{
addcoursetoo(100);
addcourse(400);
pubval = 200;
proval = 300;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
student ds("undergradute");
graduatestudent gs;
//gs.addcourse(30);
gs.addcoursetoo(40);
getchar();
return 0;
}
3. 定义只能创建一个实例的类
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <iostream>
using namespace std;
class CDebug
{
public:
~CDebug(){}
void Print(const char *str)
{
printf(str);
}
static CDebug &GetInstance()
{
static CDebug debug;
//return s_debug;
return debug;
}
void SetName(char * n)
{
strcpy(Name, n);
}
void PrintName(void)
{
printf("Name: %s\n", Name);
}
private:
HANDLE m_StdOut;
char Name[10];
static CDebug * s_debug;
//防止直接生成实例
CDebug(){}
};
int _tmain(int argc, _TCHAR* argv[])
{
//CDebug object = CDebug::GetInstance();
CDebug::GetInstance().Print("hello dshe\n");
CDebug::GetInstance().SetName("ASON");
CDebug::GetInstance().PrintName();
CDebug object2 = CDebug::GetInstance();
object2.PrintName();
getchar();
return 0;
}
4.
#include "stdafx.h"
#include "iostream"
using namespace std;
class MyClass
{
public:
MyClass(char * n = "No Name")
{
printf("MyClass\n");
strcpy(m_Name, n);
}
static MyClass GetObj(char * nn)
{
return MyClass(nn);
}
~MyClass()
{
printf("~MyClass\n");
}
void Print(void)
{
printf("Name: %s\n", m_Name);
}
private:
char m_Name[10];
};
int _tmain(int argc, _TCHAR* argv[])
{
MyClass p;
p = MyClass("aAAbBB");
p.Print();
getchar();
return 0;