0% found this document useful (0 votes)
25 views6 pages

22-Friend Functions and Class in C++-19!04!2022 (19-Apr-2022) Material I 19-04-2022 Friend Functions and Class in CPL

The document discusses C++ friend functions and class. It provides examples of using friend functions to access private members of classes. It demonstrates how friend functions allow functions outside of a class to access private and protected members. It also shows examples of friend functions adding members of two different classes and accessing private members to modify them.
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)
25 views6 pages

22-Friend Functions and Class in C++-19!04!2022 (19-Apr-2022) Material I 19-04-2022 Friend Functions and Class in CPL

The document discusses C++ friend functions and class. It provides examples of using friend functions to access private members of classes. It demonstrates how friend functions allow functions outside of a class to access private and protected members. It also shows examples of friend functions adding members of two different classes and accessing private members to modify them.
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/ 6

09-04-2022 BCSE102 Lab Session 2:00Pm to 3:30PM

// C++ Friend Functions and Class

//If a function is defined as a friend function

//in C++, then the protected and private data

//of a class can be accessed using the function.

//Keyword " friend" -comipler will know it is friend function

// declared inside the body of a class

// syntax:

//class class_name {

// friend data_type function_name (argument /s);

// }

Characteristics of a Friend function:

o The function is not in the scope of the class to which it has been declared as a friend.
o It cannot be called using the object as it is not in the scope of that class.
o It can be invoked like a normal function without using the object.
o It cannot access the member names directly and has to use an object name and dot
membership operator with the member name.
o It can be declared either in the private or the public part.

#include <iostream>

using namespace std;

class B; // forward declaration

class A {

int x;

public:

void setdata(int i)

x = i;

friend void min(A,B); // friend function


};

class B {

int y;

public:

void setdata(int i)

y = i;

friend void min(A,B); // friend function

};

void min(A a, B b)

if (a.x <= b.y)

cout<<a.x << endl;

else

cout<<b.y<<endl;

int main()

A a;

B b;

a.setdata(110);

b.setdata(202);

min(a,b);

return 0;

#include<iostream>

using namespace std;

class A {

private:
int a;

public:

A() {

a = 12345;

friend class B;

};

class B {

private:

int b;

public:

void showA(A& x)

cout<< "A:a ="<< x.a <<endl;

};

int main()

A a;

B b;

b.showA(a);

return 0;

// Friend Function

#include<iostream>

using namespace std;

class Distance {

private:

int meter;

friend int AddTen(Distance);


public:

Distance() : meter(0) {

};

//friend function definition

int AddTen(Distance d) {

//accessing private members from the

//friend function

d.meter +=10;

return d.meter;

int main()

Distance D;

cout<<"Distance : " << AddTen(D);

return 0;

Output:-
Distance : 10

...Program finished with exit code 0

Press ENTER to exit console.

// Add Members of two Different Classes

#include<iostream>

using namespace std;

class C_B; // forward declaration

class C_A{

public:

C_A() : numA(12) {
}

private:

int numA;

friend int add(C_A, C_B);

};

class C_B{

public:

C_B():numB(1) {

private:

int numB;

friend int add(C_A, C_B);

};

// accessing the members of both the Classes

int add(C_A ObjA, C_B ObjB)

return (ObjA.numA + ObjB.numB);

int main()

C_A ObjA;

C_B ObjB;

cout<< "Sum : "<<add(ObjA, ObjB);

return 0;

Output:-
Sum : 13

...Program finished with exit code 0

Press ENTER to exit console.


C++ Inheritance
//In C++, inheritance is a process in

//which one object acquires all the properties

//and behaviors of its parent object automatically.

//reusability:-

//We can reuse, extend or modify the attributes

//and behaviors which are defined in other class

// In C++, the class which inherits the members

//of another class is called derived class and the

//class whose members are inherited is called

//base class. The derived class is the

//specialized class for the base class.

// Code Reusability

You might also like