Feb 13, 2015 at 8:13am UTC
Please explain what this code will do, why we use it and where we can apply it ?
Thanks.
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
// class definition
class Fraction
{
public:
Fraction();
Fraction( long num, long den );
void display() const;
Fraction operator+( const Fraction &second ) const;
private:
static long gcf( long first, long second );
long numerator, denominator;
};
// ----------- Default constructor
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
// ----------- Constructor
Fraction::Fraction( long num, long den )
{
int factor;
if( den == 0 )
den = 1;
numerator = num;
denominator = den;
if( den < 0 )
{
numerator = -numerator;
denominator = -denominator;
}
factor = gcf( num, den );
if( factor > 1 )
{
numerator /= factor;
denominator /= factor;
}
}
// ----------- Function to print a Fraction
void Fraction::display() const
{
cout << numerator << '/' << denominator;
}
// ----------- Overloaded + operator
Fraction Fraction::operator+( const Fraction &second ) const
{
long factor, mult1, mult2;
factor = gcf( denominator, second.denominator );
mult1 = denominator / factor;
mult2 = second.denominator / factor;
return Fraction( numerator * mult2 + second.numerator * mult1,
denominator * mult2 );
}
// ----------- Greatest common factor
// computed using iterative version of Euclid's algorithm
long Fraction::gcf( long first, long second )
{
int temp;
first = labs( first );
second = labs( second );
while( second > 0 )
{
temp = first % second;
first = second;
second = temp;
}
return first;
}
//main program
void main()
{
Fraction a, b( 23, 11 ), c( 2, 3 );
a = b + c;
a.display();
cout << '\n';
system("pause");
}
Feb 13, 2015 at 8:18am UTC
Not compile.
#include <iostream.h>
there is no
iostream.h in standard C++
void main()
is illegal in C and C++
As for class, it is intended to implement runtime rational arithmetics, like compile time stnadard
http://en.cppreference.com/w/cpp/numeric/ratio
Last edited on Feb 13, 2015 at 8:20am UTC
Feb 13, 2015 at 8:37am UTC
Sorry, I have corrected, now tell me, what this program will do ?
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
// class definition
class Fraction
{
public:
Fraction();
Fraction( long num, long den );
void display() const;
Fraction operator+( const Fraction &second ) const;
private:
static long gcf( long first, long second );
long numerator, denominator;
};
// ----------- Default constructor
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
// ----------- Constructor
Fraction::Fraction( long num, long den )
{
int factor;
if( den == 0 )
den = 1;
numerator = num;
denominator = den;
if( den < 0 )
{
numerator = -numerator;
denominator = -denominator;
}
factor = gcf( num, den );
if( factor > 1 )
{
numerator /= factor;
denominator /= factor;
}
}
// ----------- Function to print a Fraction
void Fraction::display() const
{
cout << numerator << '/' << denominator;
}
// ----------- Overloaded + operator
Fraction Fraction::operator+( const Fraction &second ) const
{
long factor, mult1, mult2;
factor = gcf( denominator, second.denominator );
mult1 = denominator / factor;
mult2 = second.denominator / factor;
return Fraction( numerator * mult2 + second.numerator * mult1,
denominator * mult2 );
}
// ----------- Greatest common factor
// computed using iterative version of Euclid's algorithm
long Fraction::gcf( long first, long second )
{
int temp;
first = labs( first );
second = labs( second );
while( second > 0 )
{
temp = first % second;
first = second;
second = temp;
}
return first;
}
//main program
void main()
{
Fraction a, b( 23, 11 ), c( 2, 3 );
a = b + c;
a.display();
cout << '\n';
system("pause");
}
Thanks.
Feb 13, 2015 at 11:32am UTC
Now I have fixed errors and running. But I can't understand its logic, please explain the work of each function, constructor.
Thanks.
#include <iostream>
using namespace std;
// class definition
class Fraction
{
public:
Fraction();
Fraction( long num, long den );
void display() const;
Fraction operator+( const Fraction &second ) const;
private:
static long gcf( long first, long second );
long numerator, denominator;
};
// ----------- Default constructor
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
// ----------- Constructor
Fraction::Fraction( long num, long den )
{
int factor;
if( den == 0 )
den = 1;
numerator = num;
denominator = den;
if( den < 0 )
{
numerator = -numerator;
denominator = -denominator;
}
factor = gcf( num, den );
if( factor > 1 )
{
numerator /= factor;
denominator /= factor;
}
}
// ----------- Function to print a Fraction
void Fraction::display() const
{
cout << numerator << '/' << denominator;
}
// ----------- Overloaded + operator
Fraction Fraction::operator+( const Fraction &second ) const
{
long factor, mult1, mult2;
factor = gcf( denominator, second.denominator );
mult1 = denominator / factor;
mult2 = second.denominator / factor;
return Fraction( numerator * mult2 + second.numerator * mult1,
denominator * mult2 );
}
// ----------- Greatest common factor
// computed using iterative version of Euclid's algorithm
long Fraction::gcf( long first, long second )
{
int temp;
first = labs( first );
second = labs( second );
while( second > 0 )
{
temp = first % second;
first = second;
second = temp;
}
return first;
}
//main program
int main()
{
Fraction a, b( 23, 11 ), c( 2, 3 );
a = b + c;
a.display();
cout << '\n';
system("pause");
}
Feb 13, 2015 at 12:06pm UTC
How does this works ?
first = labs( first );
second = labs( second );
Feb 13, 2015 at 12:15pm UTC
What "second.denominator" will do ?
Thanks.
Feb 13, 2015 at 12:18pm UTC
It's the denominator member of the object called second .
I don't mean to be rude, but this is fairly fundamental C++ syntax stuff. It seems to me that you'd be much better off doing some more learning, and get a better understanding of the basics, rather than just trying to read a bit of code and ask questions about every bit you don't understand.
Feb 13, 2015 at 12:28pm UTC
Got it.
+ I have just started C++ programming. How I can become a good programmer ?
Thanks.
Feb 13, 2015 at 12:33pm UTC
Just start by working through a good textbook, that will give you some exercises to do. There are also some tutorials on this site that are worth looking through.
Once you have a basic command of the syntax, then more practice and more reading will give you a deeper understanding of good ways to design and write your code.
Feb 13, 2015 at 12:37pm UTC
When I need to ask any thing can I ?
Feb 13, 2015 at 1:40pm UTC
Yeah, sure! That's what this forum is for - so that beginners can ask questions and get help :)