Sunday, 26 August 2012

Overloading Operator "<<" to Print Object in C++

/*Overloading << Operator to print the attribute value of a class*/

#include<iostream>
#include<cstdlib>
using namespace std;
class Square
{
    friend ostream& operator<<(ostream& output,Square& p);
    private:
            int a;
    public:    
            Square(int side)
            {
                a=side;
            }
};
ostream& operator<<(ostream& output,Square& p)
{
    output<<p.a;
    return output;
}
int main()
{
    Square s(2);
    cout<<s<<"\n";
    system("pause");
    return 1;
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
@Gnosioware Solutions