May 2012
Intermediate to advanced
679 pages
16h 56m
English
In Program 8.9, we have used method show() to display the string. In C++ we normally use operator ‘<<’ for displaying inbuilt data types. It will be a nice idea if this works with our class STRING as well. We have seen the principle of operator overloading. Let us apply it to this case!
Overloading insertion (<<) operator
Program: Write a program to overload “<<” operator for class STRING.
Solution: See Program 8.11.
// string3.cpp #include<iostream.h> class STRING { private: int len; char a[80]; public: STRING( char * p); STRING(); int length(); friend ostream& operator<<(ostream & , STRING st) ; }; int main() ...
Read now
Unlock full access