C++ 类的两种定义方式
类内定义
class Teacher { private: string _name; int _age; public: Teacher() { printf("create techer \n"); } Teacher(string name) { _name = name; printf("create techer with name \n"); } void SetName(string name) { _name = name; } string GetName() { return _name; } void Say() { string des = "I'm Teacher and my name is "; des += _name; cout<< des<< endl; //printf(des); } };
类外定义
namespaceDemo.h namespace MyPrintSpace { void Say(); } namespaceDemo.cpp #include "namespaceDemo.h" //为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称, void MyPrintSpace::Say() { cout<<"MyPrintSpace say()"<<endl; }