Search This Blog

Pages

Sunday, February 13, 2011

LabTask 3.b.4

/*Write a program to make a specific function of a class a friend of other class to add elements from the two classes. Both of these classes should have two integer members. Make others global functions to subtract and multiply objects of class one and class two.
By Sandesh Hetfield Shakya
*/

#include<iostream.h>
#include <conio.h>

class add2;

class add1
{
int element;

public:
add1(int x)
{
element=x;
}

int getele()
{
return element;
}

friend int sum(add1,add2);
};

class add2
{
int element;
public:
add2(int y)
{
element=y;
}
int getele()
{
return element;
}

friend int sum(add1,add2);
};

int sum(add1 A, add2 B)
{
return (A.element+B.element);
}
int sub(add1 A, add2 B)
{
return(A.getele()-B.getele());
}
int mul(add1 A,add2 B)
{
return(A.getele()*B.getele());
}


int main ()
{
clrscr();
add1 a(5);
add2 b(7);

cout<<endl<<"sum of add1 & add2 is :"<<sum(a,b);
cout<<endl<<"subtraction: "<<sub(a,b);
cout<<endl<<"multiplication:"<<mul(a,b);
getch();
return (0);
}

No comments:

Post a Comment