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



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

Answer / 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

More OOPS Interview Questions

Is this job good for future? can do this job post grduate student?

1 Answers  


What is a class oop?

1 Answers  


Differences between inline functions and non-inline functions?

4 Answers   Ness Technologies,


JAVA is FULLY OBJECT ORIENTED PROGRAMING LANGUAGE?

3 Answers  


what is meant by files?

4 Answers   Infosys,


Program to check whether a word starts with a capital letter or not.

1 Answers   Infosys,


What is cohesion in oop?

1 Answers  


why overriding?

3 Answers  


Is following functions are said to be overloaded? int add(int a,int b) char *add(int a,int b)

4 Answers  


What is an interface in oop?

1 Answers  


Plese get me a perfect C++ program for railway/airway reservation with all details.

1 Answers   ITM,


What is the difference between pass by reference and pass by value?

12 Answers   Pfizer, TCS,


Categories