-2

I'm not sure how you code the copy constructor and "=" operator I don't know if using info[MAXITEM]=x.info[MAXITEM]; is wrong I'm still new to codding I just learned copy constructor and "=" operator so I'm not sure how to implement here.

#include <iostream>
#include <string>

using namespace std;

#define MAXITEM 100
typedef float itemType;

class List
{
    private:
        int length=0;
        itemType info[MAXITEM]; 
    public:
        List()
        {
        info[MAXITEM];  
        }
        List(const List &x)
        {
            cout<<"copy exec";//testing if copy constructor is being exec
            info[MAXITEM]=x.info[MAXITEM];
        }
        List & operator =(const List &x)
        {
            cout<<"= exec";//testing if = operator is being exec
            info[MAXITEM]=x.info[MAXITEM];
            return *this;
        }
        ~List()
        {

        }
        bool boolisthere(itemType item)const
        {
            for(int i=0;i<length;i++)
            {
                if(info[i]==item)               
                    return true;
                else
                    return false;                                   
            }           
        }
        void Insert(itemType item)
        {
            info[length]=item;
            length++;

        }
        void Delete(itemType item)
        {
            for(int i=0;i<length;i++)
            {
                if(info[i]==item)
                {
                    info[i]=info[length-1];
                    length--;   
                }   
            }

        }
        void Print()
        {
            for(int i=0;i<length;i++)
            {
                cout <<info[i]<<" ";
            }
            cout<<endl;

        }
        int Length()
        {
            return length;
        }

};

int main()
{
    List l;

    l.Insert(3.5);
    l.Insert(2.1);
    l.Insert(6.8);
    l.Insert(4.3);
    l.Insert(7.5);
    l.Insert(0.2);

    l.Length();

    l.Print();

    l.Delete(4.3);
    l.Print();

    List l2(l);//copyconstructor call
    l2.Print();

    List l3;
    l3=l; //= operator call
    l3.Print();


}
Jake Symons
  • 468
  • 2
  • 10
  • 19
Jake
  • 1
  • 7
    `info[MAXITEM]=x.info[MAXITEM];` is no good. Any hint given here would not be enough to fix your misconception. You better learn from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Feb 21 '18 at 14:21
  • 1
    `info[MAXITEM]=x.info[MAXITEM];` just copies a single item beyond the bounds of the array. – Mike Vine Feb 21 '18 at 14:21
  • can you please show me the fix with code,thanks. – Jake Feb 21 '18 at 14:27
  • Do you even need these operators (copy constructor assignment operator and destructor)? – drescherjm Feb 21 '18 at 14:28
  • 2
    tbh I could but I dont want to be responsible for someone doing cargo cult programming. I really think the best way to help you is by not providing you a working solution but refer you to good sources for learning c++ – 463035818_is_not_an_ai Feb 21 '18 at 14:29
  • 2
    Completely agree with @user463035818 You should look up arrays in your book and understand fully what that line is doing – UKMonkey Feb 21 '18 at 14:31
  • BTW, `info[MAXITEM];` in your constructor is also a bug. arrays don't work the way you think in c++. – drescherjm Feb 21 '18 at 14:32
  • I'm voting to close this question as off-topic because OP needs to read [A good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – SergeyA Feb 21 '18 at 14:46
  • @Jake • your code is very nearly correct. Just have to address a few problem areas (as already pointed out by others) and it should work. – Eljay Feb 21 '18 at 15:06
  • Welcome to Stack Overflow! You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. – Toby Speight Feb 21 '18 at 18:08

1 Answers1

2

The problem isn't your copy constructor or your copy assignment operator, the problem is that you are trying to copy the array as a sigle element. In C++ this is not possible you have to copy every single element of the array one by one. So your line info[MAXITEM]=x.info[MAXITEM] becomes

for(int i=0;i < MAXITEM;++i)
{
   info[i]=x.info[i]
}

Or if you can use STL in your program you could use the standard algorithm copy.
std::copy(std::begin(x.info), std::end(x.info), std::begin(info));

Also in the example you have posted you are trying to access the first element after the array and that's undefined behaviour!

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
blazgrom
  • 196
  • 3
  • 7