
174 C++ Programming
out << "Mass :" << c.mass << endl;
out << "Accurate Density :" << c.accurate_density << endl;
return out;
}
4.6 The friend function breaks encapsulation. Instead of declaring operator << func-
tion as a friend of CuboidMetal class, it was possible to provide inline functions such as
getLength,getHeight. What is the feasibility of this idea?
Ans. Yes, friend function breaks encapsulation. However, if functions such as getLength,
getHeight are provided in a class CuboidMetal, any user of CuboidMetal class can access values
of these attributes directly which also breaks encapsulation. Hence, use of friend function is
better because ...