954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Developing A Class

I am working on a lab and having trouble developing a class to run with the program. I have completed all the arithmetic for the program but not sure how to included the functions in with a "class".

The instructions are to develope class called "rational"- and should contain the following
a. 2 private integer data members representing the numerator and denominator of the rational #
b. a public member function that will allow any function to retrieve the numerator, using an interger return value.
c. a public member function that will allow any function to retrieve the denominator, using an interger return value.
d. a constructor that accepts the numerator and denominator values as arguments.

The main function should included 4 functions (addem, subtracem,mutiplyem,dividem)

I think I have the functions correct. Just not sure on how to include the class and constructor.

#include
#include
#include

using namespace std;


void addem (int n1, int d1, int n2, int d2, int na, int da)
{
da = d1 * d2; // Get common donimator for fractions
na = ((da/d1)*n1)+((da/d2)*n2); // Perform addition on fractions


cout <> numerator1 >> denominator1;
cout << "Enter a numerator and denominator of a fraction seperated by a space: ";
cin >> numerator2 >> denominator2;;

addem (numerator1,denominator1,numerator2,denominator2,nummeratoranswer,denominatoranswer);
subtractem(numerator1,denominator1,numerator2,denominator2,nummeratoranswer,denominatoranswer);
multiplyem(numerator1,denominator1,numerator2,denominator2,nummeratoranswer,denominatoranswer);
dividem(numerator1,denominator1,numerator2,denominator2,nummeratoranswer,denominatoranswer);

system ("pause");

return 0;
}

tonja1196
Newbie Poster
9 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

u can use . operator or u can crate object of your class by...class_name objectnae;

achyut
Newbie Poster
11 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

First, a tangential comment. Your function "addem" has a problem. You want the answers to appear in the variables na and da, so you'r asking the function to change the value of variables on the parametr list. You can't do this successfully unless you make them reference variables. (i.e., just replace na by %na, etc, in the parameter list).

Now to the main point: you need a class with 2 data members and 3 function members, as you've described. Say this class is called "rat". Your addem function (for example) shouldn't be a function of 4 integers (n1, d1, n2, d2) but rather of two objects of class rat.

murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 21
Solved Threads: 1
 

One more thing:
Code Tags

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You