By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

How can I adding two class object into a variable...?

P: n/a
the requirement is :
Create a class called Rational (rational.h) for performing arithmetic with
fractions. Write a program to test your class.

Use Integer variables to represent the private data of the class the
numerator and the denominator. Provide a constructor that enables an
object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form. For example, the
fraction 2/4 should be stored in the object as 1 in the numerator and 2 in
the denominator. Provide public member functions that perform each of the
following tasks:

a) Adding two Rational numbers. The result should be stored in reduced
form.
b) Subtracting two Rational numbers. The result should be stored in
reduced form.
c) Multiplying two Rational numbers. The result should be stored in
reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced
form.
e) Printing Rational numbers in the form a/b, where a is numerator and b
is denominator.
f) Printing Rational numbers in floating-point format.
I have writen the code like the following:

#ifndef RATIONAL_H
#define RATIONAL_H

class rational {
public :
rational();
void get_f1(int, int);
void get_f2(int, int);
void add(rational&, int, int, int );
private :
int numer;
int denom;
int result;
int result_n;
int result_d;
};

#endif

#include <iostream>
#include "rational.h"

using namespace std;

rational :: rational() {numer = denom = result = result_n = result_d = 0
;}

void rational :: get_f1(int numer, int denom)
{
do {
cout << "please input the first fraction's numerator: " ;
cin >> numer;
cout << "please input the first fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
}

void rational :: get_f2(int numer, int denom)
{
do {
cout << "please input the second fraction's numerator: " ;
cin >> numer;
cout << "please input the second fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
}

void rational :: add(rational &rational_1, int result_n, int result_d, int
result)
{
result_n = (rational_1.get_f1.numer * rational_1.get_f2.denom) +
(rational_1.get_f2.numer * rational_1.get_f1.denom); /* here i want to add
the two fractions, but i don't know how since my teach ask me to use two
class object to implement the addition */
result_d = rational_1.get_f1.denom * rational_1.get_f2.denom;
cout << rational_1.get_f1.numer <<result_d;

if (result_n==result_d)result=1;
for (int n=1 ; n <= result_d; n++)
{
for (int t=2; t <= result_n; t++)
{
if (result_n % t == 0 && result_d % t == 0)
{
result_n = result_n/t;
result_d = result_d/t;
}
}
}
if (result_d ==1)
cout <<endl <<result_n <<endl;
else if (result_d == 0)
cout <<endl << 0;
else
cout <<endl<< result_n << "/" << result_d <<endl;
}
#include <iostream>
#include "rational.h"

using namespace std;

int main ()
{
rational rational_1, rational_result;
int numer, denom, result, result_n, result_d;

rational_1.get_f1(numer, denom);
rational_1.get_f2(numer, denom);
rational_result.add(rational_1, result, result_n, result_d);
return 0;
}
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
surrealtrauma wrote:
the requirement is :
Create a class called Rational (rational.h) for performing arithmetic with
fractions. Write a program to test your class.

Use Integer variables to represent the private data of the class the
numerator and the denominator.
This requirement:
Provide a constructor that enables an
object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form. For example, the
fraction 2/4 should be stored in the object as 1 in the numerator and 2 in
the denominator.
has not been met. Nor have many others.
Provide public member functions that perform each of the
following tasks:

a) Adding two Rational numbers. The result should be stored in reduced
form.
~
b) Subtracting two Rational numbers. The result should be stored in
reduced form.
?
c) Multiplying two Rational numbers. The result should be stored in
reduced form.
?
d) Dividing two Rational numbers. The result should be stored in reduced
form.
?
e) Printing Rational numbers in the form a/b, where a is numerator and b
is denominator.
?
f) Printing Rational numbers in floating-point format.
?


I have writen the code like the following:

#ifndef RATIONAL_H
#define RATIONAL_H

class rational {
public :
rational();
Your constructor is supposed to have arguments with default values.
void get_f1(int, int);
What's that supposed to do?
void get_f2(int, int);
And that?
void add(rational&, int, int, int );
You're adding another 'rational' to "this", what are the extra three
ints for?


private :
int numer;
int denom;
int result;
int result_n;
int result_d;
};

#endif

#include <iostream>
#include "rational.h"

using namespace std;

rational :: rational() {numer = denom = result = result_n = result_d = 0
;}

