Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
94 views16 pages

Object Oriented Design and Programming CS 201: Dr. Deepika Gupta

The document discusses function overloading, constructor overloading, friend functions, and friend classes in C++. It provides examples of how to define overloaded functions using different parameter types or numbers. It also demonstrates how to define friend functions that have access to private members of a class, and how two classes can be friends so their private members are accessible to each other. The document is part of a lecture on object oriented design and programming concepts in C++.

Uploaded by

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

Object Oriented Design and Programming CS 201: Dr. Deepika Gupta

The document discusses function overloading, constructor overloading, friend functions, and friend classes in C++. It provides examples of how to define overloaded functions using different parameter types or numbers. It also demonstrates how to define friend functions that have access to private members of a class, and how two classes can be friends so their private members are accessible to each other. The document is part of a lecture on object oriented design and programming concepts in C++.

Uploaded by

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

Object Oriented Design and Programming

CS 201

Dr. Deepika Gupta

Department of CSE, IIITV-ICD

Sept 23, 2022

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 1 / 14
Lecture No.: 07-08
Function Overloading and
Friend Function

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 2 / 14
Today’s Agenda

1 Function Overloading

2 Overloading Constructor

3 Friend function

4 Friend Classes

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 3 / 14
Function Overloading

Function overloading is the process of using the same name for two or more
functions.

Each redefinition of the function must use either different types of parameters
or a different number of parameters.

These differences make the compiler knows which function to call in any
given situation.

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 4 / 14
Function Overloading

Function overloading is the process of using the same name for two or more
functions.

Each redefinition of the function must use either different types of parameters
or a different number of parameters.

These differences make the compiler knows which function to call in any
given situation.

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 4 / 14
Function Overloading

Function overloading is the process of using the same name for two or more
functions.

Each redefinition of the function must use either different types of parameters
or a different number of parameters.

These differences make the compiler knows which function to call in any
given situation.

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 4 / 14
Function Overloading

1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 i n t myfunc ( i n t i ) ; // t h e s e d i f f e r i n number o f p a r a m e t e r s
4 i n t myfunc ( i n t i , i n t j ) ;
5 i n t main ( )
6 {
7 c o u t << myfunc ( 1 0 ) << " " ; // c a l l s myfunc ( i n t i )
8 c o u t << myfunc ( 4 , 5 ) ; // c a l l s myfunc ( i n t i , i n t j )
9 return 0;
10 }
11 i n t myfunc ( i n t i )
12 {
13 return i ;
14 }
15 i n t myfunc ( i n t i , i n t j )
16 {
17 return i ∗ j ;
18 }

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 5 / 14
Function Overloading: Incorrect Overloading

1 i n t myfunc ( i n t i ) ; // E r r o r : d i f f e r i n g r e t u r n t y p e s a r e
2 f l o a t myfunc ( i n t i ) ; // i n s u f f i c i e n t when o v e r l o a d i n g .

1 v o i d f ( i n t ∗p ) ;
2 v o i d f ( i n t p [ ] ) ; // e r r o r , ∗p i s same a s p [ ]

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 6 / 14
Constructor Overloading
1 #i n c l u d e <i o s t r e a m >
2 #i n c l u d e <c s t d i o >
3 u s i n g namespace s t d ;
4 c l a s s date {
5 i n t day , month , y e a r ;
6 public :
7 d a t e ( c h a r ∗d ) ;
8 d a t e ( i n t m, i n t d , i n t y ) ;
9 v o i d show_date ( ) ;
10 } ;
11 // I n i t i a l i z e u s i n g s t r i n g .
12 d a t e : : d a t e ( c h a r ∗d )
13 {
14 s s c a n f ( d , "%d%∗c%d%∗c%d " , &month , &day , &y e a r ) ;
15 }
16 // I n i t i a l i z e u s i n g i n t e g e r s .
17 d a t e : : d a t e ( i n t m, i n t d , i n t y )
18 {
19 day = d ;
20 month = m;
21 year = y ;
22 }
23 v o i d d a t e : : show_date ( )
24 {
25 c o u t << month << " / " << day ;
26 c o u t << " / " << y e a r << " \n " ;
27 }
28 i n t main ( )
29 {
30 d a t e ob1 ( 1 2 , 4 , 2 0 0 3 ) , ob2 ( " 10/22/2003 " ) ;
31 ob1 . show_date ( ) ;
32 ob2 . show_date ( ) ;
33 return 0;
34 }

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 7 / 14
Friend Function

It is possible to grant a nonmember function access to the private members


of a class by using a friend.

A friend function has access to all private and protected members of the class
for which it is a friend.

To declare a friend function, include its prototype within the class, preceding
it with the keyword friend.

A derived class does not inherit friend functions.

Friend functions may not be declared as static.

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 8 / 14
Friend Function: Example

1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 c l a s s myclass {
4 int a , b;
5 public :
6 f r i e n d i n t sum ( m y c l a s s x ) ;
7 void set_ab ( i n t i , i n t j ) ;
8 };
9 void myclass : : set_ab ( i n t i , i n t j )
10 {
11 a = i;
12 b = j;
13 }
14 // Note : sum ( ) i s n o t a member f u n c t i o n o f any c l a s s .
15 i n t sum ( m y c l a s s x )
16 {
17 /∗ B e c a u s e sum ( ) i s a f r i e n d o f m y c l a s s , i t can
18 d i r e c t l y a c c e s s a and b . ∗/
19 return x . a + x . b ;
20 }
21 i n t main ( )
22 {
23 myclass n ;
24 n . set_ab (3 , 4) ;
25 c o u t << sum ( n ) ;
26 return 0;
27 }

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 9 / 14
Friend Function: Observations

