- Mar 21, 2009
- 409
- 2
- 81
I am taking object orientated programming right now, and I already missed the second assignment. This idea of data objects and classes is not at all coming easy to me. The programming is supposed to be done in shell/ linux environment (debian I believe, hosted on a power server) but there no reason I can't test first in Dev C++ and create the appropriate makefile and .cpp files in linux.
It is a "rational" class. It takes two "long integers" and manipulates them like a rational number. This is the barebones here, I am just trying to get adding working. I need a private member function that "sends a message to the object" of what the greatest common denominator is of two objects of class rational and add them displaying sum in lowest terms (1/16 and 1/22 for example, needs to add them and reduce).
Anyway, here is my source code. It doesn't link correctly, so I don't know if there are any logic errors. My questions are below:
rational.h -header
rational.cpp - implementation
main.cpp
my attempt to build and compile it in dev C++ outputs these linker errors:
linker errors
Truthfully I have a really foggy understanding of the way a class works. I am quite comfortable with procedural, where ever data item goes to one place and I can follow the naming and the passing easy. I really don't understand at all how a class updates the object. My professor is having us follow Meyers in his book "effective C++". Any serious programmer should know this I guess. Help getting the template down for this much would be great, then I can start worrying about operator overloading and the rest of the class functionality.
Decent programmers, take pity on my please .
It is a "rational" class. It takes two "long integers" and manipulates them like a rational number. This is the barebones here, I am just trying to get adding working. I need a private member function that "sends a message to the object" of what the greatest common denominator is of two objects of class rational and add them displaying sum in lowest terms (1/16 and 1/22 for example, needs to add them and reduce).
Anyway, here is my source code. It doesn't link correctly, so I don't know if there are any logic errors. My questions are below:
rational.h -header
Code:
#ifndef _RATIONAL_H_
#define _RATIONAL_H_
#include <iostream>
using namespace std;
class Rational
{
long _p;
long _q;
void gcm (const Rational&);
public:
Rational ( long = 0, long = 1 );
void add(const Rational&);
//void sub(const Rational&);
//void mult(const Rational&);
//void div(const Rational&);
void display() const;
};
#endif
rational.cpp - implementation
Code:
/*
Your name here
*/
#include "rational.h"
Rational::Rational ( long p, long q )
{
_p = p;
_q = q;
}
void Rational::gcm(const Rational& n)
{
long greater;
long least;
long temp;
if (_p < _q)
{
greater = _p;
least = _q;
}
else
{
greater = _q;
least = _p;
}
while (least!=0)
{
greater -= least;
if (greater < least)
{
temp = greater;
greater = least;
least = temp;
}
}
_p /= greater;
_q /= greater;
}
void Rational::add(const Rational& n)
{
int lcm;
_p = (n._p * _q) + (_p * n._q);
_q *= n._q;
}
void Rational::display() const
{
cout << _p <<"/" << _q << endl;
}
main.cpp
Code:
#include <iostream>
#include "rational.h"
#include "rational.cpp"
using namespace std;
int main()
{
Rational num(3,4);
Rational num2(3,4);
num.add(num2);
num.display();
system("PAUSE");
return EXIT_SUCCESS;
}
my attempt to build and compile it in dev C++ outputs these linker errors:
linker errors
Code:
multiple definition of `Rational::Rational(long, long)'
first defined here
multiple definition of `Rational::Rational(long, long)'
first defined here
multiple definition of `Rational::gcm(Rational const&)'
first defined here
multiple definition of `Rational::add(Rational const&)'
first defined here
multiple definition of `Rational::display() const'
first defined here
ld returned 1 exit status
C:\Dev-Cpp\Makefile.win [Build Error] [Project1.exe] Error 1
Truthfully I have a really foggy understanding of the way a class works. I am quite comfortable with procedural, where ever data item goes to one place and I can follow the naming and the passing easy. I really don't understand at all how a class updates the object. My professor is having us follow Meyers in his book "effective C++". Any serious programmer should know this I guess. Help getting the template down for this much would be great, then I can start worrying about operator overloading and the rest of the class functionality.
Decent programmers, take pity on my please .