473,785 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ help needed in class

11 New Member
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::Fract ion()
{
num=0;
den=1;
}


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



void Fraction:plus(F raction 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::divid e(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::todec imal()
{double dec;

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

void Fraction::reduc e()

{

}





Fraction::~Frac tion()
{
}







//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(cou t);
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(cou t);
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(cou t);
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(cou t);
cout << endl << "Decimal is " << first.todecimal ();
break;}
default: cout << "ERROR : Menu Choice";
break;

}








}
Dec 3 '06 #1
2 3343
horace1
1,510 Recognized Expert Top Contributor
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 New Member
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
1616
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. I'm wondering how often is this feature needed? Would there be any problems if it weren't part of the language? --
67
4285
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. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
5
8987
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 something more sophistictated like a vector of row objects. The data is of this format (same number of columns for each row): A, x, y, z, ...
8
1410
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 Combination(long n, long k)
7
2196
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 another. I have tried declaring them as shared, public, friend, etc and I always get an error stating that something is not valid on a local variable declaration. For example, in the following code for Sub DataGrid_Select, I have CurrentID and...
12
1748
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 have the following declaration: Overloads Overrides Sub Remove(ByVal wIndex As Integer)
5
1712
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 into modules. For example, assuming we have 4 main tasks to accomplish: - Printing - Editing - Viewing - Inventory Management
3
6573
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 chessSquare and attributes }
5
2294
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
13
1854
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 form to it... ill post the code for you to look at and try to explaine in detail what im trying to do. here is the code (HTML and Javascript so you get the whole picture): Javascript: var gen, lights; var lammount;
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
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,...
1
10098
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
8986
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...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
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
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.