💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
1.`Teacher.h`代码 ``` #include <string> using namespace std; class Teacher{ public: // 无参构造函数 Teacher(); // 带参构造函数及参数可附默认值 Teacher(string _name,int _age=23); //析构函数 ~Teacher(); //设置名字 void setName(string _name); //获取名字 string getName(); //设置性别 void setGender(string _gender); //获取性别 string getGender(); //设置年龄 void setAage(int _age); //获取年龄 int getAage(); //教学 void teach(); private: //变量:名字 string m_str_name; //变量:性别 string m_str_gender; //变量:年龄 int m_int_age; }; ``` ***** 2.`Teacher.cpp`代码 ``` #include "Teacher.h" #include <iostream> using namespace std; Teacher::Teacher() { // m_str_name="Jim"; // m_int_age=5; cout<<"Teacher()"<<endl; } Teacher::Teacher(string _name, int _age) { m_str_name=_name; m_int_age=_age; cout<<"Teacher(string _name, int _age)"<<endl; } Teacher::~Teacher() { cout<<"~Teacher()"<<endl; } void Teacher::setName(string _name) { m_str_name=_name; } string Teacher::getName() { return m_str_name; } void Teacher::setGender(string _gender) { m_str_gender=_gender; } string Teacher::getGender() { return m_str_gender; } void Teacher::setAage(int _age) { m_int_age=_age; } int Teacher::getAage() { return m_int_age; } void Teacher::teach() { cout<<" 现在正在上课"<<endl; } ``` ***** 3.`main.cpp`代码 ``` #include <iostream> #include <stdlib.h> #include <string> #include "Teacher.h" using namespace std; int main(){ Teacher t1; Teacher *p=new Teacher(); // delete p; system("pause"); return 0; } ```