0% found this document useful (0 votes)
36 views20 pages

OOP Lab 8

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

OOP Lab 8

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

Static and Friend

Static
”Student ”using static
class student
{
int id;
string name;
public:
void main()
static int st_num; {
student(int id, string name) student s1( 1, “ali”);
{ cout<< student:: st_num <<endl;
st_num++;
student s2 ( 5 , “ahmed”);
this->id= id; cout<< student:: st_num <<endl;
this->name= name;
} student s3( 3, “mohamed”);
}; cout<< student:: st_num <<endl;
}
int student :: st_num=0;
Student ”using static” (cont.)
class student
{ int student :: st_num=0;
int id;
string name;
static int st_num;
public: void main()
student(int id, string name) {
student s1( 1, “ali”);
{
cout<< student:: getStNum() <<endl;
st_num++;
this->id= id; student s2( 2 , “ahmed”);
this->name= name; cout<< student:: getStNum() <<endl;
}
student s3( 3, “mohamed”);
static int getStNum()
cout<< student:: getStNum() <<endl;
{
return st_num; }
}
}; // end of student class
Exercise 1
A corporation has three divisions, each responsible for sales to different
geographic locations. Design a DivSales class that keeps sales data for a
division, with the following
data members:
first_half, second_half, and static variable total_sales
The class also contains the following member functions:
• A member function that takes two arguments, each assumed to be the
sales for a half. The value of the arguments should be copied into the
variables hold the sales data. The total of the two arguments should
be added to the static variable that holds the total yearly corporate
sales.
Write a program that creates an array of three DivSales objects. The
program should ask the user to enter the sales for the two half for each
division. After the data are entered, the program should display a table
showing the division sales for each half. The program should then display
the total corporate sales for the year.
Friend Function
 A friend is a way to contain similar methods and ideas that

are outside the scope of the original class, but are reliant on

that class’ data.

 The declaration of friend function should be made inside the

body of class (can be anywhere inside class either in private

or public section) starting with keyword friend.


Example ”Single friend function”
class Battery{
void swapEmergency(Battery & s1)
int minPER, recentPER, temp;
{
if( s1.recentPER < s1.minPER )
public:
s1.recentPER = s1.temp;
Battery( int minPER , int recentPER)
}
{
temp= 20;
this-> minPER = minPER;
this-> recentPER = recentPER; void main()
}
{
Battery b1;
// do set and get for minPER, recentPER
swapEmergency ( b1 );
friend void swapEmergency(Battery&); cout << b1.getRecentPER( ) ;
}; }
Friend Function
 Non-member function.
 Have access to private members of a class.
 Friend to more than one class.
 Class gives the permission of friend not the function.
? Why do we use friend
Example ”using member function”
class square class rect
{ {
int side; int width, height;
public: public:
square(int a) rect (int a, int b)
{ {
side = a; width = a;
} height = b;
}
int sArea() int rArea()
{ {
return ( side * side ); return ( width * height );
} }
}; };
Example (cont.)

int addArea (square s, rect r)


{
return ( s.sArea() + r.rArea() );
}

void main()
{
square sq(5);
rect rec(3,5);
cout << addArea( sq , rec) <<endl;
}
Example ”using set & get”
class square class rect
{ {
int side; int width, height;
public: public:
square(int a) rect (int a, int b)
{ {
side = a; width = a;
} height = b;
int getSide() }
{ int getWidth() { return width; }
return side; int getHeight() { return height; }
} };
};
Example ”using set & get” (cont.)

int addArea (square s, rect r)


{
return ( s.getSide() * s.getSide() + r.getWidth() * r.getHeight() );
}

void main()
{
square sq(5);
rect rec(3,5);
cout << addArea( sq , rec) <<endl;
}
Friend Function
 This situation arises mostly in case of operator overloading.
 When you want to compare two private data members of two
different classes. In that case you create a normal function
and make friend in both the classes, as to provide access of
theirs private variables.
”Example ”using multiple friend

class rect;
class rect
class square {
{ int width, height;
int side; public:
public: rect (int a, int b)
square(int a) {
{ width = a;
side = a; height = b;
} }
friend int addArea(square s, rect r); friend int addArea(square s, rect r);
}; };
Example ”using multiple friend” (cont.)

int addArea (square s, rect r)


{
return ( s.side * s.side + r.width * r.height );
}

void main()
{
square sq(5);
rect rec(3,5);
cout << addArea( sq , rec) <<endl;
}
Exercises 2
 Use operator overloading instead of addArea.

friend int operator+ (square s, rect r)


Exercises 3
 Find the largest area of rectangle or square
and print it using friend function.

friend void largest (square s, rect r)


class Storage
Friend Class
{ class Display
private: {
double m_dValue; private:
public: bool m_display;
Storage(double dValue)
public:
{ Display(bool display) { m_display =
m_dValue = dValue; display; }
}
friend class Display; void displayItem(Storage storage)
}; {
if(m_display)
cout << storage.m_dValue << endl;
)(void main
{ }
;Storage st(5, 6.7) };
;Display dis(true)

;dis.displayItem(st)
}

You might also like