> Is it possible to do something similar with my double getFloatingPointEquiv() function?
No, I don't know a way to overload operator<< to print either decimal or function, if there is one.
Who knows, if you wait around, someone with more experience may actually know a way or provide some brilliant workaround.
Personally, I thought getFloatingPointEquiv() was sufficient for its purpose. Maybe, if you change the name to something like getFloat(); you can easily print the equivalent floating-point value by simply coding
|
std::cout << object1.getFloat();
|
, which isn't too bad.
> I also need to work out how to do the following as well... abjetc1 + object2
As for overloading + operator and any other arithmetic operators such as - / *, you generally follow the format
|
ObjectType ObjectType::operator+ ( const ObjectType &object ) const;
|
This is a
member function. Notice only 1 object being pass as a parameter? It's because this member function already has access to its own object. Notice that the function is marked const and so is the object being passed. When we perform binary arithmetic operations, we do
not and should
not expect to destroy or alter the objects(operands) in any way.
You can also make a
non-member function by following this general format
|
ObjectType operator+ ( const ObjectType &lhs , const ObjectType &rhs );
|
Notice that 2 objects are being passed by const reference instead of just one. It makes sense because this is a non-member function and it does not have access to any class object, so it requires 2 class objects to be passed to perform the arithmetic operation.
Another way is declaring a
friend function in the definition of the class
|
friend ObjectType operator+ ( const ObjectType &lhs , const ObjectType &rhs );
|
Think of this as a non-member function that is "friend" with the class and has access to its private variables. But, for our exercise here, we don't really need access to private variables, do we?
You might be asking: What's the difference? Member versus Nonmember.
As far as I can tell (Note that I'm not really an expert, so in my limited experience), it's a matter of preference. If you overload +-*/ as non-member, you need to pass both objects as parameters. If it's a member function, you only need to provide one object as a parameter. If you need to access private variables for any reason, you can use the friend function. Feel free to try all 3 methods.
Remember that function addition / subtraction requires you to find the least common multiple for the denominators. I will leave it to you to find out how to implement it.