Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Passing an object as an argument in custom manipulator

varun_789varun_789 Posts: 7Member
hello friends,

i want to pass an object as an argument as follows.

#include
#include
using namespace std;

class myclass
{
public :
int m,n;

myclass(){m=10;n=12;}
};

ostream & disp( ostream & output,myclass &obj)
{
output<<"Hello"<<endl;
output<<obj.m;
return output;
}

void main()
{
myclass myobjct;
cout<<disp<<myobjct;
}


Above code does not run.
How to achieve above feature ?

plase help me. its urgent.
thank you.

Comments

  • ledaslledasl Posts: 30Member
    : hello friends,
    :
    : i want to pass an object as an argument as follows.
    :
    : #include
    : #include
    : using namespace std;
    :
    : class myclass
    : {
    : public :
    : int m,n;
    :
    : //myclass(){m=10;n=12;} // this not error
    [red]
    myclass() : m(10), n(12) {} // but this better
    [/red]
    : };
    :
    : ostream & disp( ostream & output,myclass &obj)
    : {
    : output<<"Hello"<<endl;
    : output<<obj.m;
    : return output;
    : }
    :
    : void main()
    : {
    : myclass myobjct;
    : //cout<<disp<<myobjct;
    [red]
    cout << disp(cout, myobjct);
    [/red]
    : }
    :
    :
    : Above code does not run.
    : How to achieve above feature ?
    :
    : plase help me. its urgent.
    : thank you.
    :

  • varun_789varun_789 Posts: 7Member
    hi,

    i tried to pass the object the way you specified but
    at the end of the output, it gives some garbage value.

    here is the code that i executed.

    #include
    #include
    using namespace std;

    class myclass
    {
    public :
    int m,n;

    myclass(){m=10;n=12;}
    };

    ostream & disp( ostream & output,myclass &obj)
    {
    output<<"m="<<obj.m<<endl;
    output<<"n="<<obj.n<<endl;
    return output;
    }

    void main()
    {
    myclass myobjct;
    //cout<<disp<<myobjct;
    cout<<disp(cout, myobjct)<<endl;
    }


    here is the output :
    m=10
    n=12
    004777E4 // this is the garbage value
    Press any key to continue

    What is the problem ?
  • DonotaloDonotalo Posts: 715Member
    : hi,
    :
    : i tried to pass the object the way you specified but
    : at the end of the output, it gives some garbage value.
    :
    : here is the code that i executed.
    :
    : #include
    : #include
    : using namespace std;
    :
    : class myclass
    : {
    : public :
    : int m,n;
    :
    : myclass(){m=10;n=12;}
    : };
    :
    : ostream & disp( ostream & output,myclass &obj)
    : {
    : output<<"m="<<obj.m<<endl;
    : output<<"n="<<obj.n<<endl;
    : return output;
    : }
    :
    : void main()
    : {
    : myclass myobjct;
    : //cout<<disp<<myobjct;
    : cout<<disp(cout, myobjct)<<endl;
    : }
    :
    :
    : here is the output :
    : m=10
    : n=12
    : 004777E4 // this is the garbage value
    : Press any key to continue
    :
    : What is the problem ?
    :
    [purple]
    the function disp() is showing the values of obj.m and obj.n. this function also returned an ostream type object by reference. ur line of code

    [b][black]cout<<disp(cout, myobjct)<<endl; [/b][/black]

    is showing the ostream object cout (since it is returning by ur disp() function). this is not the garbage value. also not a meaningful one in this case.

    probably u were wanting an operator function to display the object directly via cout <<. then u should have the following function instead of disp():[code]
    ostream & [blue]operator <<[/blue]( ostream & output, const myclass &obj)
    {
    output<<"m="<<obj.m<<endl;
    output<<"n="<<obj.n<<endl;
    return output;
    }
    [/code]
    look i have made the obj constant inside the operator function. it is always a good idea to pass a constant reference in a function when the object should not change its value inside that function.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.