Clear to me

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");
}
what this code will do
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
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.
You did not cahnge anything in the code. All errors are still here.

As for program I alreaty told:
it is intended to implement runtime rational arithmetics
THis concrete program will output reslult of calculation 23/11 + 2/3
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");
}

It is simple. Fraction class represents Rational Number
http://en.wikipedia.org/wiki/Rational_number

Default constructor creates representation oj fraction 0/1 (0)
Constructor with two arguments num and den creates representaion num/den
It replaces denomenator 0 with 1 (a bad thing, it should throw an exception)
And it makes sure that denominator is positive: -1/-2 == 1/2, 2/-3 == -2/3
Also it reduces resulting fraction: http://en.wikipedia.org/wiki/Reduction_%28mathematics%29
http://en.wikipedia.org/wiki/Greatest_common_divisor#Reducing_fractions

Operator + perform addition of two fractions using direct approach: http://en.wikipedia.org/wiki/Rational_number#Addition

gcf() implements greatest common factor http://en.wikipedia.org/wiki/Greatest_common_divisor
using euclids algorithm http://en.wikipedia.org/wiki/Greatest_common_divisor#Using_Euclid.27s_algorithm
How does this works ?

first = labs( first );
second = labs( second );
What "second.denominator" will do ?

Thanks.
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.
Got it.

+ I have just started C++ programming. How I can become a good programmer ?

Thanks.
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.
Ok, I'll. Thanks.
When I need to ask any thing can I ?
Yeah, sure! That's what this forum is for - so that beginners can ask questions and get help :)
It would help if you used CODE TAGS format when you post. Select you code and click the <> button on the right of the edit box.
Learning about proper indentation is also a good way to get better help since it makes code easier to understand quickly. See this Wiki: http://en.wikipedia.org/wiki/Indent_style

Here is your code in tags:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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");
}
Thanks :)
Topic archived. No new replies allowed.