473,473 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 5898
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
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
-----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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
0
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
1
by: Penna Elabi | last post by:
Is class a variable? Can a variable consist of variables? For example, a vector is a class, so is this vector a variable when it includes two int variables: vector < int > exampleVector( 2, 2...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
6
by: Neo Geshel | last post by:
I am trying to deal with an image in code-behind. I consistently get the following error: Server Error in '/' Application. Object variable or With block variable not set. Description: An...
1
by: archana | last post by:
Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }
4
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it...
3
by: Newbie19 | last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message: Object variable or With block variable not set. Description: An unhandled...
4
by: Studlyami | last post by:
Okay i create a class object in my "main" class. I want to pass this object by reference to another class. I am able to pass the ref of the object to the constructor, but i don't know how i am able...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.