Unformatted text preview:

CSE 2341 Object Oriented Programming with C Note Set 7 1 Quick Look Static vs Local Variables Friendship Memberwise Assignment to copy two objects 2 Static Member If a member is declared static it is shared by all objects of the same class it must be defined outside of the class may be accessed before any instances are created 3 Example with pictures class myCls private int x y public myCls x 5 y 6 int main myCls a myCls b return 0 x ay x by 5 6 5 6 4 Example with pictures class myCls private int x y static int p public myCls x 5 y 6 int myCls p 10 int main myCls a myCls b return 0 x a y x b y 5 p 10 6 5 6 5 budget h Example Class With Static Member ifndef BUDGET H define BUDGET H Budget class declaration class Budget private static float CorpBudget float DivBudget public Budget void DivBudget 0 void AddBudget float B DivBudget B CorpBudget B 6 Example Class With Static Member budget h float GetDivBudget void return DivBudget float GetCorpBudget void return CorpBudget definition of static data member double Budget CorpBudget 0 endif 7 budgetDriver cpp Example Class With Static Member include iostream include iomanip using namespace std include budget h For Budget class int main Budget Divisions 4 for int Count 0 Count 4 Count float Bud cout Enter the budget request for division cout Count 1 cin Bud Divisions Count AddBudget Bud 8 budgetDriver cpp Example Class With Static Member cout precision 2 cout setf ios showpoint ios fixed cout nHere are the division budget requests n for Count 0 Count 4 Count cout tDivision Count 1 t cout Divisions Count GetDivBudget cout endl cout tTotal Budget Requests t cout Divisions 0 GetCorpBudget endl return 0 9 Example Class With Static Member Enter the budget request for Division 1 100000 Enter the budget request for Division 2 200000 Enter the budget request for Division 3 300000 Enter the budget request for Division 4 400000 Here are the division budget requests Division 1 100000 00 Division 2 200000 00 Division 3 300000 00 Division 4 400000 00 Total Budget Requests 1000000 00 10 Static Member Functions static return type function name parameter list callable before any objects are instantiated static member functions can access static data members before any objects are instantiated provides ability for specialized set up routines in an object 11 budget2 h Example Class With Static Member ifndef BUDGET H define BUDGET H Budget class declaration class Budget private static float CorpBudget float DivBudget public Budget DivBudget 0 void AddBudget float B DivBudget B CorpBudget B 12 Example Class With Static Member budget2 h float GetDivBudget void return DivBudget float GetCorpBudget void return CorpBudget static void mainOffice float endif 13 budget2 cpp Example Class With Static Member include budget2 h Definition of static member of Budget class float Budget corpBudget 0 Definition of static member function MainOffice This function adds the main office s budget request to the CorpBudget variable void Budget mainOffice float moffice corpBudget moffice 14 budgetDriver2 cpp Example Class With Static Member Demonstrates static member function include iostream include iomanip include budget2 h For Budget class declaration using namespace std int main int count float mainOfficeRequest const int numDivisions 4 cout Enter the main office s budget request cin mainOfficeRequest Budget Divisions numDivisions Budget mainOffice mainOfficeRequest 15 budgetDriver2 cpp Example Class With Static Member for int Count 0 Count 4 Count float budgetAmount cout Enter the budget request for division cout count 1 cin budgetAmount divisions Count addBudget budgetAmount cout setprecision 2 cout showpoint fixed cout nHere are the division budget requests n for count 0 count 4 count cout tDivision count 1 t cout divisions count getDivisionBudget endl 16 budgetDriver2 cpp Example Class With Static Member cout tTotal Requests including main office cout divisions 0 getCorpBudget endl return 0 Enter the main office s budget request 100000 Enter the budget request for Division 1 100000 Enter the budget request for Division 2 200000 Enter the budget request for Division 3 300000 Enter the budget request for Division 4 400000 Here are the division budget requests Division 1 100000 00 Division 2 200000 00 Division 3 300000 00 Division 4 400000 00 Total Requests including main office 1100000 00 17 Friends of Classes 18 Friend Function Not a member function of the class has access to the private data members of the class friend retType function name paramList 19 auxil h Friendship ifndef AUXIL H define AUXIL H class Budget Forward declaration of Budget class class AuxiliaryOffice private float auxBudget public AuxiliaryOffice auxBudget 0 void addBudget float Budget float getDivisionBudget return auxBudget endif 20 auxil cpp Friendship include auxil h include budget3 h void AuxiliaryOffice addBudget float b Budget div auxBudget b div corpBudget b 21 budget3 h Example with Friendship ifndef BUDGET3 H define BUDGET3 H include auxil h For Aux class declaration class Budget private static float corpBudget float divisionBudget public Budget divisionBudget 0 void addBudget float b divisionBudget b corpBudget b 22 budget3 h Example with Friendship float getDivisionBudget void return divisionBudget float getCorpBudget void return corpBudget static void mainOffice float friend void AuxiliaryOffice addBudget float Budget endif 23 budget3 cpp Example with Friendship include budget3 h Definition of static member of Budget class float Budget corpBudget 0 Definition of static member function MainOffice This function adds the main office s budget request to the CorpBudget variable void Budget mainOffice float moffice corpBudget moffice 24 budgetDriver3 cpp Example with Friendship include iostream include iomanip include budget3 h using namespace std int main int int count float mainOfficeRequest const int numDivisions 4 cout Enter the main office s budget request cin mainOfficeRequest Budget mainOffice mainOfficeRequest Budget divisions numDivisions AuxiliaryOffice auxOffices numDivisions 25 budgetDriver3 cpp Example with Friendship for count 0 count numDivisions count float budgetAmount cout Enter the budget request for division cout count 1 cin budgetAmount divisions count addBudget budgetAmount cout Enter the budget request for division cout count 1 s nauxiliary office cin budgetAmount auxOffices count addBudget budgetAmount divisions Count 26 Example with Friendship budgetDriver3 cpp cout


View Full Document

SMU CSE 2341 - Object Oriented Programming with C++

Loading Unlocking...
Login

Join to view Object Oriented Programming with C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Oriented Programming with C++ and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?