Saturday, 23 March 2019

CPP-EX46

 Write a C++ program to create two classes Class1 and Class2. Each  class contains one float data member. Write following functions:
     i.To accept float numbers.
     ii.To display float numbers in right justified format with precision
        of two digits.
     iii.To exchange the private values of both these classes by using
        friend function.
       
#include<iostream.h>
#include<conio.h>
 class class2;
 class class1
   {
    float value1;
    public:
    void indata(float a) {value1=a;}
    void display(void) {cout<<value1<<"\n";}
    friend void exchange(class1 &,class2 &);
   };
 class class2
   {
    float value2;
    public:
    void indata(float b) {value2=b;}
    void display(void) {cout<<value2<<"\n";}
    friend void exchange(class1 &,class2 &);
   };
 void exchange(class1 &x,class2 &y)
   {
    int temp;
    temp=x.value1;
    x.value1=y.value2;
    y.value2=temp;
   }
 int main()
   {
    clrscr();
    class1 C1;
    class2 C2;
    C1.indata(6.7847);
    C2.indata(12.4514);
    cout<<"\nValues Before Exchange.\n";
    cout.precision(2);
    cout.setf(ios::right,ios::adjustfield);
    C1.display();
    C2.display();
    exchange(C1,C2);
    cout<<"\nValues After Exchange.\n";
    C1.display();
    C2.display();
    getch();
    return 0;
   }