|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Operator Overloading (I think...?)
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
My assignment is to use the provided private data and member functions and implement them and then test them in some main. Honestly, my understanding of using classes isn't all that great anyway.
I was lost form the very start.Code:
//Rational.h
#ifndef RATIONAL_H_DEF
#define RATIONAL_H_DEF
#include <iostream>
using namespace std;
class Rational
{
long _p;
long _q;
public:
Rational ( ); //default constructor
Rational ( long, long = 1 ); //constructor
Rational ( const Rational& ); //copy constructor
Rational& operator= ( const Rational& );
Rational& operator+= ( const Rational& );
Rational& operator-= ( const Rational& );
Rational& operator*= ( const Rational& );
Rational& operator/= ( const Rational& );
};
#endif
Code:
//Rational.cpp
#include <iostream>
#include "Rational.h"
Rational::Rational ()
{
_p = 1;
_q = 1;
}
Rational& operator+= (const Rational& R)
{
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
//pasted from a previous assignment; not quite sure it works
}
__________________
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||||||||||
|
4.0 GHz
![]() |
You have:
Quote:
Quote:
Quote:
Quote:
The "*" in the return statement is a consequence of "this" actually being a pointer, which for this discussion we need not jump into those dark waters. This setup with operators allows you to chain them together, like so: Quote:
Quote:
The equivalent, non-operator, non-object statement would be: Quote:
Edit: for clarity
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
Last edited by dharmaBum : 10-12-09 at 08:54 PM |
|||||||||||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
Thanks.
My assignment has me implementing the member functions in a separate .cpp, but, out of curiosity, can I do it right where I define my class? Code:
//Rational.h
#ifndef RATIONAL_H_DEF
#define RATIONAL_H_DEF
#include <iostream>
using namespace std;
class Rational
{
long _p;
long _q;
public:
Rational ( ); //default constructor
Rational ( long, long = 1 ); //constructor
Rational ( const Rational& ); //copy constructor
Rational& operator= ( const Rational& );
Rational& operator+= ( const Rational& );
const{
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
return *this;
}
Rational& operator-= ( const Rational& );
Rational& operator*= ( const Rational& );
Rational& operator/= ( const Rational& );
friend ostream& operator<< ( ostream&, const Rational&);
friend istream& operator>> ( istream&, Rational&);
};
#endif
__________________
Last edited by stn0092 : 10-13-09 at 03:06 AM |
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||||
|
4.0 GHz
![]() |
Quote:
You have: Code:
Rational& operator+= ( const Rational& );
const{
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
return *this;
}
Instead, what you can do is simply use the normal syntax of function definitions: Code:
class Rational{
...
public:
...
Rational& operator+= ( const Rational& ){
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
return *this;
}
...
};
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
|||||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
I need some more help.
![]() I'm trying to implement operator+ using operator+=. Code:
//Rational.h
#ifndef RATIONAL_H_DEF
#define RATIONAL_H_DEF
#include <iostream>
using namespace std;
class Rational
{
long _p;
long _q;
public:
Rational ( ); //default constructor
Rational ( long, long = 1 ); //constructor
Rational ( const Rational& ); //copy constructor
Rational& operator= ( const Rational& );
Rational& operator+= ( const Rational& );
friend ostream& operator<< ( ostream&, const Rational&);
friend istream& operator>> ( istream&, Rational&);
Rational operator+ (const Rational& ) const;
Rational operator+ (long) const;
friend Rational operator+ (long, const Rational&);
};
#endif
Code:
//Rational.cpp
#include <iostream>
#include "Rational.h"
using namespace std;
Rational::Rational()
{
_p = 1;
_q = 1;
}
Rational::Rational ( long P, long Q )
{
_p = P;
_q = (Q == 0)? 1:Q;
}
Rational& Rational::operator= ( const Rational& R)
{
_p = R._p;
_q = R._q;
return *this;
}
Rational& Rational::operator+= ( const Rational& R)
{
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
return *this;
}
ostream& operator<< ( ostream& os, const Rational& R)
{
os << R._p << "/" << R._q;
return os;
}
istream& operator>> ( istream& is, Rational& R)
{
char c;
is >> R._p >> c >> R._q;
return is;
}
Rational operator+ (long L, const Rational& R)
{
Rational temp(L);
return temp+=R;
}
Code:
Compile errors Rational.o: In function `operator+(long, Rational const&)': Rational.cpp:(.text+0x274): undefined reference to `Rational::Rational(Rational const&)' Main.o: In function `main': Main.cpp:(.text+0x345): undefined reference to `Rational::operator+(Rational const&) const' collect2: ld returned 1 exit status make: *** [Main] Error 1 I was thinking I could just do return += R, but that didn't work (error: expected primary-expression before â+=â token). EDIT: I think I made some progress. In Rational.cpp, operator+ is now: Code:
Rational operator+ (long L, const Rational& R)
{
Rational temp(L);
Rational x;
x._p = temp._p += R._p;
x._q = temp._q += R._q;
return x;
}
Code:
#include <iostream>
#include "Rational.h"
using namespace std;
int main()
{
Rational rat1(1, 2);
Rational rat2(-20, 4);
Rational x;
//+
cout << rat1 << " + " << rat2 << " = ";
x = rat1+rat2;
cout << x << endl;
return 0;
}
COMPILE ERRORS
Main.cpp:(.text+0x345): undefined reference to `Rational::operator+(Rational const&) const'
collect2: ld returned 1 exit status
make: *** [Main] Error 1
__________________
Last edited by stn0092 : 10-14-09 at 03:29 AM |
|||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||
|
4.0 GHz
![]() |
Code:
#include <iostream>
#include "Rational.h"
using namespace std;
Rational::Rational()
{
_p = 1;
_q = 1;
}
Rational::Rational ( long P, long Q )
{
_p = P;
_q = (Q == 0)? 1:Q;
}
Rational& Rational::operator= ( const Rational& R)
{
_p = R._p;
_q = R._q;
return *this;
}
Rational& Rational::operator+= ( const Rational& R)
{
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
return *this;
}
ostream& operator<< ( ostream& os, const Rational& R)
{
os << R._p << "/" << R._q;
return os;
}
istream& operator>> ( istream& is, Rational& R)
{
char c;
is >> R._p >> c >> R._q;
return is;
}
Rational& Rational::operator+ (const Rational& R) const
{
_p = 1;
_q = 1;
return *this;
}
Rational& Rational::operator+ (long R) const
{
_p = 1;
_q = 1;
return *this;
}
Rational operator+ (long L, const Rational& R)
{
Rational x(L);
return x += R;
}
Quote:
__________________
Last edited by stn0092 : 10-14-09 at 09:12 AM |
||||||||||||||
|
|
|
|
|
#9 (permalink) | |||||||||||||
|
4.0 GHz
![]() |
Okay. Everything compiles, but isn't functioning correctly. My + gives me an answer of -16/4 (reducing the fraction is not required in this assignment) instead of some value equivalent of -9/2 and I can't find what I did wrong (yet again...). My += does give me a correct equivalent value of -36/8. EDIT: I can see that I wrote Rational Rational::operator+ (const Rational& R)const incorrectly...what should I put in there instead? return R;? Code:
//Rational.h
#ifndef RATIONAL_H_DEF
#define RATIONAL_H_DEF
#include <iostream>
using namespace std;
class Rational
{
long _p;
long _q;
public:
Rational ( ); //default constructor
Rational ( long, long = 1 ); //constructor
Rational ( const Rational& ); //copy constructor
Rational& operator= ( const Rational& );
Rational& operator+= ( const Rational& );
Rational& operator-= ( const Rational& );
Rational& operator*= ( const Rational& );
Rational& operator/= ( const Rational& );
friend ostream& operator<< ( ostream&, const Rational&);
friend istream& operator>> ( istream&, Rational&);
Rational operator+ (const Rational&) const;
Rational operator+ (long) const;
friend Rational operator+ (long, const Rational&);
};
#endif
Code:
//Rational.cpp
#include <iostream>
#include "Rational.h"
using namespace std;
Rational::Rational()
{
_p = 1;
_q = 1;
}
Rational::Rational ( long P, long Q )
{
_p = P;
_q = (Q == 0)? 1:Q;
}
Rational::Rational ( const Rational& R)
{
_p = R._p;
_q = R._q;
}
Rational& Rational::operator= ( const Rational& R)
{
_p = R._p;
_q = R._q;
return *this;
}
Rational& Rational::operator+= ( const Rational& R)
{
_p = (_p * R._q) + (R._p * _q);
_q *= R._q;
return *this;
}
ostream& operator<< ( ostream& os, const Rational& R)
{
os << R._p << "/" << R._q;
return os;
}
istream& operator>> ( istream& is, Rational& R)
{
char c;
is >> R._p >> c >> R._q;
return is;
}
Rational Rational::operator+ (const Rational& R)const
{
Rational t;
t+=R;
return t;
}
Rational operator+ (long L, const Rational& R)
{
return Rational(L)+=(R);
}
Code:
//Main.cpp
#include <iostream>
#include "Rational.h"
using namespace std;
int main()
{
Rational rat1(1, 2);
Rational rat2(-20, 4);
Rational temp;
//+=
temp = rat1;
cout << rat1 << " += " << rat2 << " = ";
temp+=rat2;
cout << temp << endl;
//+
temp = rat1;
cout << rat1 << " + " << rat2 << " = ";
temp = (rat1)+(rat2);
cout << temp << endl;
return 0;
}
__________________
Last edited by stn0092 : 10-14-09 at 05:09 PM |
|||||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||||
|
4.0 GHz
![]() |
Code:
Rational Rational::operator+ (const Rational& R)const
{
Rational t;
t+=R;
return t;
}
Hint: What is being added to your input (-20/4) to give as output (-16/4)?
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
Last edited by dharmaBum : 10-14-09 at 05:31 PM |
||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|