hy! I am writing an integer array class. but there is problem to return array from getter function. any one tell me the procedure of this task.

And tell about extract operator for that class.

#include<iostream>
using namespace std;

 class integer{
       friend ostream& operator<<(ostream& output, const integer& num);

       private:
               int array[10];
               
               
               
       public:        
       
               integer();  //default constructor
               integer(int arr[], int n);  // overloaded constructor
               
               void setArrayElements(int arr[], int n); // setter function for array elements
               integer getArray(); // getter for array elements
               integer operator * (const integer &arrb)const;
               
               
              void display(); // display array elements
                           
       
       
       };

 main()
 {
       
       int arrayb[10];
       for(int i = 0; i < 10; i++ )
          {
              cout << "Enter value =  ";
              cin >> arrayb[i];
               }
            
        integer a(arrayb, 10); 
        
        a.display();
        cout<<endl;
        system("pause");
       
       }
 
 
 
 integer::integer()
    {
          for(int i = 0; i < 10; i++ )
              array[i] = 0;         
                   
                   }

 integer::integer(int arr[], int n)
  {
       setArrayElements(arr, 10);               
                      }
                      
 void integer::display()
    {  cout<<endl;
    
       for(int i = 0; i < 10; i++)                 
       cout<<"\t"<<array[i]<<endl;
                        }                    

 
 void integer::setArrayElements(int arr[], int n)
   {
        for(int i = 0; i < 10; i++)
          array[i] = arr[i];               
                                
                                 }

 
integer integer::operator * (const integer &arrb)const
  {
        for(int i = 0; i < 10; i++)
          {
                integer temp;
                
                }
        }

integer integer:: getArray() // getter for array elements
  {
     // here what to do?
                }

Hi,

this is the simplest and fastest way:

declaration:

int* getArray(); // getter for array elements

implementation:

int* integer:: getArray() // getter for array elements
{
  return array;
}

You should try looking into operator overloading:

int* operator >> (int *RHS)
{
   RHS = this->array;
   return RHS; //For chaining
}

You should try looking into operator overloading:

int* operator >> (int *RHS)
{
   RHS = this->array;
   return RHS; //For chaining
}

Your operator overloading would allow this obscure code :

int * a = {1,2,3,4,5};
Integer num = Integer(a,5);
num >>(a)

which to me dosen't make sense that much. It would be hard to read.

@OP: Don't provide a way to return the internal array. Instead make the class
an array, by overloading either[] or () operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.