logging in or signing up lec9 aksu Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 36 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: December 29, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Lecture 9: Friend functions and overloading operators : Lecture 9: Friend functions and overloading operators Software Engineering: Friend functions Overloading operators Array of objects Slide2: A friend function of a class is defined outside that class’s scope, yet has the right to access private members of the class. Friend functions enhances performance. For instance, friend functions can be used to define overloading operators. An example will be given after overloading operators are discussed.Slide3: Friends can access private members of a class #include <iostream.h> Class Count{ friend void setX(Count &, int); //friend declaration public: Count(int y) {x=y;} //constructor void print() {cout<<x<<“\n”;} private: int x; }; counter.x after instantiation: 0 counter.x after call to setX: 8 void setX(Cout &c, int val) { c.x =val; } int main() { Count counter(0); cout<< “counter.x after instantiation:”; counter.print(); cout<<“counter.x after call to setX:”; setX(counter, 8); // set x with a friend counter.print(); Return 0; }Slide4: C++ is a type-sensitive and type-focused operators -- +, - *, / etc. Y=15+3; or x=16-x; operators with user-defined types can also be used. object3=object1 +object2; no new operator can be created. most of the existing operators can be overloaded so that when applied to class, they mean appropriately to the class. Slide5: Class Time { public: int hours, minutes, second; Time operator+(const Time &, const Time &); //overloading + }; Time Time:operator+(const Time & x, const Time & y) { Time z; z.hours=x.hours+y.hours; z.minutes=x.minutes+y.minutes; z.seconds=x.seconds+y.seconds; return z; } void main() { Time x1, x2, x3; x1.hours=0; x1.minutes=10; x1.seconds=20; x2.hours=1; x2.minutes=11; x2.seconds=11; x3=x1+x2; } Slide6: Another choice: define “+” as a friend operator Class Time { friend Time operator+(const Time &, const Time &); //overloading + public: int hours, minutes, second; }; Time operator+(const Time & x, const Time & y) { Time z; z.hours=x.hours+y.hours; z.minutes=x.minutes+y.minutes; z.seconds=x.seconds+y.seconds; return z ; }Slide7: Class Time { public: int hours, minutes, seconds; Time operator+(const Time &, const Time &); Time(int x, int y, int z);//constructor //overloading + }; Time Time:operator+(const Time & x, const Time & y) { Time z; z.hours=x.hours+y.hours; z.minutes=x.minutes+y.minutes; z.seconds=x.seconds+y.seconds; return z; } Time::Time(int x, int y, int z) { hours=x; minutes=y; seconds=z;} void main() { Time x1(0, 10, 20), x2(1, 11, 11) , x3; //x1.hours=0; //x1.minutes=10; //x1.seconds=20; //x2.hours=1; //x2.minutes=11; //x2.seconds=11; x3=x1+x2; } Slide8: class Money{//an ADT for HK$, in file money.h public: Money(int d,int c); //constructor d-dollars,c-cents; //precondition: d and c are assigned values: c<100; //postcondition: dollars = = d, cents = = c; void give_output(); //precondition: (dollars, cents) given //postcondition: ‘$’ and the amount is printed to screen void get_input(); //input from keyboard //precondition: $amount1.amount2 is typed //postcondition: dollars=amount1, cents=amount2; private: int dollars; int cents; //number of dollars and cents }; Slide9: Money::Money(int d,int c) //in money.cpp {dollars=d; cents=c; }; void Money::give_output() {//in money.cpp cout << “The amount is $”<<dollars<<“.”<<cents; } Implement Constructor and Output for MoneySlide10: An operator is in principle the same as a function. It has some arguments. For example “+” has two arguments, one before it and one after it. We know general functions cannot access private members of a class. The same applies to operators. To overcome this hurdle, we use friend function. friend Money operator +(const Money& x,const Money& y); //define operator “+” for class Money and making private members //of Money available to “+” by declared “+” as its friend operator. //precondition: x and y are assigned values. //postcondition: the sum of dollar values of x and y is returned. Overloading operatorsSlide11: we need to add a line in class Money prototype: friend Money operator +(const Money& x,const Moeny& y) Then the definition: Money operator +(const Money& x,const Moeny& y) {Money tmp; tmp.dollars=x.dollars+y.dollars; tmp.cents=x.cents+y.cents; if {tmp.cents >=100) {tmp.cents=tmp.cents-100; tmp.dollars=tmp.dollars+1;} return tmp; } An Example of overloading “+”Slide12: An array of objects void main(void) { int f[5]={1,1,1,1,1}, k, i,q; int driver = DETECT,mode; Elevator x[5]; char y='0'; for (k=0; k<=4; k++) {x[k].sign=1; x[k].k=0; } initgraph(&driver,&mode,"A:\\bgi"); while(y!='x') { y=read_key(); for (k=0; k<=4; k++) if(f[k]==1){x[k].k=x[k].k+x[k].sign*5;} setcolor(GREEN); for (k=0; k<=4; k++) { x[k].selevator(0+100*k); setcolor(GREEN); } for (k=0; k<=4; k++) { if (x[k].k>=250) x[k].sign=-1; if (x[k].k<=0) x[k].sign=1; } delay (300); for (k=0; k<=4; k++) x[k].ereaseel(0+100*k); } closegraph(); } Slide13: What is a class ? (type of objects) Definition of class -- member variables +member functions public vs private public member variables and functions can be used everywhere private member variables can only be accessed by the member functions of the sane class. Friend functions can access the private member variables overloading existing operators are possible objects are like “variables”, thus we can define an array of objects. We can try to do the same things for objects as for variables, e.g., pointers, etc. We will not mention too many. Summary of class Slide14: A step by step example for different concepts Question 1: define a class named Coursework containing ass1, ass2 and total as public meber variables and input and output as the member functions for input and output. Class Coursework { public: int ass1, ass2, total; void input(); void output(); }; Coursework:: input () { cout<<“Please enter the mark of ass1 \n”; cin >> ass1; cout<<“please enter the mark of ass2 \n”; cin>>ass2; } Coursework::output() { cout<<“Your ass1 is” <<ass1<<“your ass2 is” << ass2<<“the totaal is” <<total <<“\n”; }Slide15: A step by step example for different concepts (continued) Question 2: write a main function to deal with one object called x and input the data and output the data. void main (){ Coursework x; x.input(); x.output(); } Question 3: How about two objects? void main (){ Coursework x, y; x.input(); x.output(); y.input(); y.output(); } Coursework::output() { cout<<“Your ass1 is” <<ass1<<“your ass2 is” << ass2<<“the totaal is” <<total <<“\n”; }Slide16: A step by step example for different concepts (continued) Question 4: define the “constructor” Class Coursework { public: int ass1, ass2, total; Coursework(int a1, int a2); void input(); void output(); }; Coursework::Coursework(int a1, int a2) { ass1=a1; ass2=a3; total=ass1+ass2; } Question 5: How to use the construct? Void main() { Coursework x(100, 99),y; x.output(); y.input(); y.output(); } Slide17: A step by step example for different concepts (continued) Question 6: define two private variables w1 and w2. Class Coursework { public: int ass1, ass2, total; Coursework(int a1, int a2); void input(); void output(); private: float w1, w2; }; Coursework::Coursework(int a1, int a2) { ass1=a1; ass2=a3; total=w1*ass1+w2*ass2; }Slide18: A step by step example for different concepts (continued) Question 7: overload the operator “+” for the class. Class Coursework { friend Coursework operator +(const Coursework& x, const Coursework& y); public: int ass1, ass2, total; Coursework(int a1, int a2); void input(); void output(); private: float w1, w2; }; Coursework operator +(const Coursework& x, const Coursework& y); { Coursework temp; temp.ass1=x.ass1+y.ass1; temp.ass2=x.ass2+y.ass2; temp.total=x.total+y.total; temp.w1=x.w1; temp.w2=x.w2; return temp; } Slide19: A step by step example for different concepts (continued) Question 8: How to use the overloaded operator “+”? void main() Coursework x(100, 99), y(99,100), z; z=x+y; z.output(); } Slide20: Exercise 1: Give the output of the following program. #include <iostream.h> Class Count{ friend void setX(Count &, int); public: Count(int y) {x=y;} //constructor void print() {cout<<x<<“\n”;} private: int x; }; Void setX(Cout &c, int val) { c.x =val; } int main() { Count counter(0), c1(1); cout<< “counter.x after instantiation:”; counter.print(); cout<<“counter.x after call to setX:”; setX(counter, 8); // set x with a friend counter.print(); c1.print(); Return 0; }Slide21: Exercise 2: Give the output of the following program. #include <iostream.h> Class Count{ friend void setX(Count &, int); public: Count(int y) {x=y;} //constructor void print() const {cout<<x<<“\n”;} private: int x; }; Void setX(Cout &c, int val) { c.x =val; } Count counter(9); int main() { Count counter(0), c1(1); cout<< “counter.x after instantiation:”; counter.print(); cout<<“counter.x after call to setX:”; setX(counter, 8); // set x with a friend counter.print(); c1.print(); Return 0; }Slide22: Exercise 3: Give the output of the following program. #include <iostream.h> Class Count{ friend void setX(Count &, int); public: Count(int y) {x=y;} //constructor void print() const {cout<<x<<“\n”;} private: int x; }; void setX(Cout &c, int val) { c.x =val; } Count counter(9); void f() {counter.print();} int main() { Count counter(0), c1(1); f(); counter.print(); setX(counter, 8); // set x with a friend counter.print(); c1.print(); Return 0; } You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
lec9 aksu Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 36 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: December 29, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Lecture 9: Friend functions and overloading operators : Lecture 9: Friend functions and overloading operators Software Engineering: Friend functions Overloading operators Array of objects Slide2: A friend function of a class is defined outside that class’s scope, yet has the right to access private members of the class. Friend functions enhances performance. For instance, friend functions can be used to define overloading operators. An example will be given after overloading operators are discussed.Slide3: Friends can access private members of a class #include <iostream.h> Class Count{ friend void setX(Count &, int); //friend declaration public: Count(int y) {x=y;} //constructor void print() {cout<<x<<“\n”;} private: int x; }; counter.x after instantiation: 0 counter.x after call to setX: 8 void setX(Cout &c, int val) { c.x =val; } int main() { Count counter(0); cout<< “counter.x after instantiation:”; counter.print(); cout<<“counter.x after call to setX:”; setX(counter, 8); // set x with a friend counter.print(); Return 0; }Slide4: C++ is a type-sensitive and type-focused operators -- +, - *, / etc. Y=15+3; or x=16-x; operators with user-defined types can also be used. object3=object1 +object2; no new operator can be created. most of the existing operators can be overloaded so that when applied to class, they mean appropriately to the class. Slide5: Class Time { public: int hours, minutes, second; Time operator+(const Time &, const Time &); //overloading + }; Time Time:operator+(const Time & x, const Time & y) { Time z; z.hours=x.hours+y.hours; z.minutes=x.minutes+y.minutes; z.seconds=x.seconds+y.seconds; return z; } void main() { Time x1, x2, x3; x1.hours=0; x1.minutes=10; x1.seconds=20; x2.hours=1; x2.minutes=11; x2.seconds=11; x3=x1+x2; } Slide6: Another choice: define “+” as a friend operator Class Time { friend Time operator+(const Time &, const Time &); //overloading + public: int hours, minutes, second; }; Time operator+(const Time & x, const Time & y) { Time z; z.hours=x.hours+y.hours; z.minutes=x.minutes+y.minutes; z.seconds=x.seconds+y.seconds; return z ; }Slide7: Class Time { public: int hours, minutes, seconds; Time operator+(const Time &, const Time &); Time(int x, int y, int z);//constructor //overloading + }; Time Time:operator+(const Time & x, const Time & y) { Time z; z.hours=x.hours+y.hours; z.minutes=x.minutes+y.minutes; z.seconds=x.seconds+y.seconds; return z; } Time::Time(int x, int y, int z) { hours=x; minutes=y; seconds=z;} void main() { Time x1(0, 10, 20), x2(1, 11, 11) , x3; //x1.hours=0; //x1.minutes=10; //x1.seconds=20; //x2.hours=1; //x2.minutes=11; //x2.seconds=11; x3=x1+x2; } Slide8: class Money{//an ADT for HK$, in file money.h public: Money(int d,int c); //constructor d-dollars,c-cents; //precondition: d and c are assigned values: c<100; //postcondition: dollars = = d, cents = = c; void give_output(); //precondition: (dollars, cents) given //postcondition: ‘$’ and the amount is printed to screen void get_input(); //input from keyboard //precondition: $amount1.amount2 is typed //postcondition: dollars=amount1, cents=amount2; private: int dollars; int cents; //number of dollars and cents }; Slide9: Money::Money(int d,int c) //in money.cpp {dollars=d; cents=c; }; void Money::give_output() {//in money.cpp cout << “The amount is $”<<dollars<<“.”<<cents; } Implement Constructor and Output for MoneySlide10: An operator is in principle the same as a function. It has some arguments. For example “+” has two arguments, one before it and one after it. We know general functions cannot access private members of a class. The same applies to operators. To overcome this hurdle, we use friend function. friend Money operator +(const Money& x,const Money& y); //define operator “+” for class Money and making private members //of Money available to “+” by declared “+” as its friend operator. //precondition: x and y are assigned values. //postcondition: the sum of dollar values of x and y is returned. Overloading operatorsSlide11: we need to add a line in class Money prototype: friend Money operator +(const Money& x,const Moeny& y) Then the definition: Money operator +(const Money& x,const Moeny& y) {Money tmp; tmp.dollars=x.dollars+y.dollars; tmp.cents=x.cents+y.cents; if {tmp.cents >=100) {tmp.cents=tmp.cents-100; tmp.dollars=tmp.dollars+1;} return tmp; } An Example of overloading “+”Slide12: An array of objects void main(void) { int f[5]={1,1,1,1,1}, k, i,q; int driver = DETECT,mode; Elevator x[5]; char y='0'; for (k=0; k<=4; k++) {x[k].sign=1; x[k].k=0; } initgraph(&driver,&mode,"A:\\bgi"); while(y!='x') { y=read_key(); for (k=0; k<=4; k++) if(f[k]==1){x[k].k=x[k].k+x[k].sign*5;} setcolor(GREEN); for (k=0; k<=4; k++) { x[k].selevator(0+100*k); setcolor(GREEN); } for (k=0; k<=4; k++) { if (x[k].k>=250) x[k].sign=-1; if (x[k].k<=0) x[k].sign=1; } delay (300); for (k=0; k<=4; k++) x[k].ereaseel(0+100*k); } closegraph(); } Slide13: What is a class ? (type of objects) Definition of class -- member variables +member functions public vs private public member variables and functions can be used everywhere private member variables can only be accessed by the member functions of the sane class. Friend functions can access the private member variables overloading existing operators are possible objects are like “variables”, thus we can define an array of objects. We can try to do the same things for objects as for variables, e.g., pointers, etc. We will not mention too many. Summary of class Slide14: A step by step example for different concepts Question 1: define a class named Coursework containing ass1, ass2 and total as public meber variables and input and output as the member functions for input and output. Class Coursework { public: int ass1, ass2, total; void input(); void output(); }; Coursework:: input () { cout<<“Please enter the mark of ass1 \n”; cin >> ass1; cout<<“please enter the mark of ass2 \n”; cin>>ass2; } Coursework::output() { cout<<“Your ass1 is” <<ass1<<“your ass2 is” << ass2<<“the totaal is” <<total <<“\n”; }Slide15: A step by step example for different concepts (continued) Question 2: write a main function to deal with one object called x and input the data and output the data. void main (){ Coursework x; x.input(); x.output(); } Question 3: How about two objects? void main (){ Coursework x, y; x.input(); x.output(); y.input(); y.output(); } Coursework::output() { cout<<“Your ass1 is” <<ass1<<“your ass2 is” << ass2<<“the totaal is” <<total <<“\n”; }Slide16: A step by step example for different concepts (continued) Question 4: define the “constructor” Class Coursework { public: int ass1, ass2, total; Coursework(int a1, int a2); void input(); void output(); }; Coursework::Coursework(int a1, int a2) { ass1=a1; ass2=a3; total=ass1+ass2; } Question 5: How to use the construct? Void main() { Coursework x(100, 99),y; x.output(); y.input(); y.output(); } Slide17: A step by step example for different concepts (continued) Question 6: define two private variables w1 and w2. Class Coursework { public: int ass1, ass2, total; Coursework(int a1, int a2); void input(); void output(); private: float w1, w2; }; Coursework::Coursework(int a1, int a2) { ass1=a1; ass2=a3; total=w1*ass1+w2*ass2; }Slide18: A step by step example for different concepts (continued) Question 7: overload the operator “+” for the class. Class Coursework { friend Coursework operator +(const Coursework& x, const Coursework& y); public: int ass1, ass2, total; Coursework(int a1, int a2); void input(); void output(); private: float w1, w2; }; Coursework operator +(const Coursework& x, const Coursework& y); { Coursework temp; temp.ass1=x.ass1+y.ass1; temp.ass2=x.ass2+y.ass2; temp.total=x.total+y.total; temp.w1=x.w1; temp.w2=x.w2; return temp; } Slide19: A step by step example for different concepts (continued) Question 8: How to use the overloaded operator “+”? void main() Coursework x(100, 99), y(99,100), z; z=x+y; z.output(); } Slide20: Exercise 1: Give the output of the following program. #include <iostream.h> Class Count{ friend void setX(Count &, int); public: Count(int y) {x=y;} //constructor void print() {cout<<x<<“\n”;} private: int x; }; Void setX(Cout &c, int val) { c.x =val; } int main() { Count counter(0), c1(1); cout<< “counter.x after instantiation:”; counter.print(); cout<<“counter.x after call to setX:”; setX(counter, 8); // set x with a friend counter.print(); c1.print(); Return 0; }Slide21: Exercise 2: Give the output of the following program. #include <iostream.h> Class Count{ friend void setX(Count &, int); public: Count(int y) {x=y;} //constructor void print() const {cout<<x<<“\n”;} private: int x; }; Void setX(Cout &c, int val) { c.x =val; } Count counter(9); int main() { Count counter(0), c1(1); cout<< “counter.x after instantiation:”; counter.print(); cout<<“counter.x after call to setX:”; setX(counter, 8); // set x with a friend counter.print(); c1.print(); Return 0; }Slide22: Exercise 3: Give the output of the following program. #include <iostream.h> Class Count{ friend void setX(Count &, int); public: Count(int y) {x=y;} //constructor void print() const {cout<<x<<“\n”;} private: int x; }; void setX(Cout &c, int val) { c.x =val; } Count counter(9); void f() {counter.print();} int main() { Count counter(0), c1(1); f(); counter.print(); setX(counter, 8); // set x with a friend counter.print(); c1.print(); Return 0; }