473,770 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ 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==resu lt_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 5922
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==resu lt_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_decim al();
}

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_use r(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.l earn.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==resu lt_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

iD8DBQFBwMHTpxC QXwV2bJARAvgjAJ 9wBPaZfYfKCxWlO kSotBDaNl0yVwCg rpdp
V/sOdk3Qkdz7hPCzb Zq+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

"surrealtra uma" <su***********@ hotmail.com> wrote in message
news:f8******** *************** *******@localho st.talkaboutpro gramming.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
1765
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 = 5 Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'object' object has no attribute 'a'
0
1450
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 learning c# the hard way, but the similarities to Java are very helpful. However, in this case they are hurting, because I can't figure out the best way to store a class that can be accessed across the scope of an ASP.NET application. To begin with,...
1
1386
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
4806
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
6
1910
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 unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object variable...
1
2198
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
3446
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 with "Me". But if the Session variable already exists, I would like the new object be populated with everything from the object stored in the Session variable. Ideally, I'd like to retrieve the object from the Session variable and assign it to...
3
3260
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 exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object variable or With block...
4
1284
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 to caputre it and use it. class ClassA() { A(ref ClassB ObjectOfClassB) { //save object of class be to use in another function }
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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 we have to send another system
2
3589
muto222
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.