Examples of friend class by using C++
Just like functions are made friends of classes, we can also make one class to be a friend of another class. Then, the friend class will have access to all the private members of the other class.
#include <iostream. h>
class Square;
class Rectangle {
int width, height;
int width, height;
public:
Rectangle(int w = 1, int h = 1):width(w),height(h){}
void display() {
cout << "Rectangle: " << width * height << endl;
};
void morph(Square &);
};
Rectangle(int w = 1, int h = 1):width(w),height(h){}
void display() {
cout << "Rectangle: " << width * height << endl;
};
void morph(Square &);
};
class Square {
int side;
int side;
public:
Square(int s = 1):side(s){}
void display() {
cout << "Square: " << side * side << endl;
};
friend class Rectangle;
};
Square(int s = 1):side(s){}
void display() {
cout << "Square: " << side * side << endl;
};
friend class Rectangle;
};
void Rectangle::morph(Square &s) {
width = s.side;
height = s.side;
}
width = s.side;
height = s.side;
}
int main () {
Rectangle rec(5,10);
Square sq(5);
cout << "Before:" << endl;
rec.display();
sq.display();
Rectangle rec(5,10);
Square sq(5);
cout << "Before:" << endl;
rec.display();
sq.display();
rec.morph(sq);
cout << "\nAfter:" << endl;
rec.display();
sq.display();
return 0;
}
cout << "\nAfter:" << endl;
rec.display();
sq.display();
return 0;
}
Friend classes
It is also possible to make an entire class a friend of another class. This gives all of the members of the friend class access to the private members of the other class. Here is an example:
class Storage
{
private:
int m_nValue;
double m_dValue;
public:
Storage(int nValue, double dValue)
{
m_nValue = nValue;
m_dValue = dValue;
}
{
private:
int m_nValue;
double m_dValue;
public:
Storage(int nValue, double dValue)
{
m_nValue = nValue;
m_dValue = dValue;
}
// Make the Display class a friend of Storage
friend class Display;
};
friend class Display;
};
class Display
{
private:
bool m_bDisplayIntFirst;
{
private:
bool m_bDisplayIntFirst;
public:
Display(bool bDisplayIntFirst) { m_bDisplayIntFirst = bDisplayIntFirst; }
Display(bool bDisplayIntFirst) { m_bDisplayIntFirst = bDisplayIntFirst; }
void DisplayItem(Storage &cStorage)
{
if (m_bDisplayIntFirst)
std::cout << cStorage.m_nValue << " " << cStorage.m_dValue << std::endl;
else // display double first
std::cout << cStorage.m_dValue << " " << cStorage.m_nValue << std::endl;
}
};
{
if (m_bDisplayIntFirst)
std::cout << cStorage.m_nValue << " " << cStorage.m_dValue << std::endl;
else // display double first
std::cout << cStorage.m_dValue << " " << cStorage.m_nValue << std::endl;
}
};
int main()
{
Storage cStorage(5, 6.7);
Display cDisplay(false);
{
Storage cStorage(5, 6.7);
Display cDisplay(false);
cDisplay.DisplayItem(cStorage);
return 0;
}
}
The below example will completely illustrate the use of a friend classes.
For Example:-
#include<iostream.h>
#include<conio.h>
class A
{
private:
int a,b;
public:
A()
{
a=10;
b=20;
}
friend class B;
};
class B
{
public:
B()
void showA(A obj)
{
cout<<”The value of a: ”<<obj.a<<endl;
}
void showB(A obj)
{
cout<<”The value of b: ”<<obj.b<<endl;
}
};
main()
{
A x;
B y;
y.showA(x);
y.showB(x);
getch();
}
#include<conio.h>
class A
{
private:
int a,b;
public:
A()
{
a=10;
b=20;
}
friend class B;
};
class B
{
public:
B()
void showA(A obj)
{
cout<<”The value of a: ”<<obj.a<<endl;
}
void showB(A obj)
{
cout<<”The value of b: ”<<obj.b<<endl;
}
};
main()
{
A x;
B y;
y.showA(x);
y.showB(x);
getch();
}
// friend class
#include <iostream. h>
#include <iostream. h>
class Square;
class Rectangle {
int width, height;
public:
int area ()
{return (width * height);}
void convert (Square a);
};
int width, height;
public:
int area ()
{return (width * height);}
void convert (Square a);
};
class Square {
friend class Rectangle;
private:
int side;
public:
Square (int a) : side(a) {}
};
friend class Rectangle;
private:
int side;
public:
Square (int a) : side(a) {}
};
void Rectangle::convert (Square a) {
width = a.side;
height = a.side;
}
int main () {
Rectangle rect;
Square sqr (4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
width = a.side;
height = a.side;
}
int main () {
Rectangle rect;
Square sqr (4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
// friclass.cpp
// friend classes
#include <iostream.h>
// friend classes
#include <iostream.h>
////////////////////////////////////////////////////////////////
class alpha
{
private:
int data1;
public:
alpha() : data1(99) { } //constructor
friend class beta; //beta is a friend class
};
////////////////////////////////////////////////////////////////
class beta
{
public: //access private alpha data
void func1(alpha a) { cout << "\ndata1=" << a.data1; }
void func2(alpha a) { cout << "\ndata1=" << a.data1; }
};
////////////////////////////////////////////////////////////////
int main()
{
alpha a;
beta b;
b.func1(a);
b.func2(a);
cout << endl;
return 0;
}
class alpha
{
private:
int data1;
public:
alpha() : data1(99) { } //constructor
friend class beta; //beta is a friend class
};
////////////////////////////////////////////////////////////////
class beta
{
public: //access private alpha data
void func1(alpha a) { cout << "\ndata1=" << a.data1; }
void func2(alpha a) { cout << "\ndata1=" << a.data1; }
};
////////////////////////////////////////////////////////////////
int main()
{
alpha a;
beta b;
b.func1(a);
b.func2(a);
cout << endl;
return 0;
}
#include<iostream.h>
class summation{
friend class mean;
private:
int n,dd;float x ,sum;
public:
void input(){
cout<<"Enter number of the student"<<endl;
cin>>dd;
for(int j=1;j<=dd;j++){
cout<<"Enter number of degree for the stude"<<"["<<j<<"]"<<endl;
cin>>n;
sum=0;
for (int i=1;i<=n;i++){
cout<<" Enter degree "<<"["<<i<<"]"<<endl;
cin>>x;
sum+=x;}
cout<<"sum"<<"["<<i<<"]"<<sum<<endl;
}
}};
class summation{
friend class mean;
private:
int n,dd;float x ,sum;
public:
void input(){
cout<<"Enter number of the student"<<endl;
cin>>dd;
for(int j=1;j<=dd;j++){
cout<<"Enter number of degree for the stude"<<"["<<j<<"]"<<endl;
cin>>n;
sum=0;
for (int i=1;i<=n;i++){
cout<<" Enter degree "<<"["<<i<<"]"<<endl;
cin>>x;
sum+=x;}
cout<<"sum"<<"["<<i<<"]"<<sum<<endl;
}
}};
main(){
summation ob1;
ob1.input();
summation ob1;
ob1.input();
ادعو لوالدتي جزاكم الله خيرا
HamedDiyala
Examples of friend class by using C++
Reviewed by حامد طالب العراقي
on
5/16/2015 10:08:00 ص
Rating:
ليست هناك تعليقات: