type class question 1

This is a discussion on type class question 1 within the C++ Programming forums, part of the General Programming Boards category; Ok If I define a new class called bike. I have two bike objects: x,y and I want the expression ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    46

    type class question 1

    Ok If I define a new class called bike. I have two bike objects: x,y and I want the expression x+y to be a new object of the class. What is the prototype of the function that i would write to use the expression x+y?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    bike operator+(const bike& lhs, const bike& rhs);
    This function might need to be a friend of your bike class if it requires access to private or protected data.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Quote Originally Posted by jlou
    Code:
    bike operator+(const bike& lhs, const bike& rhs);
    This function might need to be a friend of your bike class if it requires access to private or protected data.
    thanks jlou

    what do the lhs and rhs mean in the code you provided

    if i do delcare this as type friend then would that precede "bike" in your code?

    thanks again

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    also is this just a case of overloading operator?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Here is Sutter's canonical form of operator+:
    Code:
    T& T::operator+=( const T& other )
    {
      // ... All addition work goes here.
      return *this;
    }
    
    const T operator+( const T& a, const T& b )
    {
      T temp( a );
      temp += b;
      return temp;
    }
    Among other things, it keeps the addition code confined to the operator+= so that if you have to update it you only have to change one place. This assumes that you have a working copy constructor, or the default copy constructor is sufficient.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Quote Originally Posted by jlou
    Here is Sutter's canonical form of operator+:
    Code:
    T& T::operator+=( const T& other )
    {
      // ... All addition work goes here.
      return *this;
    }
    
    const T operator+( const T& a, const T& b )
    {
      T temp( a );
      temp += b;
      return temp;
    }
    Among other things, it keeps the addition code confined to the operator+= so that if you have to update it you only have to change one place. This assumes that you have a working copy constructor, or the default copy constructor is sufficient.


    well I am very new at c++ and that seems a little out of my element for the time being, is there a simple way to explain that? I am just trying practice excercises here, thanks, sorry if I come off as silly

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Yes, this is just operator overloading. And yes, you would precede bike with friend inside your class, then define it outside. The lhs and rhs are just variable names, you can use whatever you want. They stand for left-hand side and right-hand side of the equation.

    If you use Sutter's form (above), you wouldn't need a friend, since operator+= is a member.
    Code:
    class bike
    {
    public:
      bike() : data(0) { }
      bike& operator+=(const bike& rhs);
    private:
      int data;
    };
    
    bike& bike::operator+=(const bike& rhs)
    {
      data += rhs.data;
      return *this;
    }
    
    const bike operator+(const bike& lhs, const bike& rhs)
    {
      bike temp(lhs);
      temp += rhs;
      return temp;
    }
    [EDIT] - The code that is in blue above is what you should update for your class. The rest of the code is pretty standard and you can use it as is.

    Also, make sure you notice the typo I fixed above (should be "temp += rhs", not "lhs += rhs").
    Last edited by jlou; 10-19-2004 at 01:31 PM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    ok, so is there a more basic prototype to use then canoical form you provided? I am just taking notes on some standard prototypes in certain situations to prepare for self test

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I'm not sure what you mean by more basic. I guess a simpler approach would be my first suggestion, especially if you only want to implement operator+.
    Code:
    class bike
    {
    public:
      bike() : data(0) { }
      friend bike operator+(const bike& lhs, const bike& rhs);
    private:
      int data;
    };
    
    bike operator+(const bike& lhs, const bike& rhs)
    {
      bike temp;
      temp.data = lhs.data + rhs.data;
      return temp;
    }

  10. #10
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    hmmm. before you've gone to far, will it make sense to "add two bikes"?
    Code:
    Bike bike3 = bike2 + bike1;   // ????
    I mean, you could always overload '+' to perform anything you want, but the...??
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    One word: Tandem

  12. #12
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by sean_mackrory
    One word: Tandem
    Oh, okay. I had to look up that word in the dictionary. But, yeah, I was just checking to make sure that you don't add unnecessary, unintuitive features to your class
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  13. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Tandem bikes are the ones with two seats...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 10:43 PM
  4. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  5. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21