GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Oct-2004, 07:24
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road

accessing a private member in a public member


hi everybody!!!
i have a class as
CPP / C++ / C Code:
typedef enum{ POS, NEG } SignType;

class Fraction
{

  private:
    int numerator;    /* numerator and denominator are declared as */
    int denominator;  /* int to simplify the algorithms, but they  */
    SignType sign;    /* will always be stored >= 0                */

  public:

    /********** Constructors, Destructors, and Clone ************/
    Fraction();  /* returns 0/1 */
    Fraction(int n, int d); /* returns n/d */

    /*********************** Accessors **************************/
    int getNumerator();
    int getDenominator();
    char getSign();

    /******************* Modifiers *****************************/
    void setNumerator(int);
    void setDenominator(int);
    void setSign(char);

    /*************** Fraction Operations ***********************/
    void reduceFract();
    Fraction negateFract( const Fraction &f1);
    Fraction addFract( const Fraction &f1, const Fraction &f2 );
    Fraction subFract( const Fraction &f1, const Fraction &f2 );
    Fraction mulFract( const Fraction &f1, const Fraction &f2 );
    Fraction divFract( const Fraction &f1, const Fraction &f2 );
    int compareFract( const Fraction &f1,  const Fraction &f2 );
    /* returns -1 if f1 <  f2
                0 if f1 == f2  
               +1 if f1 >  f2  
    */

    /******************* I/O *********************************/
    void readFract(Fraction &f);
    
    void printFract(const Fraction &f );

  // utility functions:
  private:
    int gcd(int, int);
    void arithmeticSign();
    void standardSign();

};
#endif
in this code will make some fraction operations. and i want to use private
CPP / C++ / C Code:
void arithmeticsign(); 
in public
CPP / C++ / C Code:
Fraction addFract( const Fraction &f1, const Fraction &f2 );

i know that i will just call
CPP / C++ / C Code:
arithmethicSign;
in add operation but add has two arguments and i need this arithmetic sign for both argumnets??
thanks everybody!!!
  #2  
Old 16-Oct-2004, 09:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by small_ticket
hi everybody!!!
i have a class as
CPP / C++ / C Code:
typedef enum{ POS, NEG } SignType;

class Fraction
{

  private:
    int numerator;    /* numerator and denominator are declared as */
    int denominator;  /* int to simplify the algorithms, but they  */
    SignType sign;    /* will always be stored >= 0                */

  public:

    /********** Constructors, Destructors, and Clone ************/
    Fraction();  /* returns 0/1 */
    Fraction(int n, int d); /* returns n/d */

    /*********************** Accessors **************************/
    int getNumerator();
    int getDenominator();
    char getSign();

    /******************* Modifiers *****************************/
    void setNumerator(int);
    void setDenominator(int);
    void setSign(char);

    /*************** Fraction Operations ***********************/
    void reduceFract();
    Fraction negateFract( const Fraction &f1);
    Fraction addFract( const Fraction &f1, const Fraction &f2 );
    Fraction subFract( const Fraction &f1, const Fraction &f2 );
    Fraction mulFract( const Fraction &f1, const Fraction &f2 );
    Fraction divFract( const Fraction &f1, const Fraction &f2 );
    int compareFract( const Fraction &f1,  const Fraction &f2 );
    /* returns -1 if f1 <  f2
                0 if f1 == f2  
               +1 if f1 >  f2  
    */

    /******************* I/O *********************************/
    void readFract(Fraction &f);
    
    void printFract(const Fraction &f );

  // utility functions:
  private:
    int gcd(int, int);
    void arithmeticSign();
    void standardSign();

};
#endif
in this code will make some fraction operations. and i want to use private
CPP / C++ / C Code:
void arithmeticsign(); 
in public
CPP / C++ / C Code:
Fraction addFract( const Fraction &f1, const Fraction &f2 );

i know that i will just call
CPP / C++ / C Code:
arithmethicSign;
in add operation but add has two arguments and i need this arithmetic sign for both argumnets??
thanks everybody!!!
With the information you've given, we can't tell. We need to know what arithmeticSign() does. As a guess, it returns the sign of the entire equation because it has no parameters, therefore you only need to call it once to get the sign before you do the operation.
__________________

Age is unimportant -- except in cheese
  #3  
Old 16-Oct-2004, 11:10
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
Quote:
Originally Posted by WaltP
With the information you've given, we can't tell. We need to know what arithmeticSign() does. As a guess, it returns the sign of the entire equation because it has no parameters, therefore you only need to call it once to get the sign before you do the operation.
CPP / C++ / C Code:
void Fraction :: arithmeticSign()
{
	if (sign=NEG)
		numerator=-numerator;
}
CPP / C++ / C Code:
void Fraction :: readFract(Fraction &f)
{
	float a,b,c;
	cout<<"Enter the numerator for the fraction :\n";
	cin>>a;
	cout<<"enter the denominator for the fraction :\n";
	cin>>b;
	c=a/b;
	if(c<0)
		setSign(-);
	if(c>0)
		setSign(+);
	setNumerator(a);
	setDenominator(b);

}
CPP / C++ / C Code:
void Fraction :: setNumerator(int n)
{
	numerator=abs(n);
}
void Fraction :: setDenominator(int d)
{
	denominator=abs(d);
}
void Fraction :: setSign(char c)
{
	if(c=='+') 
		sign=POS;
	
	if (c=='-')
		sign=NEG;
}

these are some function members that could make some sense to you!!!
actually we store the numerator and denominator as unsigned numbers and its sign in a different member (sign) so when we are going to do calculations we need to use both numerator and denominator as negative or positive so we must call artihmethicSign function. but the trouble is add function member has two arguments and i must call arithmethicSign for both of them ??
therefore the arithmethicSign member function is a private function so can i do ?
CPP / C++ / C Code:
f1.arithmeticSign();
f2.arithmeticSign();
or something else??
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Introduction to .NET LuciWiz .NET Forum 5 09-Aug-2007 04:53
Static Class Member Accessing Melvin Lin C++ Forum 12 04-May-2004 02:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.