1 / 8

Static Members

Static Members. 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. // fraction.h class Fraction {   public:     Fraction(const int n=0, const int d=1) : m_Num (n) { setDen (d); }

joelle-wall
Télécharger la présentation

Static Members

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: • intm_Numerator; • intm_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: • intm_Numerator; • intm_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(.000001);

  8. End of Session

More Related