0% found this document useful (0 votes)
2 views5 pages

OOP-Weekend Lecture 05

The document provides a tutorial on using friend functions in C++ to allow non-member functions access to private data of classes. It includes four tasks: creating a `Number` class to add two objects, a `Counter` class to increment a count, classes `A` and `B` to sum their private integers, and a `Person` class to compare ages. Each task is accompanied by example code demonstrating the implementation of friend functions.

Uploaded by

ayesha.widemedia
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)
2 views5 pages

OOP-Weekend Lecture 05

The document provides a tutorial on using friend functions in C++ to allow non-member functions access to private data of classes. It includes four tasks: creating a `Number` class to add two objects, a `Counter` class to increment a count, classes `A` and `B` to sum their private integers, and a `Person` class to compare ages. Each task is accompanied by example code demonstrating the implementation of friend functions.

Uploaded by

ayesha.widemedia
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/ 5

Why use a FRIEND FUNCTION?

Normally, only members of a class can access its private/protected data. But sometimes, you want a non-
member function or another class to access this data. That's where friend comes in.

Task 1

Write a class `Number` with a private integer. Use a friend function to add two `Number` objects and

return the sum.

#include <iostream>

using namespace std;

class Number

private:

int value;

public:

Number(int v)

value=v;

friend int addNumbers(Number n1, Number n2);

};

int addNumbers(Number n1, Number n2)

return n1.value + n2.value;

int main() {
Number a(5);

Number b(10);

cout << "Sum: " << addNumbers(a, b) << endl; return 0;

}
Task 2

Create a class `Counter` with a private member `count`. Write a friend function `increment()` that

increases the value of `count` and prints it.

#include <iostream>
using namespace std;

class Counter {
private:
int count;

public:
Counter()
{
count =0;
}

friend void increment(Counter c);


};

void increment(Counter c) { c.count++;


cout << "Count after increment: " << c.count << endl;
}

int main() {
Counter c; increment(c); return 0;
}
Task 3

Create two classes `A` and `B`, each with a private integer. Write a friend function `showSum()` that can

access and sum private data from both classes.

#include <iostream> using


namespace std;

class A;
class B;

class A { private:
int x;

public:
A(int val) : x(val) {} friend void
showSum(A, B);
};

class B { private:
int y;

public:
B(int val) : y(val) {} friend void
showSum(A, B);
};

void showSum(A a, B b) {
cout << "Sum: " << a.x + b.y << endl;
}

int main() {
A a(3);
B b(7);
showSum(a, b);
return 0;
}
Task 4

Create a class `Person` with a private member `age`. Write a friend function `compareAges()` that compares

the ages of two `Person` objects and prints who is older.


#include <iostream> using
namespace std; class
Person { private:
int age;

public:
Person(int a) : age(a) {}

friend void compareAges(Person p1, Person p2);


};

void compareAges(Person p1, Person p2) { if


(p1.age > p2.age)
cout << "First person is older." << endl; else if (p1.age
< p2.age)
cout << "Second person is older." << endl;
else
cout << "Both are of same age." << endl;
}

int main() {
Person p1(25), p2(30);
compareAges(p1, p2); return 0;
}

You might also like