OOP-2009 - 12 - Friend Class
OOP-2009 - 12 - Friend Class
234 LECTURE 12
Today:
Friend functions /
classes
Textbook: p. 200-203
1
Friend Functions/Classes
friends allow
functions/classes access
to private data of other
classes.
2
Friend Functions/Classes
Friend functions
A 'friend'
'friend function has access to all 'private'
members of the class for which it is a
'friend'.
'friend
To declare a 'friend'
'friend function, include its
prototype within the class, preceding it with
the C++ keyword 'friend'.
'friend
3
Sample application that can benefit from friend classes/functions
COLLISION PROBLEM
APPROACHES: SIMPLE RECTANGLE , SHRUNKEN RECT,
SPRITE IMAGE
Friend
function
class TANK
Class Wall
class TANK
Class Wall
Data members
of class Tank Function(s) permitted
to manipulate the
data members of
class Tank
6
COLLISION PROBLEM
SIMPLE RECTANGLE APPROACH
How to detect
collision between
objects?
Overlapping
region
7
COLLISION PROBLEM
SPRITE IMAGE APPROACH
How to detect
collision between
objects?
How to detect
collision between
objects?
• Fast
• Simple to implement
• perfect for a game Overlapping
with many moving objects region
9
COLLISION PROBLEM
Shrunken
rectangle
(wx1,wy1)
(x1,y1)
TANK
(x2,y2)
(wx2,wy2)
Non-rectangular object
10
COLLISION PROBLEM
Sample Collision Cases:
(wx1,wy1)
TANK
(x1,y1)
TANK
TANK
(wx2,wy2)
(x2,y2)
TANK
11
COLLISION PROBLEM
Sample Collision Cases: Note: This example is
only using the device
(t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) system of coordinates
and is therefore not
(x1,y1) scalable.
(x1,y1)
LEDGE
TANK
(x2,y2)
(x2,y2)
if( ((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) )
12
COLLISION PROBLEM
Sample Collision Cases: Note: This example is
only using the device
(t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) system of coordinates
(x1,y1) and is therefore not
(x1,y1) scalable.
TANK
LEDGE (x2,y2)
(x2,y2)
if( ((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) )
13
COLLISION PROBLEM
Sample Collision Cases: Note: This example is
only using the device
(t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) system of coordinates
and is therefore not
(x1,y1) scalable.
(x1,y1) LEDGE
TANK (x2,y2)
(x2,y2)
if( ((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) )
14
COLLISION PROBLEM
Sample Collision Cases: Note: This example is
only using the device
(t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) system of coordinates
and is therefore not
(x1,y1) scalable.
(x1,y1)
TANK
LEDGE
(x2,y2)
(x2,y2)
if( ((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x1 >= ledge.x1) && (t.x1 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y1 >= ledge.y1) && (t.y1 <= ledge.y2)) ||
((t.x2 >= ledge.x1) && (t.x2 <= ledge.x2) && (t.y2 >= ledge.y1) && (t.y2 <= ledge.y2)) )
15
Friend Functions/Classes
class T {
• Global function a() can
public: access private data in T
friend void a();
int m(); • m() can access private data
private: // ...
}; in S
void Demo::print(){
cout << endl << "This is x " << x << endl;
cout << "This is y " << y << endl;
}
public:
int getYear(){return year;}
Date( int d=1, int m=1, int y=1970 ){ // do not use initialiser list in constructor
void setYear( int y ){
setMonth( m); // as here we want to reuse the checks
setDay(d); // in the set methods // no restrictions placed on year
setYear(y); // remember month is used to verify day validity year = y;
} }
~Date(){} // no special needs for destructor
void print(){
Date( const Date & date ){ // supply an explicit copy constructor cout << day << " of " << MonthNames[month] << ", " << year << endl;
day = date.day;
month = date.month; }
year = date.year;
} private:
int day;
int getDay(){ return day; }
int month;
void setDay( int d ){
int year;
if( d > 0 && d <= DaysInMonth[month] ){
day = d; };
}
else{
cerr << "Invalid Day value " << d << endl; • Continued on next slide…
exit(1); 18
}
}
Friend Functions/Classes
bool before( Date & d1, Date & d2 ); // prototype
The End.
20
Friend Functions/Classes
Questions
21
Friend Functions/Classes
The following questions pertain to a class called Circle.
a) The only element in a circle considered unique is the radius. Write
one line of code that declares the necessary data member.
b) In which section of the class does this data member get put?
c) Write the prototype for the constructor which takes a radius as its
parameter.
e) Write the function definition for the member function Area which
computes the area of a Circle object. (Just use 3.14159 for Pi. And
remember that area of a circle of radius R is given by: R2.)
22
Friend Functions/Classes
Next:
Operators overloading
Textbook: p.203-216
23