其他分享
首页 > 其他分享> > 21 类的声明 和类的实现分开

21 类的声明 和类的实现分开

作者:互联网

MyTeacher.h

#pragma once	// 只包含一次

/* 相当于上面的语句
#ifndef __MYTEACHER_H_ // ctrl + shift + u 变大写
#define __MYTEACHER_H_
#endif
*/

class MyTeacher
{
private:
	int age;
	char name[32];

public:
	void setAge();
	int getAge();
};


MyTeacher.cpp

// 类的实现
#include "MyTeacher.h"

void MyTeacher::setAge(int age) {
	m_age = age;
}
int MyTeacher::getAge() {
	return m_age;
}

y.cpp

#include"MyTeacher.h"
// #include"MyTeacher.h"	// 如果没有#pragma once  include两次会报错
// #include"MyTeacher.h"
void main() {

	MyTeacher t1;
	t1.setAge(36);
	cout << t1.getAge() << endl;
}

标签:21,int,age,MyTeacher,setAge,分开,include,声明,void
来源: https://blog.csdn.net/qq_36643203/article/details/119334822