cse 2341 - honors principles of computer science i
DESCRIPTION
CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. Note Set 4. Quick Look. Static vs. Local Variables Friendship Memberwise Assignment to copy two objects. Static Member. If a member is declared static - PowerPoint PPT PresentationTRANSCRIPT
Static Member
3
If a member is declared static it is shared by all objects of the same classit must be defined outside of the classmay be accessed before any instances are created
Example – with pictures
4
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
6
5
6
Example – with pictures
5
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
5
6
5
6
p 10
Example Class With Static Member
6
#ifndef BUDGET_H#define BUDGET_H// Budget class declarationclass Budget{private: static float CorpBudget; float DivBudget;public:
Budget(void) { DivBudget = 0; }
void AddBudget(float B){
DivBudget += B; CorpBudget += B;
}
budget.h
Example Class With Static Member
7
float GetDivBudget(void) {
return DivBudget; }float GetCorpBudget(void){
return CorpBudget;}
};
//definition of static data memberdouble Budget::CorpBudget = 0;
#endif
budget.h
Example Class With Static Member
8
#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
Example Class With Static Member
9
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
Example Class With Static Member
10
Enter the budget request for Division 1: 100000Enter the budget request for Division 2: 200000Enter the budget request for Division 3: 300000Enter the budget request for Division 4: 400000Here are the division budget requests:
Division 1 $ 100000.00Division 2 $ 200000.00Division 3 $ 300000.00Division 4 $ 400000.00Total Budget Requests: $ 1000000.00
Static Member Functions
11
callable before any objects are instantiatedstatic member functions can access static data members
before any objects are instantiatedprovides ability for specialized set-up routines in an
object.
static <return type><function name>(<parameter list>);
Example Class With Static Member
12
#ifndef BUDGET_H#define BUDGET_H// Budget class declarationclass Budget{private: static float CorpBudget; float DivBudget;public:
Budget() { DivBudget = 0; }
void AddBudget(float B){
DivBudget += B; CorpBudget += B;
}
budget2.h
Example Class With Static Member
13
float GetDivBudget(void) {
return DivBudget; }float GetCorpBudget(void){
return CorpBudget;}static void mainOffice(float);
};
#endif
budget2.h
Example Class With Static Member
14
#include “budget2.h"
// Definition of static member of Budget classfloat 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
Example Class With Static Member
15
//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
Example Class With Static Member
16
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
Example Class With Static Member
17
Enter the main office's budget request: 100000 Enter the budget request for Division 1: 100000Enter the budget request for Division 2: 200000Enter the budget request for Division 3: 300000Enter the budget request for Division 4: 400000Here are the division budget requests:
Division 1 $ 100000.00Division 2 $ 200000.00Division 3 $ 300000.00Division 4 $ 400000.00Total Requests (including main office): $ 1100000.00
cout << "\tTotal Requests (including main office):” << “ $ "; cout << divisions[0].getCorpBudget() << endl; return 0;}
budgetDriver2.cpp
19
Friend FunctionNot a member function of the classhas access to the private data members of the class
friend <retType><function name>(<paramList>);
Friendship
20
#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
Friendship
21
#include "auxil.h"#include "budget3.h"
void AuxiliaryOffice::addBudget(float b, Budget& div){
auxBudget += b;div.corpBudget += b;
}
auxil.cpp
Example with Friendship
22
#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
Example with Friendship
23
float getDivisionBudget(void) { return divisionBudget; } float getCorpBudget(void) { return corpBudget; } static void mainOffice(float); friend void AuxiliaryOffice::addBudget (float, Budget&);};#endif
budget3.h
Example with Friendship
24
#include “budget3.h"
// Definition of static member of Budget classfloat 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
Example with Friendship
25
#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
Example with Friendship
26
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
Example with Friendship
27
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
Example with Friendship
28
Enter the main office's budget request: 100000Enter the budget request for Division 1: 100000Enter the budget request for Division 1'sauxiliary office: 50000Enter the budget request for Division 2: 200000Enter the budget request for Division 2'sauxiliary office: 40000Enter the budget request for Division 3: 300000Enter the budget request for Division 3'sauxiliary office: 70000Enter the budget request for Division 4: 400000Enter the budget request for Division 4'sauxiliary office: 65000
Example with Friendship
29
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.00Total Requests (including main office): $ 1325000.00
Classes as Friends
30
Possible to make entire classes friends of another classBudget 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)
Memberwise Assignment
32
= operator can be used to assign one objects instance to another object
instancecan be used to initialize one object with another objects
datadefault: each member from one instance is copied to its
counterpart in the other instance
Memberwise Assignment
33
#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; }};
Memberwise Assignment
34
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:1020
??
Box1: Box2:
Memberwise Assignment
35
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:1020
1020
Box1: Box2: