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

  

原文链接: https://www.cnblogs.com/lizhenlin/p/6813488.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 类的两种定义方式

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/253560

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午7:01
下一篇 2023年2月14日 上午7:02

相关推荐