1. 程式人生 > >C++ 類的兩種定義方式

C++ 類的兩種定義方式

命名 c++ ech += esp set with aced spa

類內定義

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;
}

  

C++ 類的兩種定義方式