943,670 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3183
  • C RSS
Feb 23rd, 2004
0

Need help with Const, Ref and Classes.

Expand Post »
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.


  1.  
  2. #include <iostream>
  3. using namespace std;
  4. void callingEnviron();
  5. class Fraction
  6. {
  7. private:
  8. int iNum;
  9. int iDenom;
  10. public:
  11. Fraction(void)
  12. {
  13. cout << " construtor called " << endl;
  14. iNum = 0;
  15. iDenom = 1;
  16. }
  17. Fraction(int, int)
  18. {
  19. cout << "constructor 2 called " << endl;
  20. }
  21. ~Fraction()
  22. {
  23. }
  24. /*
  25.  (3)
  26.  I've created a constructor before with Fraction(const, const) but it did nothing
  27.  With the way it calls those binary operator functions, is Fraction(int , int) sufficient
  28.  or should there be another constructor for those four binary Fraction functions.
  29.  */
  30. void assign(int, int);
  31. void printf(void);
  32. void increment(void);
  33. void addTo(const Fraction &);
  34. };
  35. void Fraction::assign(int iTemp1, int iTemp2)
  36. {
  37. iNum = iTemp1;
  38. iDenom = iTemp2;
  39. cout << " " << iNum << " " << iDenom << endl;
  40. return;
  41. }
  42. void Fraction::printf(void)
  43. {
  44. cout <<" " << iNum
  45. <<" " << iDenom
  46. << endl;
  47. }
  48. Fraction addTwoFraction(const Fraction &frOld1, const Fraction &Old2)
  49. {
  50. Fraction iAdd = frOld1;
  51. /*
  52.  (2) This is an area I need help with, once i pass the adress of the first fraction
  53.  How would i get acess to those private members. The way i have it setup those private
  54.  members are accessible through the function calls defined in the class,assign and printf.
  55.  So i'm just a little bit lost as to whether when i pass the first fraction i have to
  56.  assign those variables and values to a new fraction that can be worked with.
  57.  
  58.  The general algorithm I've worked on for finding the sum of two fractions is this.
  59.  iAdd.iNum= ((firstFraction.iNum) * (secondFraction.iDenom)) + ((firstFraction.iDenom2) * (secondFraction.iNum));
  60.  iAdd.iDenom= ((firstFraction.iDenom) * (secondFraction.iDenom));
  61.  */
  62. return iAdd;
  63. }
  64. Fraction addTwoFraction(const Fraction &frOld1, const Fraction &Old2);
  65. Fraction subtractTwoFraction(const Fraction &frOld1, const Fraction &Old2);
  66. Fraction multiplyTwoFraction(const Fraction &frOld1, const Fraction &Old2);
  67. Fraction divideTwoFraction(const Fraction &frOld1, const Fraction &Old2);
  68. Fraction firstFraction;
  69. Fraction secondFraction;
  70.  
  71. int main()
  72. {
  73. callingEnviron();
  74. return 0;
  75. }
  76. void callingEnviron()
  77. {
  78. int ifirstnum;
  79. int ifirstdenom;
  80. int isecondnum;
  81. int iseconddenom;
  82. cout << "Enter the numerator of 1st fraction" << endl;
  83. cin >> ifirstnum;
  84. cout << "Enter the denominator of 1st fraction" << endl;
  85. cin >> ifirstdenom;
  86. firstFraction.assign(ifirstnum, ifirstdenom);
  87. firstFraction.printf();
  88. cout << "Enter the numerator of 2nd fraction" << endl;
  89. cin >> isecondnum;
  90. cout << "Enter the denominator of 2nd fraction" << endl;
  91. cin >> iseconddenom;
  92. firstFraction.assign(isecondnum, iseconddenom);
  93. firstFraction.printf();
  94. cout<<" " << &firstFraction << endl;
  95.  
  96. cout<<" " << &secondFraction << endl;
  97.  
  98. Fraction addTwoFraction(Fraction firstFraction,Fraction secondFraction);
  99. /*
  100.  (1) This is the first part i have some uncertainty about. How to call this function.
  101.  Should it be simply -
  102.  addTwoFraction(firstFraction,secondFraction);
  103.  Do you think the function call should even be here and not somewhere such as in the assign
  104.  and printf functions defined in class where the iNum and iDenom private members are more so
  105.  accessible.
  106.  */
  107.  
  108. return;
  109. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dev1l is offline Offline
1 posts
since Feb 2004
Mar 23rd, 2004
0

Re: Need help with Const, Ref and Classes.

1)what u should do is have a member function called add_fract(Fraction ftoadd). this function would be used as follows:
  1. Fraction fract(3,4), other_fract(5,6);
  2.  
  3. fract.add_fract(other_fract);
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?
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: fread problem?
Next Thread in C Forum Timeline: typedef question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC