Thread: overloading for newbies

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    overloading for newbies

    Hello. I'm new to C++ but I have some knowledge of C. You may laugh because I don't know what overloading means. I've been doing some reading and I have the vauge idea that overloading means you have two different functions say, but with the same name. Am I in the right direction? Anyway, I need to know how to overload | and & to compute a union of sets and an intersection of sets. Please help.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    There is overloading functions and overloading operators. Overloading functions is giving two functions the same name, but having the input parameters be different. For example:
    Code:
    void DisplayData(int integerData)
    {
        cout << integerData << endl;
    }
    
    void DisplayData(float floatData)
    {
        cout << floatData << endl;
    }
    I think what you want is operator overloading. This allows you to give a new definition to an operator (like | or & or * or + or =, etc.) based on what types are used with the operator. Usually, this is used for a specific class. So if you have a class that represents a set, you could overload the operator | to combine two instances of the set. Do you already have a class that represents a set? That would be a good start.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    Ok I understand now. Thanks. But how would you go about overloading an operator? Would it be possible for you to give me a short example? Or a link to a good explanation/example?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 03:26 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM