473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ help needed in class

11
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:

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.

ok i dont seem to understand why i keep getting the same error, someone please help me, its due monday :( it keeps saying error C2470: 'Fraction' : looks like a function definition, but there is no parameter list; skipping apparent body

//this is the header file
#include <iostream>
#include <fstream>
using namespace std;

class Fraction
{
public:
Fraction(void);
Fraction(int n, int d);
void plus(Fraction second);
void minus(Fraction second);
void times(Fraction second);
void divide(Fraction second);
void reduce();
double todecimal();
void scan(istream& in);
void print(ostream& out);
~Fraction();

private:
int num,den;

};



// The cpp file
#include "Fraction.h"

Fraction::Fraction()
{
num=0;
den=1;
}


Fraction::Fraction(int n, int d){
num=n;
den=d;
}



void Fraction:plus(Fraction second){
int tempden= den*second.den;
num=num*second.den + den*second.num;
den=tempden;
}

void Fraction::minus(Fraction second){
int tempden= den*second.den;
num=num*second.den - den*second.num;
den = tempden;}

void Fraction::times(Fraction second){
num = num*second.num;
den=second.den*den;
}

void Fraction::divide(Fraction second){
num = num*second.den;
den= den*second.num;
}

void Fraction::scan(istream & in){
char slash;
in >> num >> slash >> den;

}


void Fraction:print(ostream & out){
out << num <<" / " << den;}





double Fraction::todecimal()
{double dec;

dec=(num*1.00)/(den*1.00);
return dec;
}

void Fraction::reduce()

{

}





Fraction::~Fraction()
{
}







//program file



#include "Fraction.h"
using namespace std;
int main(){
Fraction first;
Fraction d(1,2);
cout << " Fraction class menu" << endl;
cout << " " << endl;
cout << " Please enter fraction in form of 1/2"<<endl;
first.scan(cin);
cout << " What would you like to do ? (fraction will automatically be reduced and given in decimals)"<< endl;
cout << "1. plus" << endl;
cout << "2. minus" << endl;
cout << "3. times"<<endl;
cout << "4. divide"<<endl;

cout << " Make your choice now" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1: {
cout << "Enter a second fraction to be added to";
d.scan(cin);
first.plus(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();


break;}
case 2: {
cout << " Enter a second fraction to be subtracted from";
d.scan(cin);
first.minus(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 3: {
cout << "Enter a second fraction to be timed by" << endl;
d.scan(cin);
first.times(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 4:{ cout << "Enter a second fraction to be divided by" <<endl;
d.scan(cin);
first.divide(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
default: cout << "ERROR : Menu Choice";
break;

}








}
Dec 3 '06 #1
2 3318
horace1
1,510 Expert 1GB
your fraction.cpp file contained a couple of faulty characters

fixed code and this now compiles and when linked with main() runs
Expand|Select|Wrap|Line Numbers
  1. #include "Fraction.h"
  2.  
  3. Fraction::Fraction()
  4. {
  5. num=0;
  6. den=1;
  7. }
  8.  
  9. Fraction::Fraction(int n, int d){
  10. num=n;
  11. den=d;
  12. }
  13.  
  14. void Fraction::plus(Fraction second){   // ** removed  odd character here
  15. int tempden= den*second.den;
  16. num=num*second.den + den*second.num;
  17. den=tempden;
  18. }
  19.  
  20. void Fraction::minus(Fraction second){
  21. int tempden= den*second.den;
  22. num=num*second.den - den*second.num;
  23. den = tempden;}
  24.  
  25. void Fraction::times(Fraction second){
  26. num = num*second.num;
  27. den=second.den*den;
  28. }
  29.  
  30. void Fraction::divide(Fraction second){
  31. num = num*second.den;
  32. den= den*second.num;
  33. }
  34.  
  35. void Fraction::scan(istream & in){
  36. char slash;
  37. in >> num >> slash >> den;
  38.  
  39. }
  40.  
  41. void Fraction::print(ostream & out){  // ** removed odd character here
  42. out << num <<" / " << den;}
  43.  
  44.  
  45. double Fraction::todecimal()
  46. {double dec;
  47.  
  48. dec=(num*1.00)/(den*1.00);
  49. return dec;
  50. }
  51.  
  52. void Fraction::reduce()
  53.  
  54. {
  55.  
  56. }
  57.  
  58. Fraction::~Fraction()
  59. {
  60. }
  61.  
  62.  
Dec 3 '06 #2
Anan18
11
thanks sir. let me see where the errors were.
Dec 3 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Russell Wallace | last post by:
Hi all, Python lets you continue a single logical line across more than one physical line, either by putting a \ at the end or letting it happen automatically with an incomplete infix operator....
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
8
by: Brett Romero | last post by:
I'd like to know when exactly the this. keyword is needed. For example: public class Combination { private long n = 0; private long k = 0; private long data = null; public...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
12
by: Lee Silver | last post by:
In a base class I have the following 2 declarations: Overridable Sub Remove(ByVal wIndex As Integer) and Overridable Sub Remove(ByVal wValue As Object) In an immediately derived class I...
5
by: Steve | last post by:
Hi, I am sitting down to design our next set of internal apps. I would like to approach this in a way that would allow me to break logical parts of the application that handle specific tasks...
3
by: unicorn7777777 | last post by:
Hi, I need a deep copy constructor for a class which contains an array for another class: class Chessboard { public: ChessSquare chessSquare; // copy constructor needed to copy all...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
13
by: DDragon | last post by:
ok here is the problem, i have to forms which have values i wish to be added together i can add together the values in one form all right but im having problems with adding the values of the other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.