| | |
Developing A Class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 9
Reputation:
Solved Threads: 0
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 <iostream>
#include <cassert>
#include <math.h>
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 <<n1<<"/"<<d1<<" + "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
void subtractem (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 subtraction on fractions
cout <<n1<<"/"<<d1<<" - "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
void multiplyem (int n1, int d1, int n2, int d2, int na, int da)
{
da = d1*d2; // Get common donimator for fractions
na = n1*n2; // Perform multiplication on fractions
cout <<n1<<"/"<<d1<<" * "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
void dividem (int n1, int d1, int n2, int d2, int na, int da)
{
da = d1*n2; // Get common donimator for fractions
na = n1*d2; // Perform multiplication on fractions
cout <<n1<<"/"<<d1<<" / "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
int main () {
int numerator1,denominator1,numerator2,denominator2,nummeratoranswer,denominatoranswer; // Declare Numerator and Denominator Variables
cout << "Enter a numerator and denominator of a fraction seperated by a space: "; // Prompt and get numerators and denominators to use in operation
cin >> 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;
}
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 <iostream>
#include <cassert>
#include <math.h>
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 <<n1<<"/"<<d1<<" + "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
void subtractem (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 subtraction on fractions
cout <<n1<<"/"<<d1<<" - "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
void multiplyem (int n1, int d1, int n2, int d2, int na, int da)
{
da = d1*d2; // Get common donimator for fractions
na = n1*n2; // Perform multiplication on fractions
cout <<n1<<"/"<<d1<<" * "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
void dividem (int n1, int d1, int n2, int d2, int na, int da)
{
da = d1*n2; // Get common donimator for fractions
na = n1*d2; // Perform multiplication on fractions
cout <<n1<<"/"<<d1<<" / "<<n2<<"/"<<d2;
cout <<" = "<<na<<"/"<<da<<"\n";
}
int main () {
int numerator1,denominator1,numerator2,denominator2,nummeratoranswer,denominatoranswer; // Declare Numerator and Denominator Variables
cout << "Enter a numerator and denominator of a fraction seperated by a space: "; // Prompt and get numerators and denominators to use in operation
cin >> 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;
}
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
One more thing:
Code Tags
Code Tags
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
![]() |
Similar Threads
- problem with DW CSS Horizontal Spry (HTML and CSS)
- Help With Objects. (C#)
- problem with storage ..need help (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with while, do while, and for loops
- Next Thread: help needed with errors
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





