Title: Chapter 14: more about classes Static Members
1Chapter 14 more about classesStatic Members
- If a member variable is declared static, all
objects of that class have access to a single
instance of that variable. - If a member function is declared static, it may
be called before any instances of the class are
defined and it may only use static variables.
2Member Variable is Staticint Ystatic int X
3budget.h Program
- ifndef BUDGET_H
- define BUDGET_H
- class Budget
- private
- static float corpBudget
- float divBudget
- public
- Budget ( void ) divBudget 0
- void addBudget ( float b )
- divBudget b corpBudget divBudget
- float getDivBudget ( void ) return divBudget
- float getCorpBudget ( void ) return
corpBudget - // Budget
- endif
- budget.cpp odd way to initialize
- float BudgetcorpBudget 250000
4Static Member Functions
- The syntax for a static member function is
- static ltreturn typegtltfunction namegt(ltparameter
listgt) - A classs static member variables come into
existence before any instances of the class are
created. - The static member functions use only static
variables and are callable before any instances
of the class are created..
5A New budget.h Program
- ifndef BUDGET_H
- define BUDGET_H
- class Budget
- private
- static float corpBudget
- float divBudget
- public
-
- static void mainOffice ( float )
- // Budget
- endif
6budget.cpp Program
- include "budget.h"
- float BudgetcorpBudget 0
- void BudgetmainOffice ( float moffice )
- corpBudget moffice
- // BudgetmainOffice
7Using budget.h Program
- include ltiostream.hgt
- include ltiomanip.hgt
- include "budget.h"
- void main ( void )
- float amount
- cout ltlt "Enter the main office's budget request
" - cin gtgt amount
- BudgetmainOffice(amount)
-
8Friends of Classes
- A friend is a function that is not a member of a
class, but has access to the private members of
the class. - The syntax of a friend function is as follows and
is included in the .h file for the class - friend ltreturn typegtltfunction namegt(ltparameter
type listgt)
9 auxil.h Program
- ifndef AUXIL_H
- define AUXIL_H
- class Budget // Forward declaration of Budget
class - class Aux
- private
- float auxBudget
- public
- Aux ( void ) auxBudget 0
- void addBudget ( float, Budget )
- float getDivBudget ( void ) return auxBudget
- // Aux
- endif
10 budget.h Program
- ifndef BUDGET_H
- define BUDGET_H
- include "auxil.h"
- class Budget
- private
- static float corpBudget
- float divBudget
- public
- Budget ( void ) divBudget 0
- void addBudget ( float b )
- divBudget b corpBudget divBudget
- float getDivBudget ( void ) return
divBudget - float getCorpBudget ( void ) return
corpBudget - static void mainOffice ( float )
- friend void AuxaddBudget ( float, Budget
) - // Budget
- endif
11Friend Classes
- As mentioned before, it is possible to make an
entire class a friend of another class. - The Budget class could make the Aux class its
friend with the following declaration friend
class Aux - Note generally a friend is bad as it violates
the encapsulation principle.
12Memberwise Assignment
- The operator may be used to assign one object
to another, or to initialize one object with
another objects data. - By default, each member of one object is copied
to its counterpart in the other object.
13Copy Constructors
- A copy constructor is a special constructor,
called whenever a new object is created and
initialized with another objects data. - Assume the class Set has a member variable as
follows bool member which is dynamically
allocated. - Consider the following declarations
- Set x(5,15) // 15 possible members, 5 is a
member - Set y x // Copy constructor is called
- but is only a shallow copy, so when set y
changes, set x does to
14The Default Copy Constructor
- If a class doesnt have a copy constructor, C
automatically creates a default copy constructor.
- The default copy constructor performs a
memberwise assignment.
15The this Pointer
- this is a special built-in pointer that is
available in any member function. - this contains the address of the object that
called the member function. It is the name for
ME.
16Operator Overloading
- C allows you to redefine how standard operators
work when used with class objects.
17Issues of Operator Overloading
- You can change an operators entire meaning when
you overload it. (But dont.) - You cannot change the number of operands taken by
an operator. For example, the symbol must
always be a binary operator. Likewise, and --
must always be unary operators.
18Operators That can be Overloaded
19- Because our unionSet has same arguments and
return value as , we can simply make a name
change - Set Setoperator(Set arg2)
- Set res int min
- if(memberMaxgtarg2.memberMax)
- minarg2.memberMax
- res new Set(this)
- else
- res new Set(arg2)
- min memberMax
-
- for (int i0 i lt min i)
- res-gtmemberi arg2.memberi memberi
- return res
20Overloading the Prefix Operator
- Set Setoperator(void)
- bool member1 new boolmemberMax1
- for (int i0 i lt memberMax i)
- member1i memberi
- delete member
- member member1
- membermemberMaxfalse
- memberMax
- return this
21Overloading the Postfix Operator
- Set Setoperator(int x)// parameter is never
used! - bool member1 new boolmemberMax1
- for (int i0 i lt memberMax i)
- member1i memberi
- delete member
- member member1
- membermemberMaxfalse
- memberMax
- return this
22Overloading Relational Operators
- bool Setoperatorlt(Set arg2)// subset for us
- int min memberMax
- if (arg2.memberMax lt min)
- min arg2.memberMax
- for(int i0 i ltmini)
- if (memberi !arg2.memberi) return
false - if (memberMax gtmin)
- for (imin i lt memberMaxi)
- if (memberi) return false
- return true
23Overloading the Operator
- bool Setoperator(Set arg2)
- if (memberMax!arg2.memberMax) return false
- for (int i0 i lt memberMaxi)
- if (memberi!arg2.memberi) return false
- return true
24Overloading the ltlt Operator
- Note this cannot be a member function of Set as
the first argument is not a Set - ostream operatorltlt ( ostream strm, Set obj )
- strm ltlt Whatever I want
- strmltlt but be careful about member
variables - strm ltlt Since Im not a member, I cant
access them - // operatorltlt
25Overloading the gtgt Operator
- istream operatorgtgt ( istream strm, Set obj )
- int ele
- strm gtgt ele
- Set.addToSet(x) //or make friend or use
input() member - // operatorgtgt
26Overloading the Operator
- In addition to the traditional operators, C
allows you to change the way the (subscript)
symbols work.
27 intarry.h Program
- ifndef INTARRY_H
- define INTARRY_H
- Class IntArray
- private
- int aptr
- int arraySize
- void memError ( void )
- void subError ( void )
- public
- IntArray ( int )
- IntArray ( const IntArray )
- IntArray ( void )
- int size ( void ) return arraySize
- int operator ( const int )
- // IntArray
- endif
28 intarray.cpp Program
- IntArrayIntArray ( int s )
- arraySize s
- aptr new int s
- if ( aptr NULL )
- memError()
- for ( int count 0 count lt arraySize count
) - ( aptr count ) 0
- // IntArrayIntArray
- IntArrayIntArray ( const IntArray obj )
- arraySize obj.arraySize
- aptr new int arraySize
- if ( aptr NULL )
- memError()
- for ( count 0 count lt arraySize count )
- ( aptr count ) ( obj.aptr count )
- // IntArrayIntArray
29 intarray.cpp Program (cont)
- IntArrayIntArray ( void )
- if ( arraySize gt 0 )
- delete aptr
- // IntArrayIntArray
- void IntArraymemError ( void )
- cout ltlt "ERROR Cannot allocate memory.\n"
- exit( 1 )
- IntArraymemError
30 intarray.cpp Program (cont)
- void IntArraysubError ( void )
- cout ltlt "ERROR Subscript out of range.\n"
- exit( 1 )
- // IntArraysubError
- int IntArrayoperator ( const int sub )
- if ( sub lt 0 sub gt arraySize )
- subError()
- return aptrsub
- // IntArrayoperator
31 Object Conversion via overloading
- Special operator functions may be written to
convert a class object to any other type. - FeetInchesoperator float ( void )
- float temp feet
- temp (inches / 12.0)
- return temp
- // FeetInchesoperator float
32Note
- No return type is specified in the function
header for the previous example. - Because it is a FeetInches-to-float conversion
function, it will always return a float.