Although there is nothing gained by making sum( ) a friend rather than a


member function of myclass.

There are some circumstances in which friend functions are quite valuable.

First, friends can be useful when you are overloading certain types of
operators (we will discuss with operator overloading).

Second, friend functions make the creation of some types of I/O functions
easier (will discuss with templates).

The third reason that friend functions may be desirable is that in some cases,
two or more classes may contain members that are interrelated relative to
other parts of your program.

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 10 / 14
Friend Function: Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 c o n s t i n t IDLE = 0 ;
4 c o n s t i n t INUSE = 1 ;
5 c l a s s C2 ; // f o r w a r d d e c l a r a t i o n
6 c l a s s C1 {
7 i n t s t a t u s ; // IDLE i f o f f , INUSE i f on s c r e e n
8 // . . .
9 public :
10 void set_status ( int state ) ;
11 f r i e n d i n t i d l e ( C1 a , C2 b ) ;
12 };
13 c l a s s C2 {
14 i n t s t a t u s ; // IDLE i f o f f , INUSE i f on s c r e e n
15 // . . .
16 public :
17 void set_status ( int state ) ;
18 f r i e n d i n t i d l e ( C1 a , C2 b ) ;
19 };
20 v o i d C1 : : s e t _ s t a t u s ( i n t s t a t e )
21 { status = state ; }
22 v o i d C2 : : s e t _ s t a t u s ( i n t s t a t e )
23 { status = state ; }
24 i n t i d l e ( C1 a , C2 b )
25 {
26 i f (a . status | | b . status ) return 0;
27 else return 1;
28 }
29 i n t main ( )
30 {
31 C1 x ; C2 y ;
32 x . s e t _ s t a t u s ( IDLE ) ;
33 y . s e t _ s t a t u s ( IDLE ) ;
34 i f ( i d l e ( x , y ) ) c o u t << " S c r e e n can be u s e d . \ n " ;
35 e l s e c o u t << " I n u s e . \ n " ;
36 x . s e t _ s t a t u s ( INUSE ) ;
37 i f ( i d l e ( x , y ) ) c o u t << " S c r e e n can be u s e d . \ n " ;
38 e l s e c o u t << " I n u s e . \ n " ;
39 return 0;
40
Dr. }
Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 11 / 14
Friend Function: Example 2
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3 c o n s t i n t IDLE = 0 ;
4 c o n s t i n t INUSE = 1 ;
5 c l a s s C2 ; // f o r w a r d d e c l a r a t i o n
6 c l a s s C1 {
7 i n t s t a t u s ; // IDLE i f o f f , INUSE i f on s c r e e n
8 public :
9 void set_status ( int state ) ;
10 i n t i d l e ( C2 b ) ; // now a member o f C1
11 } ;
12 c l a s s C2 {
13 i n t s t a t u s ; // IDLE i f o f f , INUSE i f on s c r e e n
14 public :
15 void set_status ( int state ) ;
16 f r i e n d i n t C1 : : i d l e ( C2 b ) ;
17 } ;
18 v o i d C1 : : s e t _ s t a t u s ( i n t s t a t e )
19 { s t a t u s = s t a t e ; }
20 v o i d C2 : : s e t _ s t a t u s ( i n t s t a t e )
21 { s t a t u s = s t a t e ; }
22 // i d l e ( ) i s member o f C1 , b u t f r i e n d o f C2
23 i n t C1 : : i d l e ( C2 b )
24 {
25 i f ( status | | b . status ) return 0;
26 else return 1;
27 }
28 i n t main ( )
29 {
30 C1 x ; C2 y ;
31 x . s e t _ s t a t u s ( IDLE ) ;
32 y . s e t _ s t a t u s ( IDLE ) ;
33 i f ( x . i d l e ( y ) ) c o u t << " S c r e e n can be u s e d . \ n " ;
34 e l s e c o u t << " I n u s e . \ n " ;
35 x . s e t _ s t a t u s ( INUSE ) ;
36 i f ( x . i d l e ( y ) ) c o u t << " S c r e e n can be u s e d . \ n " ;
37 e l s e c o u t << " I n u s e . \ n " ;
38 return 0;
39 }
Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 12 / 14
Friend Classes

It is possible for one class to be a friend of another class.

When this is the case, the friend class and all of its member functions have
access to the private members defined within the other class.

Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 13 / 14
Friend Class: Example
1 // U s i n g a f r i e n d c l a s s .
2 #i n c l u d e <i o s t r e a m >
3 u s i n g namespace s t d ;
4 c l a s s TwoValues {
5 int a;
6 int b;
7 public :
8 TwoValues ( i n t i , i n t j ) { a = i ; b = j ; }
9 f r i e n d c l a s s Min ;
10 };
11 c l a s s Min {
12 public :
13 i n t min ( TwoValues x ) ;
14 };
15 i n t Min : : min ( TwoValues x )
16 {
17 return x . a < x . b ? x . a : x . b ;
18 }
19 i n t main ( )
20 {
21 TwoValues ob ( 1 0 , 2 0 ) ;
22 Min m;
23 c o u t << m. min ( ob ) ;
24 return 0;
25 }
Dr. Deepika Gupta (Department of CSE, IIITV-ICD) Object Oriented Design and Programming Sept 23, 2022 14 / 14

You might also like