void rational :: get_f1(int numer, int denom)
{
do {
cout << "please input the first fraction's numerator: " ;
cin >> numer;
cout << "please input the first fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
So, 'numer' and 'denom' variables here are updated from 'cin', but what
effect do they have on the 'rational' object for which you call that
member function? None, whatsoever.
}

void rational :: get_f2(int numer, int denom)
{
do {
cout << "please input the second fraction's numerator: " ;
cin >> numer;
cout << "please input the second fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
Same here.
}

void rational :: add(rational &rational_1, int result_n, int result_d, int
result)
{
result_n = (rational_1.get_f1.numer * rational_1.get_f2.denom) +
(rational_1.get_f2.numer * rational_1.get_f1.denom); /* here i want to add
the two fractions, but i don't know how since my teach ask me to use two
class object to implement the addition */
result_d = rational_1.get_f1.denom * rational_1.get_f2.denom;
cout << rational_1.get_f1.numer <<result_d;

if (result_n==result_d)result=1;
for (int n=1 ; n <= result_d; n++)
{
for (int t=2; t <= result_n; t++)
{
if (result_n % t == 0 && result_d % t == 0)
{
result_n = result_n/t;
result_d = result_d/t;
}
}
}
if (result_d ==1)
cout <<endl <<result_n <<endl;
else if (result_d == 0)
cout <<endl << 0;
else
cout <<endl<< result_n << "/" << result_d <<endl;
}
#include <iostream>
#include "rational.h"

using namespace std;

int main ()
{
rational rational_1, rational_result;
int numer, denom, result, result_n, result_d;

rational_1.get_f1(numer, denom);
rational_1.get_f2(numer, denom);
rational_result.add(rational_1, result, result_n, result_d);
return 0;
}


The 'rational' object should only _store_ numbers and perform some
arithmetic _operations_, but not _input_ them. The input should be
done by the test program.

I think you're missing the whole point of object oriented programming.
What is expected from your test program is to be able to do

#include "rational.h"

int main()
{
int a, b, c, d;
// input a, b, c, d, somehow. 'b' and 'd' should be != 0
rational r11(a, b), r21(c, c);
r11.add(r21); // will add 'r21' to 'r11' and store it in 'r11'
r11.print_ratio();
r11.print_decimal();
}

The same program should be OK if you replace 'add' with 'multiply' or
'subtract' or 'divide'.

V
Jul 22 '05 #2

P: n/a
surrealtrauma wrote:
the requirement is :
Create a class called Rational (rational.h) for performing arithmetic with
fractions. Write a program to test your class.
Thanks for posting the code. This shows that you've made a good
starting attempt and don't want other people to do your homework.

Use Integer variables to represent the private data of the class the
numerator and the denominator. Provide a constructor that enables an
object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form. For example, the
fraction 2/4 should be stored in the object as 1 in the numerator and 2 in
the denominator. Provide public member functions that perform each of the
following tasks:

a) Adding two Rational numbers. The result should be stored in reduced
form.
b) Subtracting two Rational numbers. The result should be stored in
reduced form.
c) Multiplying two Rational numbers. The result should be stored in
reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced
form.
e) Printing Rational numbers in the form a/b, where a is numerator and b
is denominator.
f) Printing Rational numbers in floating-point format.
I have writen the code like the following:

#ifndef RATIONAL_H
#define RATIONAL_H An alternative: H_RATIONAL.
By the way, you may want to use ".h" suffixes for header files
that can be translated by C or C++ and "*.hxx" or ".hpp" for
files that are C++ only. For simple projects, this doesn't
make much difference. But for larger projects that use C and
C++ files, then there is a big difference.

class rational {
public :
rational();
void get_f1(int, int);
void get_f2(int, int); Note:
Methods starting with "get" usually return some data member
of the class. Perhaps you may want to get more specific:
void get_f1_from_user(int, int);

Also, what does 'f1' mean?

void add(rational&, int, int, int ); The function parameters are not intuitive here.
What are you adding?
void add(ractional& fraction,
int numerator,
int denomenator,
int whole_number);



private :
int numer;
int denom;
int result;
int result_n;
int result_d;
}; Why are the "result" variables part of this class?

#endif You may want to identify this "endif":
#endif // RATIONAL_H
#include <iostream>
#include "rational.h"

using namespace std;

rational :: rational() {numer = denom = result = result_n = result_d = 0
;} You should use initialization lists:
rational ::
rational()
: numer(0), denom(0), result(0), result_n(0), result_d(0)
{
}


void rational :: get_f1(int numer, int denom) Do not use the same parameter names as member names.
Readers will get confused as to whether you are using the
parameter variable or the member variable.

