We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If you're going to use a friend function, don't forget the following:
1.Friend functions are NON-MEMBER functions, but they have direct access to private data members. Meaning, you don't need accessors and can instead use d.length, d.breadth, d.height.
2.Because it's considered a NON-MEMBER function, you would define it above/outside the public section of the class functions.
-The Box parameter for the overloaded << operator needs to be const and &, because we are NOT modifying the Box object, and we pass it by reference so that we don't create a brand new instance of the Box object just to use the function.
-We also want to make sure that the accessor functions (e.g. "getters") have const at the end of the function header:
int getLength() const and the reason for this is because we want to ensure that accessing the private data member in a class object does not get modified in any way, since the intention of getLength() is to just return the value of the length.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Box It!
You are viewing a single comment's thread. Return to all comments →
If you're going to use a friend function, don't forget the following:
1.Friend functions are NON-MEMBER functions, but they have direct access to private data members. Meaning, you don't need accessors and can instead use d.length, d.breadth, d.height.
2.Because it's considered a NON-MEMBER function, you would define it above/outside the public section of the class functions.
ex) class Box { friend ostream& operator<<(ostream& output, const Box& D);
public: ... ... private: ... };
Two other separate points
-The Box parameter for the overloaded << operator needs to be const and &, because we are NOT modifying the Box object, and we pass it by reference so that we don't create a brand new instance of the Box object just to use the function.
-We also want to make sure that the accessor functions (e.g. "getters") have const at the end of the function header:
int getLength() const and the reason for this is because we want to ensure that accessing the private data member in a class object does not get modified in any way, since the intention of getLength() is to just return the value of the length.