请高手帮帮忙看看小弟的程序
#ifndef NUMBER_H
#define NUMBER_H
class Number;
class Number
{
private:
double num;
public:
Number();
Number(double);
~Number();
void setNumber(double);
double getNumber() const;
Number operator = (const Number &);
Number operator + (const Number &);
Number operator - (const Number &);
Number operator * (const Number &);
Number operator / (const Number &);
friend ostream &operator << (ostream &, const Number &);
friend istream &operator >> (istream &, Number &);
};
#endif
#include "Number.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Number;
Number::Number()
{
num=0.0;
}
Number::Number(double n)
{
num=n;
}
Number::~Number()
{
// To destroy the memory in this program.
}
void Number::setNumber(double n)
{
num=n;
}
double Number::getNumber() const
{
return num;
}
Number Number::operator + (const Number &right)
{
Number third;
third.num=num+right.num;
return third;
}
Number Number::operator - (const Number &right)
{
Number third;
third.num=num-right.num;
return third;
}
Number Number::operator = (const Number &right)
{
num=right.num;
return num;
}
Number Number::operator * (const Number &right)
{
Number third;
third.num=num*right.num;
return third;
}
Number Number::operator / (const Number &right)
{
Number third;
third.num=num/right.num;
return third;
}
/*ostream &operator << (ostream &strm, const Number &obj)
{
strm << obj.num;
return strm;
}
*/
istream &operator >> (istream &strm, Number &obj)
{
strm >> obj.num;
return strm;
}
#include <iostream>
#include <conio.h>
#include "Number.h"
using namespace std;
class Number;
int main()
{
Number box1, box2, box3, box4, box5, box6, box7;
cout << "Enter box1 >> ";
cin >> box1;
cout << "Enter box2 >> ";
cin >> box2;
box3=box1+box2;
box4=box1-box2;
box5=box4;
box6=box1*box2;
box7=box1/box2;
cout << endl;
cout << "box3 = box1 + box2 = " << box3 << endl;
cout << "box4 = box1 - box2 = " << box4 << endl;
cout << "box5 = box4 = " << box5 << endl;
cout << "box6 = box1 * box2 = " << box6 << endl;
cout << "box7 = box1 / box2 = " << box7 << endl;
getch();
return 0;
}
小弟的程序不知为何不能运行,请高手帮帮忙下~ 注:小弟是新手~
[解决办法]
using namespace std;
class Number;//这行去掉试试
[解决办法]
这样改就OK了
#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
#include <conio.h>
using namespace std;
class Number
{
private:
double num;
public:
Number();
Number(double);
~Number();
void setNumber(double);
double getNumber() const;
Number operator = (const Number &);
Number operator + (const Number &);
Number operator - (const Number &);
Number operator * (const Number &);
Number operator / (const Number &);
friend ostream &operator << (ostream &, const Number &);
friend istream &operator >> (istream &, Number &);
};
#endif
#include "Number.h"
Number::Number()
{
num=0.0;
}
Number::Number(double n)
{
num=n;
}
Number::~Number()
{
// To destroy the memory in this program.
}
void Number::setNumber(double n)
{
num=n;
}
double Number::getNumber() const
{
return num;
}
Number Number::operator + (const Number &right)
{
Number third;
third.num=num+right.num;
return third;
}
Number Number::operator - (const Number &right)
{
Number third;
third.num=num-right.num;
return third;
}
Number Number::operator = (const Number &right)
{
num=right.num;
return num;
}
Number Number::operator * (const Number &right)
{
Number third;
third.num=num*right.num;
return third;
}
Number Number::operator / (const Number &right)
{
Number third;
third.num=num/right.num;
return third;
}
ostream &operator << (ostream &strm, const Number &obj)
{
strm << obj.num;
return strm;
}
istream &operator >> (istream &strm, Number &obj)
{
strm >> obj.num;
return strm;
}
#include "Number.h"
int main()
{
Number box1, box2, box3, box4, box5, box6, box7;
cout << "Enter box1 >> ";
cin >> box1;
cout << "Enter box2 >> ";
cin >> box2;
box3=box1+box2;
box4=box1-box2;
box5=box4;
box6=box1*box2;
box7=box1/box2;
cout << endl;
cout << "box3 = box1 + box2 = " << box3 << endl;
cout << "box4 = box1 - box2 = " << box4 << endl;
cout << "box5 = box4 = " << box5 << endl;
cout << "box6 = box1 * box2 = " << box6 << endl;
cout << "box7 = box1 / box2 = " << box7 << endl;
getch();
return 0;
}