424570636-friend-function-1-pptx.hiipptx

13 views 6 slides Apr 30, 2025
Slide 1
Slide 1 of 6
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6

About This Presentation

Here’s a detailed description (approximately 3000 characters) suitable for the "Friend Function" PowerPoint presentation (PPT). This can be used in the slide notes, as a project description, or documentation:

Description: Friend Function in C++ (Approx. 3000 characters)
The Friend Funct...


Slide Content

Friend Function & Friend Class V.Radhesyam

'friend ' function/Class A 'friend ' function has access to all 'private' members of the class for which it is a 'friend '. A friend class has access to all 'private' members of the class for which it is a 'friend '. Friend function is non-member function of a Class To declare a 'friend ' function, include its prototype within the class, preceding it with the C++ keyword 'friend '.

class base { int val1, val2; public: void get () { cout << "Enter two values:"; cin >> val1>>val2; } friend float mean(base ob ); }; Friend Function float mean(base ob ) { return float(ob.val1 + ob.val2) / 2; } void main() { clrscr (); base obj ; obj.get (); cout << "\n Mean value is : " << mean( obj ); getch (); }

class beta; class alpha { private: int data; public: alpha() { data=3; } friend int frifun ( alpha,beta ); }; class beta { private: int data; public: beta() { data=7; } friend int frifun (alpha ,beta ); }; int frifun (alpha a,beta b) { return( a.data+b.data ); } Void main() { alpha aa; beta bb; cout << frifun ( aa,bb ); } Friend Function with two classes example

Friend Class class Rectangle { int L,B; public : Rectangle () { L=10 ; B=20; } friend class Square; }; class Square { int S; public: Square () { S=5 ; } void Display(Rectangle Rect ) { cout <<"Length : "<< Rect.L ; cout <<"Breadth : "<< Rect.B ; cout <<"Side : "<<S ; } }; void main() { Rectangle R; Square S; S.Display (R); }

Lab Assignments Write a program to accept the student detail such as name and 3 different marks by get_data () method and display the name and average of marks using display() method. Define a friend class for calculating the average of marks using the method mark_avg (). Write a C++ program to find the sum of two complex numbers using friend functions. Develop a C++ Program to Implement Friend Class for Adding two Numbers where class sum is declared as friend of other class used to read values