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 |
Is this job good for future? can do this job post grduate student?
What is a class oop?
Differences between inline functions and non-inline functions?
JAVA is FULLY OBJECT ORIENTED PROGRAMING LANGUAGE?
what is meant by files?
Program to check whether a word starts with a capital letter or not.
What is cohesion in oop?
why overriding?
Is following functions are said to be overloaded? int add(int a,int b) char *add(int a,int b)
What is an interface in oop?
Plese get me a perfect C++ program for railway/airway reservation with all details.
What is the difference between pass by reference and pass by value?