1 / 36

CSE 2341 - Honors Principles of Computer Science I

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 4. Quick Look. Static vs. Local Variables Friendship Memberwise Assignment to copy two objects. Static Member. If a member is declared static

Download Presentation

CSE 2341 - Honors Principles of Computer Science I

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. CSE 2341 - HonorsPrinciples of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 4

  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; } x 5 a y 6 x 5 b y 6

  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; } p 10 x 5 a y 6 x 5 b y 6

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

  11. 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.

  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 <iostream> #include <iomanip> #include "budget2.h"//For Budget class declarationusing 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: 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

  18. Friends of Classes

  19. friend <retType><function name>(<paramList>); • Friend Function • Not a member function of the class • has access to the private data members of the class

  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 <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]; 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: 100000 Enter the budget request for Division 1: 100000 Enter the budget request for Division 1's auxiliary office: 50000 Enter the budget request for Division 2: 200000 Enter the budget request for Division 2's auxiliary office: 40000 Enter the budget request for Division 3: 300000 Enter the budget request for Division 3's auxiliary office: 70000 Enter the budget request for Division 4: 400000 Enter the budget request for Division 4's auxiliary office: 65000

  29. Example with Friendship Here are the division budget requests: Division 1: $100000.00 Auxiliary office of Division 1: $50000.00 Division 2: $200000.00 Auxiliary office of Division 2: $40000.00 Division 3: $300000.00 Auxiliary office of Division 3: $70000.00 Division 4: $400000.00 Auxiliary office of Division 4: $65000.00 Total Requests (including main office): $ 1325000.00

  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 <iostream> 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; } Box1: Box2: ? ? 10 20 Before:

  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; } Box1: Box2: 10 20 10 20 After:

  36. ?

More Related