类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
///////////////////////////////////////
//
// filename: Tdate.h
//
//////////////////////////////////////
class Tdate
{
public:
void Set(int,int,int);
int IsLeapYear();
void Print();
private:
int month;
int day;
int year;
};
///////////////////////////////////////
//
// filename: Tdate.cpp
//
//////////////////////////////////////
#include <iostream.h>
#include "Tdate.h"
void Tdate::Set(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
int Tdate::IsLeapYear()
{
return((year%4==0&&year%100!=0)||(year%400==0));
}
void Tdate::Print()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
///////////////////////////////////////
//
// filename: Test.cpp
//
//////////////////////////////////////
#include <iostream.h>
#include "Tdate.h"
void SomeFunc(Tdate *ps)
{
ps->Print();
if(ps->IsLeapYear())
cout<<"A leap year.\n";
else cout<<"not aleap year.\n";
}
int main(void)
{
Tdate s;
s.Set(2,15,1998);
return 0;
}
网友回答:
g++ -o test test.cpp Tdate.cpp
# g++ Test.cpp Tdate.cpp
# g++ ch11_2.cpp -o ch11_2 Tdate.cpp
g++ test.cpp Tdate.cpp -o test
如果要分别检查的话
g++ -c Tdate.cpp
g++ -c test.cpp
这样只编译不连接
g++ -c Tdate.cpp -o libTdate.o
ar rcs libTdate.a libTdate.o
cp libTdate.a /lib
g++ test.cpp -o test -lTdate
这应该是你想要的结果吧。
把头文件里面的#include <iostream.h>改成:
#include <iostream>
using namespace std;
然后先编译TDate.cpp: g++ -c TDate.cpp生成TDate.o
再编译test.cpp: g++ -c test.cpp生成test.o
最后连接:g++ -o test test.o TDate.o生成test可执行文件.