类的声明和类的实现分开

来源:互联网 发布:linux 使用socket通信 编辑:程序博客网 时间:2024/05/29 19:39

传智扫地僧课程学习笔记。


类的声明和实现分开,
可以自己去手动的创建.cpp和.h文件,


也可以通过IDE提供的快捷方式来做,
步骤为:
选中项目,
右键选“添加"->"类”->"c++类"
双击后,输入类名,自动生成cpp和h文件,


MyTeacher.h

#pragma once  //只包含一次/*#ifndef __MYTEACHER_H_  //ctrl +shift + u 变大写#define __MYTEACHER_H_*/class MyTeacher{private:int m_age;char m_name[32];public:void setAge(int age);int  getAge();};/*#endif*/

MyTeacher.cpp

#include "MyTeacher.h"void MyTeacher::setAge(int age){m_age = age;}int  MyTeacher::getAge(){return m_age;}

mainclass.cpp

#include <iostream>using namespace std;#include "MyTeacher.h"void mainxxx(){MyTeacher t1;t1.setAge(36);cout<<t1.getAge()<<endl;cout<<"hello..."<<endl;system("pause");return ;}

#pragma once
意思是,只包含一次,

然后注意头文件包含,不要漏了,

0 0