•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,624 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,187 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2393 | Replies: 1
![]() |
•
•
Join Date: Feb 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hi there,I'm having a problem with calling an object member function of a class 'Fraction'. Particularly the Fraction addTwoFraction,mult.div,etc.I've included the code i've worked on and in it you'll see i've put 3 instances of where i'd like atleast your thoughts on it. The first is in callingEnviron(defined right after main in this code) and works itself up from there. Thanks for any help guys.
#include <iostream>
using namespace std;
void callingEnviron();
class Fraction
{
private:
int iNum;
int iDenom;
public:
Fraction(void)
{
cout << " construtor called " << endl;
iNum = 0;
iDenom = 1;
}
Fraction(int, int)
{
cout << "constructor 2 called " << endl;
}
~Fraction()
{
}
/*
(3)
I've created a constructor before with Fraction(const, const) but it did nothing
With the way it calls those binary operator functions, is Fraction(int , int) sufficient
or should there be another constructor for those four binary Fraction functions.
*/
void assign(int, int);
void printf(void);
void increment(void);
void addTo(const Fraction &);
};
void Fraction::assign(int iTemp1, int iTemp2)
{
iNum = iTemp1;
iDenom = iTemp2;
cout << " " << iNum << " " << iDenom << endl;
return;
}
void Fraction::printf(void)
{
cout <<" " << iNum
<<" " << iDenom
<< endl;
}
Fraction addTwoFraction(const Fraction &frOld1, const Fraction &Old2)
{
Fraction iAdd = frOld1;
/*
(2) This is an area I need help with, once i pass the adress of the first fraction
How would i get acess to those private members. The way i have it setup those private
members are accessible through the function calls defined in the class,assign and printf.
So i'm just a little bit lost as to whether when i pass the first fraction i have to
assign those variables and values to a new fraction that can be worked with.
The general algorithm I've worked on for finding the sum of two fractions is this.
iAdd.iNum= ((firstFraction.iNum) * (secondFraction.iDenom)) + ((firstFraction.iDenom2) * (secondFraction.iNum));
iAdd.iDenom= ((firstFraction.iDenom) * (secondFraction.iDenom));
*/
return iAdd;
}
Fraction addTwoFraction(const Fraction &frOld1, const Fraction &Old2);
Fraction subtractTwoFraction(const Fraction &frOld1, const Fraction &Old2);
Fraction multiplyTwoFraction(const Fraction &frOld1, const Fraction &Old2);
Fraction divideTwoFraction(const Fraction &frOld1, const Fraction &Old2);
Fraction firstFraction;
Fraction secondFraction;
int main()
{
callingEnviron();
return 0;
}
void callingEnviron()
{
int ifirstnum;
int ifirstdenom;
int isecondnum;
int iseconddenom;
cout << "Enter the numerator of 1st fraction" << endl;
cin >> ifirstnum;
cout << "Enter the denominator of 1st fraction" << endl;
cin >> ifirstdenom;
firstFraction.assign(ifirstnum, ifirstdenom);
firstFraction.printf();
cout << "Enter the numerator of 2nd fraction" << endl;
cin >> isecondnum;
cout << "Enter the denominator of 2nd fraction" << endl;
cin >> iseconddenom;
firstFraction.assign(isecondnum, iseconddenom);
firstFraction.printf();
cout<<" " << &firstFraction << endl;
cout<<" " << &secondFraction << endl;
Fraction addTwoFraction(Fraction firstFraction,Fraction secondFraction);
/*
(1) This is the first part i have some uncertainty about. How to call this function.
Should it be simply -
addTwoFraction(firstFraction,secondFraction);
Do you think the function call should even be here and not somewhere such as in the assign
and printf functions defined in class where the iNum and iDenom private members are more so
accessible.
*/
return;
}
•
•
Join Date: Mar 2004
Posts: 76
Reputation:
Rep Power: 5
Solved Threads: 2
1)what u should do is have a member function called add_fract(Fraction ftoadd). this function would be used as follows:
then this would add other_fract to fract. you also need to add a 'get' method to your class, something that will return the two private members num and denom.
2)get/set methods!
3)i dont understand what you mean?
Fraction fract(3,4), other_fract(5,6); fract.add_fract(other_fract);
2)get/set methods!
3)i dont understand what you mean?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- designing classes (C++)
- Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b (C++)
- problem when trying to use a copy constuctor (C++)
- class toString() functionality in C++ ...? (C++)
- Using map<> with classes (C++)
- Understanding classes (C++)
- Linked List (C++)
Other Threads in the C Forum
- Previous Thread: fread problem?
- Next Thread: typedef question


Linear Mode