I have only been programming for a about 5 months, 2 months in C++ and this is my first attempt at a class and I'm probably just running into some serious newbie errors but the web tutorials are very confusing.
#ifndef RATIONAL_
#define RATIONAL_
usingnamespace std;
#include <iostream>
class Rational
{
public:
// Construct rational from numerator and denominator
//
Rational( int = 0, int = 1 );
// Construct rational by copying existing rational
//
Rational( const Rational& );
// Assign into rational by copying existing rational
//
Rational& operator=( const Rational& );
//Find Greatest Common Divisor
int GCD();
//Simplify the rational number to its smallest form
void Simplify();
// Return true if rational is valid (non-zero denominator)
//
bool IsValid() const;
// Return value of numerator
//
int Numerator() const;
// Return value of denominator
//
int Denominator() const;
// Input/Output operations
//
friend istream& operator>>( istream&, Rational& );
friend ostream& operator<<( ostream&, const Rational& );
private:
// Insert your declarations here
int Numerator_;
int Denominator_;
};
// Comparative operations
//
booloperator==( const Rational&, const Rational& );
booloperator!=( const Rational&, const Rational& );
booloperator< ( const Rational&, const Rational& );
booloperator<=( const Rational&, const Rational& );
booloperator> ( const Rational&, const Rational& );
booloperator>=( const Rational&, const Rational& );
// Arithmetic operations
//
Rational operator+( const Rational&, const Rational& );
Rational operator-( const Rational&, const Rational& );
Rational operator*( const Rational&, const Rational& );
Rational operator/( const Rational&, const Rational& );
#endif
I keep fixing the first error i recieve then another 10 pop up... My code will be on the next post, and if anyone has any ideas on overloading the operator>> that would be helpful im lost on that one...
/*-----------------------------------------------------------------------------
Name: constructor for class "Rational"
Purpose: Initialize the contents of a rational number
Receive: The numerator and denominator or rational number
-----------------------------------------------------------------------------*/
Rational::Rational( int Num, int Den )
{
Numerator_ = Num;
if (Den <= 0)
{
Denominator_ = 1;
}
else
{
Denominator_ = Den;
}
}
/*-----------------------------------------------------------------------------
Name: copy constructor for class "Rational"
Purpose: Initialize the value and scale inside a Rational object
Recieve: The rational to be copied
-----------------------------------------------------------------------------*/
Rational::Rational( const Rational& C )
{
Numerator_ = C.Numerator_;
Denominator_ = C.Denominator_;
}
/*-----------------------------------------------------------------------------
Name: assignment operator for class "Rational"
Purpose: Copy the contents of one rational to another
Recieve: The rational which is to be copied
Return: The rational number
-----------------------------------------------------------------------------*/
Rational& Rational::operator=(const Rational& C )
{
Numerator_ = C.Numerator_;
Denominator_ = C.Denominator_;
return *this;
}
/*-----------------------------------------------------------------------------
Name: GCD
Purpose: Find the greatest common divisor of a rational number
Return: GCD
-----------------------------------------------------------------------------*/
int Rational::GCD()
{
int A = Numerator_;
int B = Denominator_;
int Temp;
while(B)
{
Temp = B;
B = A%B;
A = Temp;
}
return A;
}
/*-----------------------------------------------------------------------------
Name: Simplify
Purpose: Simplify a rational number
-----------------------------------------------------------------------------*/
void Rational::Simplify()
{
int GCDNumber = GCD();
if(GCDNumber != 0)
{
Numerator_ = Numerator_/GCDNumber;
Denominator_ = Denominator_/GCDNumber;
}
}
/*-----------------------------------------------------------------------------
Name: IsValid
Purpose: Test whether or not the rational is valid
Return: The Boolean value for the result of the test
-----------------------------------------------------------------------------*/
bool Rational::IsValid() const
{
return ( Denominator_ != 0 );
}
/*-----------------------------------------------------------------------------
Name: Numerator
Purpose: extract numerator from rational number
Return: numerator
-----------------------------------------------------------------------------*/
int Rational::Numerator() const
{
return Numerator_;
}
/*-----------------------------------------------------------------------------
Name: Denominator
Purpose: extract denominator from rational number
Return: denominator
-----------------------------------------------------------------------------*/
int Rational::Denominator() const
{
return Denominator_;
}
/*-----------------------------------------------------------------------------
Name: operator==
Purpose: compare 2 rationals for equality
Recieve: the 2 rationals
Return: boolean result of the comparison
-----------------------------------------------------------------------------*/
booloperator==( const Rational& A, const Rational& B )
{
A.Simplify();
B.Simplify();
return(A.Numerator() == B.Numerator() and A.Denominator()== B.Denominator());
}
/*-----------------------------------------------------------------------------
Name: operator!=
Purpose: compare 2 rationals for inequality
Recieve: the 2 rationals
Return: boolean result of the comparison
-----------------------------------------------------------------------------*/
booloperator!=( const Rational& A, const Rational& B )
{
A.Simplify();
B.Simplify();
return(A.Numerator() != B.Numerator() and A.Denominator()!= B.Denominator());
}
/*-----------------------------------------------------------------------------
Name: operator<
Purpose: tell whether A is less than B
Recieve: the 2 rationals A and B
Return: boolean result of the comparison
-----------------------------------------------------------------------------*/
booloperator<( const Rational& A, const Rational& B )
{
A.Simplify();
B.Simplify();
return(A.Numerator()*B.Denominator())<(A.Denominator()*B.Numerator());
}
/*-----------------------------------------------------------------------------
Name: operator<=
Purpose: tell whether A is less than or equal to B
Recieve: the 2 rationals A and B
Return: boolean result of the comparison
-----------------------------------------------------------------------------*/
booloperator<=( const Rational& A, const Rational& B )
{
A.Simplify();
B.Simplify();
return(A.Numerator()*B.Denominator())<=(A.Denominator()*B.Numerator());
}
/*-----------------------------------------------------------------------------
Name: operator>
Purpose: tell whether A is greater than B
Recieve: the 2 rationals A and B
Return: boolean result of the comparison
-----------------------------------------------------------------------------*/
booloperator>( const Rational& A, const Rational& B )
{
A.Simplify();
B.Simplify();
return(A.Numerator()*B.Denominator())>(A.Denominator()*B.Numerator());
}
/*-----------------------------------------------------------------------------
Name: operator>=
Purpose: tell whether A is greater than or equal to B
Recieve: the 2 rationals A and B
Return: boolean result of the comparison
-----------------------------------------------------------------------------*/
booloperator>=( const Rational& A, const Rational& B )
{
A.Simplify();
B.Simplify();
return(A.Numerator()*B.Denominator())>=(A.Denominator()*B.Numerator());
}
/*-----------------------------------------------------------------------------
Name: operator+
Purpose: Add two rational numbers
Recieve: The two rationals to be added
Return: The rational result of adding the two rationals
-----------------------------------------------------------------------------*/
Rational operator+( const Rational& A, const Rational B )
{
int Num;
int Den;
Num = A.Numerator()*B.Denominator()+A.Denominator()*B.Numerator();
Den = A.Denominator()*B.Denominator();
Rational C(Num, Den);
return C.Simplify();
}
/*-----------------------------------------------------------------------------
Name: operator*
Purpose: Multiply two rational numbers
Recieve: The two rationals to be multiplied
Return: The rational result of multiplying the two rationals
-----------------------------------------------------------------------------*/
Rational operator*( const Rational& A, const Rational B )
{
int Num;
int Den;
Num = A.Numerator()*B.Numerator();
Den = A.Denominator()*B.Denominator();
Rational C(Num, Den);
return C.Simplify();
}
proj06.driver.cpp: In function 'int main()':
proj06.driver.cpp:38: warning: the address of 'Rational G()', will always evaluate as 'true'
proj06.rational.cpp: In function 'bool operator==(const Rational&, const Rational&)':
proj06.rational.cpp:150: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:151: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator!=(const Rational&, const Rational&)':
proj06.rational.cpp:167: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:168: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator<(const Rational&, const Rational&)':
proj06.rational.cpp:183: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:184: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator<=(const Rational&, const Rational&)':
proj06.rational.cpp:199: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:200: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator>(const Rational&, const Rational&)':
proj06.rational.cpp:216: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:217: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator>=(const Rational&, const Rational&)':
proj06.rational.cpp:232: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:233: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'Rational operator+(const Rational&, Rational)':
proj06.rational.cpp:255: error: conversion from 'void' to non-scalar type 'Rational' requested
proj06.rational.cpp: In function 'Rational operator-(const Rational&, Rational)':
proj06.rational.cpp:276: error: conversion from 'void' to non-scalar type 'Rational' requested
proj06.rational.cpp: In function 'Rational operator*(const Rational&, Rational)':
proj06.rational.cpp:296: error: conversion from 'void' to non-scalar type 'Rational' requested
proj06.rational.cpp: In function 'Rational operator/(const Rational&, Rational)':
proj06.rational.cpp:316: error: conversion from 'void' to non-scalar type 'Rational' requested