Download presentation
Presentation is loading. Please wait.
1
Static Members
2
There can be only one a static member variable in a class represents all objects of that class, doing so with one and only one value
3
// fraction.h class Fraction { public: Fraction(const int n=0, const int d=1) : m_Num(n) { setDen(d); } void readin(); ... static double zero_tolerance; private: int m_Numerator; int m_Denominator; }; double Fraction::zero_tolerance = .0001;
4
// fraction.h class Fraction { public: Fraction(const int n=0, const int d=1) : m_Num(n) { setDen(d); } void readin(); ... static double zero_tolerance; private: int m_Numerator; int m_Denominator; }; double Fraction::zero_tolerance = .0001; // main.cpp ... Fraction f(3,10000); if (static_cast<double>(f.getNumer())/f.getDenom() <= Fraction::zero_tolerance) f.setNumer(0);
5
// fraction.h class Fraction { public: static double zero_tolerance; ... private: void zeroize_if_close(); }; double Fraction::zero_tolerance = .0001; // fraction.cpp void Fraction::zeroize_if_close() if(static_cast<double>(m_Numerator)/m_Denominator <= zero_tolerance) m_Numerator = 0; return; }
6
// fraction.cpp void Fraction::zeroize_if_close() { if(static_cast<double>(m_Numerator)/m_Denominator <= zero_tolerance) m_Numerator = 0; return; } void Fraction::readin() cout<<“enter numerator: ”; cin>>m_Numerator; cout<<“enter denominator: ”; cin>>m_Denominator; zeroize_if_close(); return;
7
Static Functions // fraction.h class Fraction { public:
static void reset_zero_tolerance(const double tol){zero_tolerance = tol;} ... }; double Fraction::zero_tolerance = .0001; // main.cpp Fraction::reset_zero_tolerance( );
8
End of Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.