#include<iostream.h>
#include<conio.h>

using namespace std;
class Array

{
      private:
              int* ptr;
              int size;
              
      public:
             Array()  :ptr(0), size(0)
             {  }
             Array (int s) : size(s)
             { ptr= new int[s];   }
             
             Array(Array&);
             
             ~Array()
             { delete[] ptr;}
             
             int& operator [] (int j)
             {
                  return *(ptr+j);
                  }
                  
             Array& operator = (Array&);
             };
            // ...........................................................................
             Array::Array(Array& a)
             {
                  size=a.size;
                  ptr=new int[size];
                  for(int j=0; j<size; j++)
                  *(ptr+j)=*(a.ptr+j) ;
                
                  }
                  
             Array& Array::operator = (Array& a)
             
             { 
                    delete[] ptr;
                    size=a.size;
                    ptr=new int[a.size];
                    for(int j=0; j<a.size;j++)
                    *(ptr+j)=*(a.ptr+j);
                    return *this;
                    }
                  ////////////////////////////////////////////////
                  main()
                  { 
                      const int asize=10;
                      Array arr1(asize);
                      for(int j=0; j<=asize; j++)
                      {arr1[j]=j*j;
                      
                      
                      cout<< arr1[j]<<"     "; }
                      
                     Array arr2(arr1);
                     
                      cout<<"\narr2: "  ;
                      for (int j=0; j<asize; j++)
                     
                      cout<< arr2[j]<< "   " << endl ; 
                                      
                      Array arr3, arr4;
                      arr4=arr3=arr1; 
                      
                       cout<<"\narr3: "  ;
                               for (int j=0; j<=asize;j++)
                      cout<<arr3[j] << " " ;
                      
                      cout<<"\narr4: "  ;
                         for (int j=0; j<asize; j++)
                          cout<<arr4[j] << "   " ;
                         // getch();
                          cout<< endl;
                          
                   return 0;
                          getch();
                          }
Comments
presumptious twit

The error is in the chair.
The error didn't read "how to post code" threads all over the forum.
The error needs to do better at making a post which is readable.

The code was really hard to read...

When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

You have a couple of places where your loops are wrong:

// This type is wrong, it will go one too far.
for(int j=0; j<=asize; j++)
// This type is the right way to do it.
for(int j=0; j<asize; j++)

And your 'print the contents of an array' code that you keep repeating might make another good member for your Array class.

You also write code this way quite a bit:

for(int j=0; j<a.size;j++)
*(ptr+j)=*(a.ptr+j);

Do you realize that this is functionally equivalent to the much easier to read:

for (int j = 0; j < a.size; j++) {
    ptr[j] = a.ptr[j];
}

(I added the braces to the for loop because I like them)

Which of your many output functions isn't working?

Do any of them work?

What output do you have (if any)?

What do you think is wrong? (Make a guess if you need to)

The error is in the chair.
The error didn't read "how to post code" threads all over the forum.
The error needs to do better at making a post which is readable.

:)
I solved my problem and now its working. thanks for your help

#include<iostream.h>
#include<conio.h>

using namespace std;
class Array

{
      private:
              int* ptr;                                     // pointer to array contents
              int size;                                     // size of an array

      public:
             Array()  :ptr(0), size(0)                         // no argument constructor
             {  }
             Array (int s) : size(s)                            // one argument constructor
             { ptr= new int[s];   }

             Array(Array&);                                   // copy constructor

             ~Array()                                         // destructor
             { delete[] ptr;}

             int& operator [] (int j)              // overloaded subscript operator
             {
                  return *(ptr+j);
                  }

             Array& operator = (Array&);                      // overloaded assignment operator
             };
            // ...........................................................................

             Array::Array(Array& a)                          // copy constructor
             {
                  size=a.size;                                 // new one is same size

                  ptr=new int[size];                          // get space for contents

                  for(int j=0; j<size; j++)                    // copy contents to new one

               //   *(ptr+j)=*(a.ptr+j) ;
                  ptr[j] = a.ptr[j];                     


                  }

             Array& Array::operator = (Array& a)           // overload = operator

             { 
                    delete[] ptr;          // delete old content if any

                    size=a.size;               // make this object same size

                    ptr=new int[a.size];          // get space for contents

                    for(int j=0; j<a.size;j++)     // copy contents to new one  
                   // *(ptr+j)=*(a.ptr+j);
                   ptr[j] = a.ptr[j]; 
                    return *this;          // return this object
                    }
                  ////////////////////////////////////////////////
                  main()
                  { 
                      const int asize=10;                // size of array

                      Array arr1(asize);                    // make an array

                      for(int j=0; j<asize; j++)                // fill it with squares
                      {arr1[j]=j*j;


                      cout<< arr1[j]<<"     "; }

                     Array arr2(arr1);                   // use the copy constructor

                      cout<<"\narr2: "  ;

                      for (int j=0; j<asize; j++)                 // check that it worked

                      cout<< arr2[j]<< "   " ; 

                      Array arr3, arr4;             // make two empty Array objects

                      arr4=arr3=arr1;                 // use the assignment operator

                       cout<<"\narr3: "  ;
                               for (int j=0; j<asize;j++)  // check that it worrked on arr3
                      cout<<arr3[j] << " " ;

                      cout<<"\narr4: "  ;
                         for (int j=0; j<asize; j++)        // check that it worrked on arr3              
                          cout<<arr4[j] << "   " ;

                          cout<< endl;

                   //return 0;
                          getch();
                          }

Edited 3 Years Ago by pritaeas: Fixed formatting

Comments
Oh dear :\
This article has been dead for over six months. Start a new discussion instead.