C & C++ Programming - Friend Functions
#pragma once
#include "iostream"
using namespace std;
// Forward declaration
class CDerivedOne;
class CDerivedTwo;
//////////////////////////////////////////////////////////////////////////////////////////////////
// Class CBase
class CBase
{
private:
double m_dA;
double m_dB;
public:
CBase(int x = 0, double y = 0)
{
m_dA = x, m_dB = y;
}
friend void Add(CBase &x, CDerivedOne &y, CDerivedTwo &z);
friend void Add(CBase &x, CDerivedTwo &y, CDerivedOne &z);
friend void Add(CDerivedTwo &x, CBase &y, CDerivedOne &z);
friend void Add(CDerivedTwo &x, CDerivedOne &y, CBase &z);
friend void Add(CDerivedOne &x, CDerivedTwo &y, CBase &z);
friend void Add(CDerivedOne &x, CBase &y, CDerivedTwo &z);
void display()
{
cout<<"Entered values are" << endl << "A= " << m_dA << endl<<"B= " <<
m_dB << endl;
}
void read()
{
cout << "The values of a & b" << endl;
cout << "1. = ";
cin >> m_dA;
cout << "2. = ";
cin >> m_dB;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// Class CDerivedOne
class CDerivedOne:public CBase
{
private:
double m_dA;
double m_dB;
public:
CDerivedOne(int x=0,double y=0 )
{
m_dA = x, m_dB = y;
}
friend void Add(CBase &x, CDerivedOne &y, CDerivedTwo &z);
friend void Add(CBase &x, CDerivedTwo &y, CDerivedOne &z);
friend void Add(CDerivedTwo &x, CBase &y, CDerivedOne &z);
friend void Add(CDerivedTwo &x, CDerivedOne &y, CBase &z);
friend void Add(CDerivedOne &x, CDerivedTwo &y, CBase &z);
friend void Add(CDerivedOne &x, CBase &y, CDerivedTwo &z);
void display()
{
cout<<"Entered values are" << endl << "A = " << m_dA << endl << "CDerivedOne
= " << m_dB << endl;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// Class CDerivedTwo
class CDerivedTwo:public CBase
{
private:
double m_dA;
double m_dB;
public:
CDerivedTwo(int x=0, double y=0)
{
m_dA = x, m_dB = y;
}
friend void Add(CBase &x, CDerivedOne &y, CDerivedTwo &z);
friend void Add(CBase &x, CDerivedTwo &y, CDerivedOne &z);
friend void Add(CDerivedTwo &x, CBase &y, CDerivedOne &z);
friend void Add(CDerivedTwo &x, CDerivedOne &y, CBase &z);
friend void Add(CDerivedOne &x, CDerivedTwo &y, CBase &z);
friend void Add(CDerivedOne &x, CBase &y, CDerivedTwo &z);
void display()
{
cout << "Class Derived Two" << endl;
cout << "Entered values are"<< endl << "A = " << m_dA << endl << "B = " << m_dB
<< endl;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// Programmer: //
// Description: //
//////////////////////////////////////////////////////////////////////////////////////////////////
//! USE OF CLASS/CONSTRUCT.
#include "stdafx.h"
#include "Arithmatic.h"
//////////////////////////////////////////////////////////////////////////////////////////////////
// All the function are defined in global space can be used within class as friend
void Add(CBase &x, CDerivedOne &y, CDerivedTwo &z)
{
x.m_dA = x.m_dA + y.m_dA + z.m_dA;
x.m_dB = x.m_dB + y.m_dB + z.m_dB;
}
void Add(CBase &x, CDerivedTwo &y, CDerivedOne &z)
{
x.m_dA = x.m_dA + y.m_dA + z.m_dA;
x.m_dB = x.m_dB + y.m_dB + z.m_dB;
}
void Add(CDerivedTwo &x, CBase &y, CDerivedOne &z)
{
x.m_dA=x.m_dA+y.m_dA+z.m_dA;
x.m_dB=x.m_dB+y.m_dB+z.m_dB;
}
void Add(CDerivedTwo &x, CDerivedOne &y, CBase &z)
{
x.m_dA = x.m_dA + y.m_dA + z.m_dA;
x.m_dB = x.m_dB + y.m_dB + z.m_dB;
}
void Add(CDerivedOne &x, CDerivedTwo &y, CBase &z)
{
x.m_dA = x.m_dA + y.m_dA + z.m_dA; x.m_dB = x.m_dB + y.m_dB + z.m_dB;
}
void Add(CDerivedOne &x, CBase &y, CDerivedTwo &z)
{
x.m_dA = x.m_dA + y.m_dA + z.m_dA; x.m_dB = x.m_dB + y.m_dB + z.m_dB;
}
#pragma once
#include "iostream"
using namespace std;
//class A;
// here the Class B is friend of A it can access any functions of A
class A
{
public:
friend class B;
private:
void FunctionA()
{
cout << "FunctionA()" << endl;
}
};
///////////////////////////////////////////////////////////////////////////////////
class B
{
public:
void FunctionB()
{
A a;
a.FunctionA();
cout << "FunctionB" << endl;
}
};
//programme for FRIEND FUNCTION
#include "iostream"
using namespace std;
class COperators
{
private:
int m_nX, m_nY;
public:
COperators()
{
}
COperators(int x, int y) //constructor which inititilise the varible
{
m_nX = x, m_nY = y;
}
void read(); //member fn of class COperators
void display(); //member fn of class COperators
COperators operator +(int x); //member fn of class COperators containing
one argument as int
COperators operator +(COperators x); //member fn of class COperators containing
one argument as object
friend COperators operator +(int x, COperators y); //friend fn similar to
general fn and not m_nX member fn of class A(does not require ::)
COperators operator -(int x); //member fn of class COperators containing
one argument as int
COperators operator -(COperators x); //member fn of class COperators containing
one argument as object
friend COperators operator -(int x, COperators y); //friend fn similar to
general fn and not m_nX member fn of class A(does not require ::)
COperators operator *(int x);
COperators operator *(COperators x);
friend COperators operator *(int x ,COperators y);
COperators operator /(int x);
COperators operator /(COperators x);
friend COperators operator /(int x, COperators y);
};
void COperators::read()
{
cout << "Please enter the number\n";
cout << "1. = ";
cin >> m_nX;
cout << "2. = ";
cin >> m_nY;
}
void COperators::display()
{
cout << "\nThe numbers are\n";
cout << "m_nX = " << m_nX << "\t" << "m_nY = " << m_nY << endl;
}
// Local variable will be added to both members resp.
COperators COperators::operator +(int x)
{
COperators t;// temp object
t.m_nX = m_nX + x;
t.m_nY = m_nY + x;
return t;// returns the object
}
// Class variable will be added to both members independently.
COperators COperators::operator +(COperators x)
{
COperators t;
t.m_nX = m_nX + x.m_nX;
t.m_nY = m_nY + x.m_nY;
return t;
}
// Local variable is added to each member variable independently
COperators operator +(int x, COperators y)
{
COperators t;
t.m_nX = x + y.m_nX;
t.m_nY = x + y.m_nY;
return t;
}
COperators operator -(int x, COperators y)
{
COperators t;
t.m_nX = x - y.m_nX;
t.m_nY = x - y.m_nY;
return t;
}
COperators operator *(int x, COperators y)
{
COperators t;
t.m_nX = x * y.m_nX;
t.m_nY = x * y.m_nY;
return t;
}
COperators operator /(int x, COperators y)
{
COperators t;
t.m_nX = x / y.m_nX;
t.m_nY = x / y.m_nY;
return t;
}
COperators COperators::operator -(int x)
{
COperators t;
t.m_nX = m_nX - x;
t.m_nY = m_nY-x;
return t;
}
#include "stdafx.h"
#include "Arithmatic.h"
#include "Operator.h"
#include "Friend_Function_Inside_Class.h"
int _tmain(int argc, _TCHAR* argv[])
{
CBase objBase(2, 4);
CDerivedOne objDerivedOne(1, 2);
CDerivedTwo objDerivedTwo(1, 4);
Add(objBase, objDerivedOne, objDerivedTwo);
objBase.display();
///////////////////////////////////////////////////////////
COperators obj1, obj2;
obj1.read();
obj1.display();
// Here the 10 added to both member variables
obj1 = obj1 + 10;
obj1.display();
////////////////////////////////////////////////////////////////////
B b;
b.FunctionB();
return 0;
}