// 类的申明和实现分开写在不同的文件中
main.cpp
// main.cpp
#include <iostream>
#include <string>
#include "student.h"
using namespace std;
int main(void)
{
Student x1("柳絮飘",22,"学明路115号");
Student x2(x1);
// ----
return 0;
}
//类的头文件 student.h
// student.h
#include <string>
using namespace std;
class Student
{
public:
// 存
void setname(string s);
void setage(int y);
void setaddress(string add);
// 取
string getname();
int getage();
string getaddress();
Student & show();
Student(string name,int age,string address);
~Student();
Student(Student& copyfun);
protected:
private:
string name;
int age;
string address;
};
// 类的实现 student.cpp
//student.cpp
#include "student.h"
#include <iostream>
#include <string

这篇博客介绍了如何在C++中将类的声明与实现分开,并重点讲解了拷贝构造函数的使用,通过示例展示了如何在main.cpp中调用拷贝构造函数创建对象。
最低0.47元/天 解锁文章
871

被折叠的 条评论
为什么被折叠?



