proper syntax in converting friend functions to class functions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: proper syntax in converting friend functions to class functions

  1. #1
    floyd Guest

    proper syntax in converting friend functions to class functions


    How do I convert these to member functions

    friend ostream& operator<< (ostream& c, positive_fraction f1);
    {
    if (f1.top == 0)co <<0;
    if (f1.top >=f1.bottom)
    {
    co << f1.top / f1.bottom !=0;
    if (f1.top % f1.bottom !=0)
    co << "and";
    }
    if (f1.top % f1.bottom != 0)
    co << f1.top % f1.bottom << "/" << f1.bottom;
    return co;
    }


    friend istream& operator>> (istream& c, positive_fraction& f1);

    {
    char ch;
    int s,t;
    c>>s>>ch>>t;
    if (ch !='/') return c;
    f1.assign (s,t);
    return c;
    }

  2. #2
    marilyn Guest

    Re: proper syntax in converting friend functions to class functions

    You can't. operator<< and operator>> require the first operand to be an
    ostream or istream object. If you make it a member, the first operand will
    be the implicit 'this'.


    "floyd " <floyd_ferret@hotmail.com> wrote in message
    news:3ba22046$1@news.devx.com...
    >
    > How do I convert these to member functions
    >
    > friend ostream& operator<< (ostream& c, positive_fraction f1);
    > {
    > if (f1.top == 0)co <<0;
    > if (f1.top >=f1.bottom)
    > {
    > co << f1.top / f1.bottom !=0;
    > if (f1.top % f1.bottom !=0)
    > co << "and";
    > }
    > if (f1.top % f1.bottom != 0)
    > co << f1.top % f1.bottom << "/" << f1.bottom;
    > return co;
    > }
    >
    >
    > friend istream& operator>> (istream& c, positive_fraction& f1);
    >
    > {
    > char ch;
    > int s,t;
    > c>>s>>ch>>t;
    > if (ch !='/') return c;
    > f1.assign (s,t);
    > return c;
    > }




  3. #3
    James Curran Guest

    Re: proper syntax in converting friend functions to class functions

    // public, member function
    positive_fraction::Display(ostream& co) const
    {
    // as below
    }

    // non-member, non-friend
    ostream& operator<<(ostream& co, const positive_fraction& f1)
    {
    f1.Display(os);
    return os;
    }

    istream is left as an exercise for the student.....

    --
    Truth,
    James Curran
    www.NJTheater.com (Professional)
    www.NovelTheory.com (Personal)
    www.BrandsForLess.Com (Day Job)


    "floyd " <floyd_ferret@hotmail.com> wrote in message
    news:3ba22046$1@news.devx.com...
    >
    > How do I convert these to member functions
    >
    > friend ostream& operator<< (ostream& c, positive_fraction f1);
    > {
    > if (f1.top == 0)co <<0;
    > if (f1.top >=f1.bottom)
    > {
    > co << f1.top / f1.bottom !=0;
    > if (f1.top % f1.bottom !=0)
    > co << "and";
    > }
    > if (f1.top % f1.bottom != 0)
    > co << f1.top % f1.bottom << "/" << f1.bottom;
    > return co;
    > }
    >
    >
    > friend istream& operator>> (istream& c, positive_fraction& f1);
    >
    > {
    > char ch;
    > int s,t;
    > c>>s>>ch>>t;
    > if (ch !='/') return c;
    > f1.assign (s,t);
    > return c;
    > }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center