Thread: Series of class questions #1

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Series of class questions #1

    Hey, I hope that this will be specific enough, but here it goes:

    I'd like to overload the + operator in my class called List. It is called in the main like this:
    Code:
    List c,d;
    ....data....
    d = d + c;
    The code I have for the fxn is this:
    Code:
    List operator+(List& l1, List& l2)
    {
    List sum;
    sum=l1.mylist[]+l2.mylist[];
    return sum;
    }
    This code is outside of the class List. I'm sure that the syntax for the arrays are wrong, that is another question. any help would be much appreciated.... more to come...

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What are you trying to do? Are you trying to combine the two arrays to create a big one, or do you want to add each element of each array together, or what? Also, post up more code, like the class declarations and definitions.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    thanks for the reply. I'm trying to combine the 2 to make one giant array.

    class:
    Code:
    class List
    {
    public:
    typedef int value_type;
    static const value_type CAP=30;
    value_type mylist[CAP];
    List();
    List(List& x);
    //void destroy();
    //void assign();
    void remove();  //destroy operation
    void first(); //first operation
    void next(); //next operation
    void prev(); //previous operation
    void Makecurrent(); //makecurrent operation
    value_type examine(); //examine operation
    void replace(value_type v); //replace operation
    value_type insertafter(value_type v); //insert after operation
    value_type insertbefore(value_type v); //insert before operation
    value_type size(); //Count operation
    friend ostream & operator<<(ostream& out, List x)
    {
    return out << x;
    }
    
    private:
    int pos;
    int used;
    };
    List operator+(List& l1, List& l2)
    {
    List sum;
    sum=l1.mylist[]+l2.mylist[];
    return sum;
    }
    
    List::List()
    {
    pos=0;
    used=0;
    }
    List::List(List& x)
    {
    pos=x.pos;
    used=x.used;
    }
    void List::remove()
    {
    
    }
    void List::first()
    {
    pos=mylist[0];
    
    }
    void List::next()
    {
    if(pos<used)
    pos++;
    }
    void List::prev()
    {
    if(pos>used)
    {pos--;}
    pos--;
    }
    List::Makecurrent()
    {
    
    }
    value_type List::examine()
    {
    return pos;
    
    }
    void List::replace(value_type v)
    {
    
    
    }
    value_type List::insertafter(value_type v)
    {
    	
    if(used>CAP)
    	return 0;
    for(int i=used;i>pos;i--)
    	mylist[i+1]=mylist[i];
    	pos++;
    	mylist[pos+1]=v;
    	used++;
    	return 1;
    }
    value_type List::insertbefore(value_type v)
    {
    if(used==0)
    	return 0;
    //for(int i=)
    }
    value_type List::size()
    {
    return used;
    
    }
    Let me know if you need more info... Thanks again

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Yep, this is certainly do-able. First thing though, you might want to increase CAP a bit, just in case. Here we go:

    [code]
    List operator+(List& l1, List& l2)
    {
    List sum;
    int nCount=l1.size()+l2.size();


    for (int i=0;i<l1.size();i++)
    sum.mylist[i]=l1.mylist[i];

    for (;i<nCount;i++)
    sum.mylist[i]=l2.mylist[i-l1.size()];

    return sum;
    }

    That should be approximately it, you may have to fiddle around to ensure that the correct elements are being used. If you cant do that, you shouldn't be programming at this level yet.

    I'm sorry, but I can't work with your class very well because I don't think it's a very good system. You need to add a member function to simply add a value on the end, and you may want to think about linked lists so that you will always have enough space for all your elements, ie, you wont have to CAP your array. If you insist on continuing with your class, and have problems, then I'll try to help.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Wait, you don't know how tu use arrays, yet want to implement a class operator? Have you been doing some pure object programming things before?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Class Questions
    By shane1985 in forum C++ Programming
    Replies: 7
    Last Post: 10-29-2003, 10:34 AM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM