Untitled Document
Untitled Document
When overloading using a member function, the operator function is a member of the
class and works on the calling object.
#include<iostream>
class Distance {
private:
int feet;
// Constructors
std::cout << "F: " << feet << " I: " << inches << std::endl;
};
int main() {
D3.displayDistance();
D4.displayDistance();
Practicalkida.com
return 0;
When overloading using a friend function, the operator function is defined outside the
class but is granted access to the class's private and protected members.
#include<iostream>
class Distance {
private:
int feet;
int inches;
public:
// Constructors
std::cout << "F: " << feet << " I: " << inches << std::endl;
}
Practicalkida.com
};
int main() {
D3.displayDistance();
D4.displayDistance();
return 0;
}
Member Function: The overloaded - operator as a member function is called on the
object directly (e.g., -D1). The function works on this pointer, representing the
calling object.
Friend Function: The overloaded - operator as a friend function takes an object of the
class as a parameter and returns a new object with negated values.
Practicalkida.com