### Static const datatype question for Fraction class

Hi,
My Fraction.h class looks like :

 ``12345678910111213141516171819`` ``````class Fraction { int num; unsigned int den; public: Fraction(int = 1,int =1); //Constants of Datatype static const Fraction sc_fUnity ; // Defines 1/1 static const Fraction sc_fZero; // Defines 0/1 // Static Utility functions static const int precision() {return 1000000;}; };``````

The implementation Fraction.cpp is as follows :

 ``123456`` ``````#include "Fraction.h" Fraction::Fraction(int n, int d):num(n),den(d) { cout << This is double param constructor <

And the application main.cpp is

 ``123456`` ``````int main() { Fraction f1(3,9); f1 = Fraction::sc_fUnity; // how to implement this ? }``````

How can I write the Fraction.cpp for the constant static member ?
const Fraction Fraction::sc_fUnity( 1, 1 );
const Fraction Fraction::sc_fZero( 0, 1 );
Hi, thanks. Can you please explain it a bit ?
I do not know what is not clear for you?
 ``123456789101112131415161718192021222324252627`` ``````///////////////////// fraction.h ////////////////////////////// // include guard see: http://en.wikipedia.org/wiki/Include_guard #ifndef FRACTION_H_INCLUDED_ #define FRACTION_H_INCLUDED_ #include // forward declaration of std::ostream struct fraction { fraction( int numerator = 1, int denominator = 1 ) ; fraction( double v ) ; static const fraction zero ; static const fraction one ; static int precision() ; // ... private: int num ; int den ; void normalize() ; friend std::ostream& operator<< ( std::ostream& stm, const fraction& f ) ; }; #endif // FRACTION_H_INCLUDED_ ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````///////////////////// fraction.cpp ////////////////////////////// #include "fraction.h" #include const fraction fraction::zero(0,1) ; const fraction fraction::one(1,1) ; int fraction::precision() { return 1000000 ; } static unsigned int hcf( unsigned int a, unsigned int b ) { if( b == 0 ) return a ; else return hcf( b, a%b ) ; } static int make_pos( int a ) { return a<0 ? -a : +a ; } void fraction::normalize() { bool positive = ( num >= 0 ) == ( den >= 0 ) ; num = make_pos(num) ; den = make_pos(den) ; int h = hcf(num,den) ; if( h == 0 ) h = 1 ; num /= h ; den /= h ; if( !positive ) num = -num ; } fraction::fraction( int numerator, int denominator ) : num(numerator), den(denominator) { normalize() ; } fraction::fraction( double d ) : den( precision() ) { num = int( d * precision() ) ; normalize() ; } std::ostream& operator<< ( std::ostream& stm, const fraction& f ) { if( f.num < 0 ) stm << '-' ; return stm << '(' << make_pos(f.num) << '/' << f.den << ')' ; }``````

 ``12345678910111213141516171819202122`` ``````///////////////////// main.cpp ////////////////////////////// #include "fraction.h" #include int main() { fraction a( 24, 36 ) ; std::cout << a << '\n' ; // (2/3) fraction b( -24, -36 ) ; std::cout << b << '\n' ; // (2/3) fraction c( 24, -36 ) ; std::cout << c << '\n' ; // -(2/3) fraction d( -24, 36 ) ; std::cout << d << '\n' ; // -(2/3) fraction e( -24, -36 ) ; std::cout << e << '\n' ; // (2/3) fraction f( 0, -36 ) ; std::cout << f << '\n' ; // (0/1) fraction g( 36, 0 ) ; std::cout << g << '\n' ; // (1/0) fraction h( 0, 0 ) ; std::cout << h << '\n' ; // (0/0) fraction i( 1.25 ) ; std::cout << i << '\n' ; // (5/4) fraction j( -1.25 ) ; std::cout << j << '\n' ; // -(5/4) fraction k( 1.252868 ) ; std::cout << k << '\n' ; // (313217/250000) fraction l( 1.2500009999 ) ; std::cout << l << '\n' ; // (5/4) }``````

http://ideone.com/pED5K6
what does this static const is meant to be doing ?
The code is working now, but what happens 'f1 = Fraction::sc_fUnity ;' is called. Why in the implementation 'const Fraction::Fraction sc_fUnity(1,1);' the
body of the function is missing ?
> Why in the implementation 'const Fraction::Fraction sc_fUnity(1,1);'
> the body of the function is missing ?

Because it is not a static member function; it is a static member variable.

 ``1234567891011121314151617`` ``````#include struct A { static int foo() ; // declaration of static member function static const int bar ; // declaration of static member variable }; int A::foo() { return 1 ; } // definition of static member function const int A::bar(2) ; // definition of static member variable int main() { std::cout << A::foo() << ' ' // call function A::foo() << A::bar << '\n' ; // A::bar is not a function; there is no function call }``````
Topic archived. No new replies allowed.