Static Members.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Advertisements

Operator overloading redefine the operations of operators
For(int i = 1; i
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
1 CSC241: Object Oriented Programming Lecture No 21.
EC-241 Object-Oriented Programming
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
برنامه سازي پيشرفته 1 4. using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public.
Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.
EXAMPLE 1 Writing Equivalent Fractions. EXAMPLE 1 Writing Equivalent Fractions Write two fractions that are equivalent to. Writing Equivalent Fractions.
Changing Percents to a Fraction #3 To change a percent to a fraction you need to first write the numerator over 100. Next simplify the fraction.
Modeling Multiplication of Fractions
Negation Operator. Code Trace // main.cpp... g = -f;... // fraction.h... class Fraction { friend Fraction operator-(const.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Greatest Common Factor.
Fractions VI Simplifying Fractions Factor A number that divides evenly into another. Factors of 24 are 1,2, 3, 4, 6, 8, 12 and 24. Factors of 24 are.
Adding & Subtracting Whole Number and Fractions
Conversion of Fractions - Mixed Number to Improper Fraction
Multiplying Fractions. Fraction with a Fraction 1. Multiply numerators 2. multiply denominators 3. reduce.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Regarding assignment 1 Style standards Program organization
Find the common denominator. Make equivalent fractions.
4 Rational Numbers: Positive and Negative Fractions.
Fractions: Adding and Subtracting Like Denominators
Structs versus Classes
Automatics, Copy Constructor, and Assignment Operator
null, true, and false are also reserved.
Fraction Abstract Data Type
Accessor and Mutator Functions
Comparing Fractions A VISUAL LESSON.
Stack Memory 2 (also called Call Stack)
Change each Mixed Number to an Improper Fraction.
Arrays and Classes Programming in C++ Fall 2008
Friend Functions.
Modeling Multiplication of Fractions
Introduction to Programming
Java Lesson 36 Mr. Kalmes.
Fractions: Adding and Subtracting Like Denominators
Bracket Operator.
Default Arguments.
Problem Solving Session
class PrintOnetoTen { public static void main(String args[]) {
Fractions IV Equivalent Fractions
Comparing fractions.
Which fraction is the same as ?
Programming Language C Language.
Adding fractions with like Denominators
subtracting fractions with like denominators
Defining Class Functions
Class rational part2.
Understanding Fractions: Part of a Whole
Chapter 11 Classes.
Fractions VII Subtracting Like Denominators
Reducing (Simplifying) Fractions
Presentation transcript:

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); }     void readin();     ...     static double zero_tolerance;   private:     int m_Numerator;     int m_Denominator; }; double Fraction::zero_tolerance = .0001;

// 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);

// 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; }

// 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;

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

End of Session