I am attempting to write a program that defines a class Money which contains two integer data fields, dollar and cents. And then Overload four arithmetic operators: +, -, *, /.
Money.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#ifndef MONEY_H
#define MONEY_H
using namespace std;
class Money
{
public:
Money();
Money(int d, int c);
Money (int some);
Money operator-() const;
Money operator+(const Money& b) const; //constructer for addition
Money operator-(const Money& b) const; //constructor for subtraction
Money operator*(double m) const; //constructor for multiply
Money operator/(double d) const; //constructor for division
bool operator==(const Money& b) const;
void print() const;
private:
int dollars;
int cents;
};
#endif
|
Money.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include "Money.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
Money::Money()
{
}
Money Money::operator+(const Money& b2) const
{
int total = (dollars * 100 + cents) +
(b2.dollars * 100 + b2.cents);
Money local(total);
return local;
}
Money Money::operator-(const Money& b2) const
{
int diff = (dollars * 100 + cents) -
(b2.dollars * 100 + b2.cents);
Money local(diff);
return local;
}
Money Money::operator-() const
{
int neg = - (dollars * 100 + cents);
Money local(neg);
return local;
}
bool Money::operator==(const Money& b2) const
{
int thistotal = (dollars * 100 + cents);
int b2total = (b2.dollars * 100 + b2.cents);
return (thistotal == b2total);
}
|
main.cpp
1 2 3 4 5 6 7 8 9
|
#include <iostream>
#include "Money.h"
using namespace std;
int main() {
return 0;
}
|
I haven't done anything with main.cpp because I keep receiving an error when I attempt to compile:
1>------ Build started: Project: prog05, Configuration: Debug Win32 ------
1>Build started 6/19/2012 11:40:02 PM.
1>InitializeBuildStatus:
1> Touching "Debug\prog05.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1> Money.cpp
1> Generating Code...
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>Money.obj : error LNK2019: unresolved external symbol "public: __thiscall Money::Money(int)" (??0Money@@QAE@H@Z) referenced in function "public: class Money __thiscall Money::operator-(void)const " (??GMoney@@QBE?AV0@XZ)
1>C:\Users\ryan\Documents\Visual Studio 11\Projects\Programming II\prog05\Debug\prog05.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.12
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========