Thread: Question about class

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Question about class

    I am stuck over these for hours. Plz help.

    1.There are 3 files here: fraction.h - class definition , fraction.cpp - member functions & main.cpp- main function. How to link fraction.cpp with fraction.h? Does it need to link fraction.cpp with main.cpp also? How? Actually I tried to link them. I am not sure what I have done.

    2.There are 2 errors in Fraction.cpp. It is specified in the code. It said missing ; before string .That's weird. It doesn't have any string. I can't get it. How to fix them?And I am sure every { matches with } there.How come there is unexpected file end?

    3. I wrote a function- add(Fraction num) to get the numerator & denominator of (num1+num2) so that it is called in this way:
    ans = num1;
    ans.add(num2);
    Does it have problem?

    This is a simplified version of my code:
    header:
    Code:
    class Fraction
    {
    public:
    	Fraction(int numer1, int denom1);
    	Fraction();
    	void display();
    	void add(Fraction num);
    
    private:
    	int denom,numer;
    	void reduce();
    }
    Fraction.cpp
    Code:
    #include "Fraction.h"
    #include <iostream>
    using namespace std;
    Fraction::Fraction(int numer1, int denom1)
    {
    	numer=numer1;
    	denom=denom1;
    }
    Fraction::Fraction()
    {
    	numer=0;
    	denom=0;
    }
    void Fraction::display()
    {
    	cout<<numer<<"/"<<denom;
    }
    void Fraction::reduce()
    {
    	int x;
    	if (numer>denom)
    	{
    		for(x=denom;x>1;x--)
    		{
    			if ((numer%x==0) && (denom%x==0))
    			{
    				numer=numer/x;
    				denom=denom/x;break;
    			
                            }//syntax error : missing ';' before 'string'
                             //fatal error C1004: unexpected end of file found
    		}
    	}
    	if (numer<denom)
    	{
    		for(x=numer;x>1;x--)
    		{
    			if ((numer%x==0) && (denom%x==0))
    			{
    				numer=numer/x;
    				denom=denom/x;break;
    			}
    		}
    	}
    }
    void add(Fraction num)
    {
    	numer=numer+num.numer;
    	denom=denom+num.denom;
    	reduce();
    }
    main.cpp
    Code:
    #include "fraction.h"
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	int numerator, denominator;
    	Fraction num1, num2, ans;
    
    	cout << "Input a fraction (a/b): ";
    	cin >> numerator >> denominator;
    	num1 = Fraction(numerator, denominator);
    
    	cout << "Input another fraction (a/b): ";
    	cin >> numerator >> denominator;
    	num2 = Fraction(numerator, denominator);
    
    	// addition
    	ans = num1;
    	ans.add(num2);
    	num1.display();
    	cout << " + ";
    	num2.display();
    	cout << " = ";
    	ans.display();
    	cout << endl;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    class Fraction
    {
    public:
    	Fraction(int numer1, int denom1);
    	Fraction();
    	void display();
    	void add(Fraction num);
    
    private:
    	int denom,numer;
    	void reduce();
    };
    Code:
    void Fraction::add(Fraction num)
    {
    	numer=numer+num.numer;
    	denom=denom+num.denom;
    	reduce();
    }

    [edit]And main should return an int... [/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How to link fraction.cpp with fraction.h?
    Headers aren't linked. The preprocessor adds them to your .cpp files before compilation and linking glues together the object files that result from compilation. Just include your headers and link your .cpp files. All will be well.

    >It said missing ; before string
    Sometimes a syntax error will hide for a while. The actual problem is that you didn't terminate your Fraction class definition with a semicolon.

    >Does it have problem?
    Yes, right now the definition of add doesn't say that it's a member function of Fraction. You need to add Fraction:: to it.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thx!I thought it would be going more complicated.lol
    Last edited by Roy01; 11-20-2006 at 11:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM