0% found this document useful (0 votes)
40 views23 pages

OOP-2009 - 12 - Friend Class

Uploaded by

visapoy63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views23 pages

OOP-2009 - 12 - Friend Class

Uploaded by

visapoy63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

159.

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

• One class for each moving object


• One instance of a class doesn’t know the boundaries of
other moving objects
• Data members of each object is hidden and protected from
other moving objects
4
COLLISION PROBLEM
Friend functions/class by-pass object data hiding

Friend
function
class TANK
Class Wall

Intermediary function that is


permitted to manipulate the
data members of both Tank
and Wall classes 5
COLLISION PROBLEM
Friend functions/class by-pass object data hiding

Friend to class Tank

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?

• not convincing enough!

Overlapping
region

7
COLLISION PROBLEM
SPRITE IMAGE APPROACH

How to detect
collision between
objects?

• can be a major Overlapping


bottleneck in region
performance to get a
decent animation
8
COLLISION PROBLEM
SHRUNKEN RECTANGLE APPROACH

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 a() {// can access • all functions of T can access


// private data in T...} private data in X

class S { friends should be used with


public: caution:
friend int T::m(); they by-pass C++’s data hiding
//...
}; principle.

class X { It is the responsibility of the


public: code for which access is to
friend class T;
//... be given to say who it’s
}; friends are - i.e. who does
16
it trust!
Friend Functions/Classes
class Demo {
friend void Change( Demo obj );
public:
Demo(double x0=0.0, int y0=0.0):x(x0),y(y0){}
void print();
private:
double x; int y;
};

void Demo::print(){
cout << endl << "This is x " << x << endl;
cout << "This is y " << y << endl;
}

void Change( Demo obj ) {


obj.x += 100;
obj.y += 200;
cout << "This is obj.x" << obj.x << endl;
cout << "This is obj.y" << obj.y << endl;
} 17
Friend Functions/Classes
#include <iostream>
using namespace std; int getMonth(){ return month; }
void setMonth( int m ){
const int DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if( m >= 1 && m <= 12 ){
enum Months { unused, January, February, March, April, May, June,
month = m;
July, August, September, October, November, December };
const char *MonthNames[]={"Unused", "January", "February", "March", "April", "May", }
"June", else{
"July", "August", "September", "October", "November", "December" }; cerr << "Invalid Month value " << m << endl;
exit(1);
class Date{ }
friend bool before( Date & d1, Date & d2 ); }

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

int main(){ // test the Calendar collection


Date today;
• We wanted the global
Date lecture11( 6, 8, 2008 );
today.setMonth( August ); before() function to
today.setDay( 7 );
today.setYear( 2008 );
today.print();
have access to the
lecture11.print();
if( before( lecture11 , today ) ) internals of the Date
cout << "lecture11 was before today" << endl;
Date tomorrow( 8, 8, 2008 );
if( before( tomorrow , today ) )
class
cout << "tomorrow was before today" << endl;
else
cout << "tomorrow was not before today" << endl;
• Date declares it as a
}
return 0;
friend function
// return true if Date1 is before Date2
bool before(
before Date & d1, Date & d2 ){
if( d1.year < d2.year )
return true;
else if( d1.year == d2.year ){ • Example output:
if( d1.month < d2.month )
return true; 7 of August, 2008
else if( d1.month == d2.month ){
if( d1.day < d2.day ) 6 of August, 2008
return true;
} lecture11 was before today
}
return false; tomorrow was not before today 19
}
Friend Functions/Classes

The End.

20
Friend Functions/Classes
Questions

1. Is there any difference between List x; and List x();?


2. Is the default constructor for Fred always Fred::Fred()?
3. Should constructors use "initializer lists" or "assignment"?
4. How do you know when to declare a function as a member
function or a friend function in your class?

From: C++ FAQ Lite


http://www.parashift.com

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.

d) In which section of the class does this constructor get put?

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

You might also like