谢谢了
还有一个问题
定义一个类保存为1.h
#ifdef MONEY_H
#define MONEY_H
class Money
{
public:
Money();
Money(long doller,int cent);
Money(double amout);
void assignMoney(long doller,int cent);
void assignMoney(double amount);
void addTo(Money cash);
void muitiplyBy(double num);
double quot(Money cash);
bool lessthan(Money cash),equalto(Money cash);
void display();
private:
long dollerandcent;
long round(double num);
};
#endif
能否被2.h引用
#include <iostream>
using namespace std;
#include "1.h"
void Money::Money()
{
dollerandcent=0;
}
void Money::Money(long doller,int cent)
{dollerandcent=doller*100+cent;
}
void Money::Money(double amount)
{ dollerandcent=round(amount*100);
}
void Money:: assignMoney(long doller,int cent)
{dollerandcent=doller*100+cent;
}
void Money::assignMoney(double amount)
{ dollerandcent=round(amount*100);
}
void Money::addTo(Money cash)
{dollerandcent=dollerandcent+cash.dollerandcent;
}
void Money::muitiplyBy(double num)
{dollerandcent=round(amount*100);
}
double Money::quot(Money cash){
return static_cast<double>(dollerandcent)/cash.dollerandcent;
}
bool Money::lessthan(Money cash)
{bool ret;
if(dollerandcent<cash.dollerandcent)
ret=true;
else
ret=false;
return ret;
}
bool Money::equalto(Money cash)
{bool ret;
if(dollerandcent==cash.dollerandcent)
ret=true;
else
ret=false;
return ret;
}
void Money::display()
{int cent;
cout<<dollerandcent/100<<".";
cent=dollerandcent%100;
if (cent<10)
cout<<"0";
cout<<cent;
}
long Money::round(double num)
{long ron;
if (num>=0)
ron=static_cast<long>(num+0.5);
else
ron=static_cast<long>(num-0.5);
return ron;
}
也就是把函数声明和函数定义放在两个文件中。可以吗