Friend Function And Class In C++ | Program Examples - LBM4

Recent Post

Friday, 29 November 2019

Friend Function And Class In C++ | Program Examples




In some cases you need to access the private data members of a class from non member functions. In such situations you must declare the function as friend function of the class. This friend function seems to violate the data hiding feature of OOP concept. However, the function that accesses private data must be declared as friend function within the class. With friend functions data integrity is still maintained.

Sometimes you may need to make, one or all the member functions of a class friend to other class. For that we declare class or member function as the friend to the other class so that one or all the member functions of the declared class will be friend to the class within which it is declared.

Example:

class breadth;
class length
{
private:
......
public:
......
friend int add(length, breadth); //friend function declaration
};
class breadth
{
private:
......
public:
......
friend int add(length, breadth); //friend function declaration
};
int add( length l, breadth b) { }


1. Write a class for instantiating the objects that represent the two-dimensional Cartesian coordinate system.
A. make a particular member function of one class to friend function in another class for addition
B. make other three functions to work as a bridge between the classes for multiplication, division and subtraction.
C. Also write a small program to demonstrate that all the member functions of one class are the friend functions of another class if the former class is made friend to the latter.
Make least possible classes to demonstrate all above in single program without conflict.


 #include<iostream>
using namespace std;
class another;
class cartesian
{
    int x,y;
public:
    cartesian(int a=2, int b=4)
    {
        x=a;
        y=b;
    }
    void add(cartesian A,another B);
   friend void sub(cartesian A,another B);
    friend void mult(cartesian A,another B);
    friend void div(cartesian A,another B);

};
class another
{
    int x,y;
public:
    another(int a=3,int b=6)
    {
        x=a;
        y=b;

    }
    friend void cartesian:: add(cartesian A,another B);
   friend void sub(cartesian A,another B);
    friend void mult(cartesian A,another B);
    friend void div(cartesian A,another B);

};
void cartesian::add(cartesian A,another B)
{
    int x=A.x+B.x;
    int y=A.y+B.y;
    cout<<"sum:"<<x<<","<<y<<""<<endl;
}
void sub(cartesian A,another B)
{
    int x=A.x-B.x;
    int y=A.y-B.y;
    cout<<"diff:"<<x<<","<<y<<""<<endl;
}
main()
{

cartesian C,sum;
another A;
sum.add(C,A);
sub(C,A);
return 0;
}

Write a class to store x, y, and z coordinates of a point in three-dimensional space. Using operator overloading, write friend functions to add, and subtract the vectors.

#include<iostream>
using namespace std;
class coordinate
{
    float x,y,z;
public:
    coordinate(float a,b,c):x(a),y(b),z(c){}
    friend coordinate operator +(coordinate(a,b));
    friend coordinator operator -(coordinate(a,b));
    void display()
    {
        cout<<"(c"<<x<<","<<y<<","<<z<<endl;

    }
};
coordinate operator +(coordinate a,b)
{
    coordinate temp(a.x+b.x,a.y+b.y,a.z+b.z);
    return temp;
}
coordinate operator -(coordinate a,b);
{
    coordinate temp(a.x-b.x,a.y-b.y,a.z-b.z);
    return temp;
}
main()
{
    int x,y,z;
    char temp;
    cout<<"enter coordinate x,y,z"<<endl;
    cin>>x>>temp>>y>>temp>>z;
    coordinate (x,y,z);
    cout<<"enter coordinate"<<endl;
    cin>>x>>temp>>y>>temp>>z;
    coordinate b(x,y,z);
    cout<<"sum is:";
    coordinate =a+b;
    s.display;
    return 0;
}








No comments:

Post a Comment