Friend
Friend Function
- A friend function of a class is defined outside that class's scope but it has the right to access all private and protected members
- Friend declaration can go in either the public, private, or protected section of a class
Regular Stand-alone Function
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Util.h"
class Rectangle
{
private:
double width;
double length;
public:
//constructor
Rectangle(double w, double l);
//accessor
double getWidth() const;
double getLength() const;
double getArea() const;
//mutator
void setWidth(double w);
void setLength(double l);
//friend
friend void display(const Rectangle &r);
};
#endif
//Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle(double w, double l):width(w), length(l){}
double Rectangle::getWidth() const
{
return width;
}
double Rectangle::getLength() const
{
return length;
}
double Rectangle::getArea() const
{
return width*length;
}
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
//Util.h
#ifndef UTIL_H
#define UTIL_H
#include <iostream>
//forward declaration
class Rectangle;
void display(const Rectangle &r);
#endif
//Util.cpp
#include <iostream>
#include "Rectangle.h"
#include "Util.h"
void display(const Rectangle &r)
{
std::cout<<"Width: "<<r.width<<std::endl;
std::cout<<"Length: "<<r.length<<std::endl;
}
#include <iostream>
#include "Rectangle.h"
#include "Util.h"
int main(int argc, char *argv[])
{
Rectangle *r = new Rectangle(5, 10);
display(*r);
return 0;
}
Member Function of Another Class
//Square.h
#ifndef SQUARE_H
#define SQUARE_H
#include "Rectangle.h"
class Square
{
private:
int edge;
public:
Square(int e):edge(e){}
int getEdge() const;
friend void Rectangle::setEdge(const Square &s);
};
#endif
//Square.cpp
#include "Square.h"
int Square::getEdge() const {return edge;}
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Square;
class Rectangle
{
private:
double width;
double length;
public:
//constructor
Rectangle(double w, double l);
//accessor
double getArea() const;
//mutator
void setEdge(const Square &s);
};
#endif
//Rectangle.cpp
#include "Rectangle.h"
#include "Square.h"
Rectangle::Rectangle(double w, double l):width(w), length(l){}
double Rectangle::getArea() const
{
return width*length;
}
void Rectangle::setEdge(const Square &s)
{
width = s.edge;
length = s.edge;
}
//main.cpp
#include <iostream>
#include "Rectangle.h"
#include "Square.h"
int main(int argc, char *argv[])
{
Square s(10);
Rectangle r(0, 0);
r.setEdge(s);
std::cout<<"Area: "<<r.getArea()<<std::endl;
return 0;
}
Friend Class
//Square.h
#ifndef SQUARE_H
#define SQUARE_H
class Square
{
private:
int edge;
public:
friend class Cube;
};
#endif
//Cube.h
#ifndef CUBE_H
#define CUBE_H
#include "Square.h"
class Cube
{
private:
Square s;
public:
int getArea() const
{
return s.edge*s.edge;
}
void setEdge(int e)
{
s.edge = e;
}
};
#endif
//main.cpp
#include <iostream>
#include "Cube.h"
int main(int argc, char *argv[])
{
Cube c;
c.setEdge(10);
std::cout<<"Area: "<<c.getArea()<<std::endl;
return 0;
}
Other class have direct access to protected and private members of the class