命令: g++ main.cpp head.cpp -o main
最好的文件是使makefile在linux里面
---------------------------------
head.h
---------------------------------
#ifndef HEAD_H
#define HEAD_H
# include<iostream>
# include<string>
using namespace std;
class test
{
public:
test(string & str);
void print(void);
private:
string s;
};
#endif
---------------------------------
head.cpp
---------------------------------
# include<iostream>
# include<string>
# include"head.h"
using namespace std;
test::test(string& str0)
{
s = str0;
}
void test::print(void)
{
cout<<"this is a test:"<<endl;
cout<<s<<endl;
}
---------------------------------
main.cpp
---------------------------------
# include"head.h"
using namespace std;
int main(void)
{
string str1("you are very good");
test a(str1);
a.print();
return 0;
}