1 CSE 2341 Object Oriented Programming with C++ Note Set #7
2 Quick Look Static vs. Local Variables Friendship Memberwise Assignment to copy two objects
3 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
4 Example – with pictures class myCls { private: int x, y; public: myCls(){x = 5; y = 6;} }; int main() { myCls a; myCls b; return 0; } a b x y x y
5 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; } a b x y x y p 10
6 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; } budget.h
7 Example Class With Static Member float GetDivBudget(void) { return DivBudget; } float GetCorpBudget(void) { return CorpBudget; } }; //definition of static data member double Budget::CorpBudget = 0; #endif budget.h
8 Example Class With Static Member #include #include 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); } budgetDriver.cpp
9 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; } budgetDriver.cpp
10 Example Class With Static Member Enter the budget request for Division 1: Enter the budget request for Division 2: Enter the budget request for Division 3: Enter the budget request for Division 4: Here are the division budget requests: Division 1$ Division 2$ Division 3$ Division 4$ Total Budget Requests:$
11 Static Member Functions 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. static ( );
12 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; } budget2.h
13 Example Class With Static Member float GetDivBudget(void) { return DivBudget; } float GetCorpBudget(void) { return CorpBudget; } static void mainOffice(float); }; #endif budget2.h
14 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; } budget2.cpp
15 Example Class With Static Member //Demonstrates static member function #include #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); budgetDriver2.cpp
16 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; } budgetDriver2.cpp
17 Example Class With Static Member cout << "\tTotal Requests (including main office):” << “ $ "; cout << divisions[0].getCorpBudget() << endl; return 0; } budgetDriver2.cpp Enter the main office's budget request: Enter the budget request for Division 1: Enter the budget request for Division 2: Enter the budget request for Division 3: Enter the budget request for Division 4: Here are the division budget requests: Division 1$ Division 2$ Division 3$ Division 4$ Total Requests (including main office): $
18 Friends of Classes
19 Friend Function –Not a member function of the class –has access to the private data members of the class friend ( );
20 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 auxil.h
21 Friendship #include "auxil.h" #include "budget3.h" void AuxiliaryOffice::addBudget(float b, Budget& div) { auxBudget += b; div.corpBudget += b; } auxil.cpp
22 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; } budget3.h
23 Example with Friendship float getDivisionBudget(void) { return divisionBudget; } float getCorpBudget(void) { return corpBudget; } static void mainOffice(float); friend void AuxiliaryOffice::addBudget (float, Budget&); }; #endif budget3.h
24 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; } budget3.cpp
25 Example with Friendship #include #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]; budgetDriver3.cpp
26 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]); } budgetDriver3.cpp
27 Example with Friendship cout << setprecision(2); cout << showpoint << fixed; cout << "Here are the division budget requests:\n"; for (count = 0; count < 4; count++) { cout << "\tDivision " << (count + 1) << "\t\t\t$ "; cout << setw(7); cout << divisions[count].getDivisionBudget() << endl; cout << "\tAuxiliary Office of Division " <<(count+1) << "\t$ "; cout << auxOffices[count].getDivisionBudget() << endl; } cout <<"Total Requests (including main office): $ "; cout << divisions[0].getCorpBudget() << endl; return 0; } budgetDriver3.cpp
28 Example with Friendship Enter the main office's budget request: Enter the budget request for Division 1: Enter the budget request for Division 1's auxiliary office: Enter the budget request for Division 2: Enter the budget request for Division 2's auxiliary office: Enter the budget request for Division 3: Enter the budget request for Division 3's auxiliary office: Enter the budget request for Division 4: Enter the budget request for Division 4's auxiliary office: 65000
29 Example with Friendship Here are the division budget requests: Division 1:$ Auxiliary office of Division 1:$ Division 2:$ Auxiliary office of Division 2:$ Division 3:$ Auxiliary office of Division 3:$ Division 4:$ Auxiliary office of Division 4:$ Total Requests (including main office): $
30 Classes as Friends Possible to make entire classes friends of another class Budget could make Auxiliary a friend with: friend class AuxiliaryOffice; All member functions of AuxiliaryOffice would have access to all private data members of Budget (even those added in later development)
31 Memberwise Assignment
32 Memberwise Assignment = operator –can be used to assign one objects instance to another object instance –can be used to initialize one object with another objects data –default: each member from one instance is copied to its counterpart in the other instance
33 Memberwise Assignment #include class Rectangle { private: float width; float length; public: void setWidth(float w) { width = w; } void setLength (float l){ length = l; } float getWidth() { return width; } float getLength() { return length; } float GetArea() { return width * length; } };
34 Memberwise Assignment int main() { Rectangle Box1, Box2; Box1.setWidth(10); Box1.setLength(20); cout << "Before the assignment:\n"; cout << "Box 1's Width: " << Box1.getWidth()<< endl; cout << "Box 1's Length: " << Box1.getLength() << endl; cout << "Box 1's Area: " << Box1.getArea() << endl; Box2 = Box1 ; cout << " \n"; cout << "After the assignment:\n"; cout << "Box 2's Width: " << Box2.getWidth() << endl; cout << "Box 2's Length: " << Box2.getLength() << endl; cout << "Box 2's Area: " << Box2.getArea() << endl; } Before: ???? Box1:Box2:
35 Memberwise Assignment int main() { Rectangle Box1, Box2; Box1.setWidth(10); Box1.setLength(20); cout << "Before the assignment:\n"; cout << "Box 1's Width: " << Box1.getWidth()<< endl; cout << "Box 1's Length: " << Box1.getLength() << endl; cout << "Box 1's Area: " << Box1.getArea() << endl; Box2 = Box1 ; cout << " \n"; cout << "After the assignment:\n"; cout << "Box 2's Width: " << Box2.getWidth() << endl; cout << "Box 2's Length: " << Box2.getLength() << endl; cout << "Box 2's Area: " << Box2.getArea() << endl; } After: Box1:Box2:
36 Fini ?