If you want to load the member variables then don't use
parameters here. Otherwise, pass these by reference so
this method can change the values of the parameters.
{
do {
cout << "please input the first fraction's numerator: " ;
cin >> numer;
cout << "please input the first fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
} If you remove the parameters and the word "first" from the prompt,
you can use this method to input the fraction (first, second, third,
etc.) from the user without writing a second one.


void rational :: get_f2(int numer, int denom)
{
do {
cout << "please input the second fraction's numerator: " ;
cin >> numer;
cout << "please input the second fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
} The above method is not necessary. Just use the previous one.

void rational :: add(rational &rational_1, int result_n, int result_d, int
result)
{ [snip] } In order to add fractions, they must have equivalent denomenators.
If they don't, then you have to come up with a least common denomenator.

rational&
rational ::
add(int whole_number)
{
numer = numer + denom * whole_number;
return *this;
}



#include <iostream>
#include "rational.h"

using namespace std;

int main ()
{
rational rational_1, rational_result;
int numer, denom, result, result_n, result_d;

rational_1.get_f1(numer, denom);
rational_1.get_f2(numer, denom);
rational_result.add(rational_1, result, result_n, result_d);
return 0;
}


Your instructor probably wants you to be able to do something
like this in your main program:
rational fraction_a;
rational fraction_b;
rational fraction_result;

fraction_a.get_from_user();
fraction_b.get_from_user();
result = fraction_a.add(fraction_b);
result.display();
return EXIT_SUCCESS;

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

surrealtrauma wrote:
the requirement is :
Create a class called Rational (rational.h) for performing arithmetic with
fractions. Write a program to test your class.

Use Integer variables to represent the private data of the class the
numerator and the denominator. Provide a constructor that enables an
object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form. For example, the
fraction 2/4 should be stored in the object as 1 in the numerator and 2 in
the denominator. Provide public member functions that perform each of the
following tasks:

a) Adding two Rational numbers. The result should be stored in reduced
form.
b) Subtracting two Rational numbers. The result should be stored in
reduced form.
c) Multiplying two Rational numbers. The result should be stored in
reduced form.
d) Dividing two Rational numbers. The result should be stored in reduced
form.
e) Printing Rational numbers in the form a/b, where a is numerator and b
is denominator.
f) Printing Rational numbers in floating-point format.
I have writen the code like the following:

#ifndef RATIONAL_H
#define RATIONAL_H

class rational {
public :
rational();
void get_f1(int, int);
void get_f2(int, int);
void add(rational&, int, int, int );
private :
int numer;
int denom;
int result;
int result_n;
int result_d;
};

#endif

#include <iostream>
#include "rational.h"

using namespace std;

rational :: rational() {numer = denom = result = result_n = result_d = 0
;}

void rational :: get_f1(int numer, int denom)
{
do {
cout << "please input the first fraction's numerator: " ;
cin >> numer;
cout << "please input the first fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
}

void rational :: get_f2(int numer, int denom)
{
do {
cout << "please input the second fraction's numerator: " ;
cin >> numer;
cout << "please input the second fraction's denominator: ";
cin >> denom;
cout << "the fraction is: " << numer << "/" << denom <<endl;
} while (denom == 0);
}

void rational :: add(rational &rational_1, int result_n, int result_d, int
result)
{
result_n = (rational_1.get_f1.numer * rational_1.get_f2.denom) +
(rational_1.get_f2.numer * rational_1.get_f1.denom); /* here i want to add
the two fractions, but i don't know how since my teach ask me to use two
class object to implement the addition */
result_d = rational_1.get_f1.denom * rational_1.get_f2.denom;
cout << rational_1.get_f1.numer <<result_d;

if (result_n==result_d)result=1;
for (int n=1 ; n <= result_d; n++)
{
for (int t=2; t <= result_n; t++)
{
if (result_n % t == 0 && result_d % t == 0)
{
result_n = result_n/t;
result_d = result_d/t;
}
}
}
if (result_d ==1)
cout <<endl <<result_n <<endl;
else if (result_d == 0)
cout <<endl << 0;
else
cout <<endl<< result_n << "/" << result_d <<endl;
}
#include <iostream>
#include "rational.h"

using namespace std;

int main ()
{
rational rational_1, rational_result;
int numer, denom, result, result_n, result_d;

rational_1.get_f1(numer, denom);
rational_1.get_f2(numer, denom);
rational_result.add(rational_1, result, result_n, result_d);
return 0;
}

Have a look at Boost's rational class for ideas:
http://www.boost.org/libs/rational/index.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFBwMHTpxCQXwV2bJARAvgjAJ9wBPaZfYfKCxWlOkSotB DaNl0yVwCgrpdp
V/sOdk3Qkdz7hPCzbZq+TZ8=
=iJOe
-----END PGP SIGNATURE-----
Jul 22 '05 #4

P: n/a
Ms. Thomas Matthews, i dont understand this part, what is the meaning of
whole_number? rational& rational ::....?

"In order to add fractions, they must have equivalent denomenators.
If they don't, then you have to come up with a least common denomenator.

rational&
rational ::
add(int whole_number)
{
numer = numer + denom * whole_number;
return *this;
} "
Jul 22 '05 #5

P: n/a

"surrealtrauma" <su***********@hotmail.com> wrote in message
news:f8******************************@localhost.ta lkaboutprogramming.com...
Ms. Thomas Matthews, i dont understand this part, what is the meaning of
whole_number?
He means 'integer', i.e. a 'whole number', with no fractional part.
rational& rational ::....?
The first part (rational&) specifies that the function returns
a reference to type 'rational'. The second part (rational::)
indicates that the function is a member of class 'rational'.
(The 'fully qualified' name of the function is 'rational::add').

Which C++ book(s) are you reading that don't explain this?

"In order to add fractions, they must have equivalent denomenators.
If they don't, then you have to come up with a least common denomenator.

rational&
rational ::
add(int whole_number)
{
numer = numer + denom * whole_number;
return *this;
} "


-Mike
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.