Static const datatype question for Fraction class

Hi,
My Fraction.h class looks like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 :

1
2
3
4
5
6
#include "Fraction.h"

Fraction::Fraction(int n, int d):num(n),den(d)
{
   cout << This is double param constructor <<endl;
}

And the application main.cpp is

1
2
3
4
5
6
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
///////////////////// fraction.h //////////////////////////////

// include guard see: http://en.wikipedia.org/wiki/Include_guard
#ifndef FRACTION_H_INCLUDED_
#define FRACTION_H_INCLUDED_

#include <iosfwd> // 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_ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
///////////////////// fraction.cpp //////////////////////////////

#include "fraction.h"
#include <iostream>

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 << ')' ;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
///////////////////// main.cpp //////////////////////////////

#include "fraction.h"
#include <iostream>

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

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.