adspace


to find out the minimum of two integer number of two different
classes using friend function

Answer Posted / Vishal Jaiswal

In C++, you can write a friend function to compare and find the minimum of two integers from different classes. Here's an example:

class Class1 {
int num1;

public:
void setNum(int n) {
num1 = n;
}

friend int findMin(int, int); // declare as a friend function
};

class Class2 {
int num2;

public:
void setNum(int n) {
num2 = n;
}
};

int findMin(int n1, int n2) {
return (n1 < n2) ? n1 : n2; // return the minimum number
}

// Usage:
Class1 obj1;
Class2 obj2;
obj1.setNum(5);
obj2.setNum(7);
std::cout << findMin(obj1.num1, obj2.num2) << std::endl; // Output: 5

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Please send ford technologies placement paper 2 my mail id

2179


Hi friends I have experience of 6 months in website design and maintanence. Now i am looking for other IT jobs.. to switch platform. please post any interview you know in chennai.

2278