Lecture 9: Friend functions and overloading operators  Software Engineering:

advertisement
Lecture 9: Friend functions and
overloading operators
 Software Engineering:



Friend functions
Overloading operators
Array of objects
Page 1
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
• 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.
Page 2
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Friends can access private members of a class
#include <iostream.h>
void setX(Cout &c, int val)
Class Count{
{ c.x =val; }
friend void setX(Count &, int);
int main()
//friend declaration
{ Count counter(0);
public:
Count(int y) {x=y;} //constructor
cout<< “counter.x after
instantiation:”;
void print() {cout<<x<<“\n”;}
counter.print();
private:
int x;
};
cout<<“counter.x after call to setX:”;
setX(counter, 8);
// set x with a friend
counter.print();
counter.x after instantiation: 0
Return 0;
counter.x after call to setX: 8
}
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Page 3
• 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.
Page 4
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Class Time {
void main() {
public:
Time x1, x2, x3;
int hours, minutes, second;
Time operator+(const Time &, const Time &);
//overloading +
x1.hours=0;
x1.minutes=10;
};
Time Time:operator+(const Time & x, const Time & y)
x1.seconds=20;
{
x2.hours=1;
Time z;
x2.minutes=11;
z.hours=x.hours+y.hours;
z.minutes=x.minutes+y.minutes;
x2.seconds=11;
z.seconds=x.seconds+y.seconds;
x3=x1+x2;
return z;
}
}
2016/5/29
Page 5
CS3369 Real Time Control
Software/WANG Lusheng
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 ;
}
Page 6
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Class Time {
Time::Time(int x, int y, int z)
public:
int hours, minutes, seconds;
{ hours=x; minutes=y;
seconds=z;}
Time operator+(const Time &, const Time &);
void main() {
Time(int x, int y, int z);//constructor
Time x1(0, 10, 20), x2(1, 11,
11) , x3;
//overloading +
//x1.hours=0;
};
//x1.minutes=10;
Time Time:operator+(const Time & x, const Time & y)
{
//x2.hours=1;
Time z;
//x2.minutes=11;
z.hours=x.hours+y.hours;
//x2.seconds=11;
z.minutes=x.minutes+y.minutes;
x3=x1+x2;
z.seconds=x.seconds+y.seconds;
}
return z;
2016/5/29
}
//x1.seconds=20;
Page 7
CS3369 Real Time Control
Software/WANG Lusheng


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
};
Page 8
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Implement Constructor and Output for Money
 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;
}
Page 9
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Overloading operators
 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.
Page 10
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
An Example of overloading “+”
 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;
 }
Page 11
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
 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();
}
Page 12
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Summary of class
 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.
Page 13
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
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 {
Coursework::output()
public:
{ cout<<“Your ass1 is”
<<ass1<<“your ass2 is” <<
ass2<<“the totaal is” <<total
<<“\n”;
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;
}
Page 14
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
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::output()
Coursework x;
{ cout<<“Your ass1 is”
<<ass1<<“your ass2 is” <<
ass2<<“the totaal is” <<total
<<“\n”;
x.input();
x.output();
}
Question 3: How about two objects?
}
void main (){
Coursework x, y;
x.input(); x.output();
y.input(); y.output();
}
2016/5/29
Page 15
CS3369 Real Time Control
Software/WANG Lusheng
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);
Question 5: How to use the
construct?
void input();
Void main() {
void output(); };
Coursework x(100, 99),y;
Coursework::Coursework(int a1, int a2)
x.output();
{ ass1=a1;
y.input();
ass2=a3;
y.output();
total=ass1+ass2;
}
}
Page 16
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
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);
Coursework::Coursework(int
a1, int a2)
{ ass1=a1;
void input();
ass2=a3;
void output();
total=w1*ass1+w2*ass2;
private:
}
float w1, w2;
};
Page 17
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
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();
Coursework operator +(const Coursework& x,
const Coursework& y);
{ Coursework temp;
temp.ass1=x.ass1+y.ass1;
private:
temp.ass2=x.ass2+y.ass2;
float w1, w2;
temp.total=x.total+y.total;
};
temp.w1=x.w1;
temp.w2=x.w2;
return temp;
}
Page 18
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
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();
}
Page 19
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
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 setX(Cout &c, int val)
{ c.x =val; }
int main()
{ Count counter(0), c1(1);
cout<< “counter.x after instantiation:”;
void print() {cout<<x<<“\n”;}
private:
int x;
};
counter.print();
cout<<“counter.x after call to setX:”;
setX(counter, 8);
// set x with a friend
counter.print();
c1.print();
Return 0;
}
Page 20
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Exercise 2: Give the output of the following program.
#include <iostream.h>
Void setX(Cout &c, int val)
Class Count{
{ c.x =val; }
friend void setX(Count &, int);
public:
Count(int y) {x=y;} //constructor
Count counter(9);
int main()
{ Count counter(0), c1(1);
void print() const {cout<<x<<“\n”;}
private:
int x;
};
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;
}
Page 21
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Exercise 3: Give the output of the following program.
#include <iostream.h>
Class Count{
friend void setX(Count &, int);
public:
void setX(Cout &c, int val)
{ c.x =val; }
Count counter(9);
Count(int y) {x=y;} //constructor
void f() {counter.print();}
void print() const {cout<<x<<“\n”;}
int main()
private:
{ Count counter(0), c1(1);
f();
int x;
counter.print();
};
setX(counter, 8);
// set x with a friend
counter.print();
c1.print();
Return 0;
}
2016/5/29
CS3369 Real Time Control
Software/WANG Lusheng
Page 22
Download