Tutorial :Array class in C++: How to use one method in another


I have a class for performing various array operations. I like to use my insert method in my populate method. Can someone guide me on that? Here is the code:

#include <iostream>  #include <cstdlib>  using namespace std;    const int MAX=5;    class array  {  private:      int arr[MAX];    public:      void insert(int pos, int num);      void populate(int[]);      void del(int pos);      void reverse();      void display();      void search(int num);  };    void array::populate(int a[])  {      for (int i=0;i<MAX;i++)      {          arr[i]=a[i];      }  }    void array::insert(int pos, int num)  {      for (int i=MAX-1;i>=pos;i--)      {           arr[i] = arr[i-1];           arr[i]=num;      }  }      void array::del(int pos)  {      for (int i=pos;i<MAX;i++)      {          arr[pos]=arr[pos + 1];      }  }    void array::display()  {      for (int i=0;i<MAX;i++)          cout<<arr[i];  }      void array::search(int num)  {      for (int i=0;i<MAX;i++)      {          if (arr[i]==num)          {              cout<<"\n"<<num<<" found at index "<<i;              break;          }          if (i==MAX)          {              cout<<num <<" does not exist!";          }      }  }    int main()  {      array a;        for (int j=0;j<MAX;j++)      {          a.insert(j,j);      }        a.populate(a);        a.insert(2,7);        a.display();        a.search(44);        system("pause");  }  


I like to use my insert method in my populate method. Can someone guide me on that?

That would mean that instead of the straightforward and efficient "copy from one array to another" approach, you'd call insert for each value of the input with the correct index in place of the assignment.

To call a method on the current instance, from inside a method:

insert(x, y);  //or  this->insert(x, y);  

Your code also contains an error, in that you pass a wrong type to populate in main. It expect int* (a real array), not an array object.


Please elaborate your question. If you just need a good container have a look at the STL (Standard Template Library) std::vector. It's part of the C++ standard and comes with your compiler.

If you want to learn how to write a custom class, please try to be more precise in your question.

Also consider the wealth of beginner tutorials available on the net, for example:


Here is a little example on how to write a custom class with one member function calling the other and accessing a private data member (note that inside a member function you can refer to any other member directly):

#include <iostream>    class Example  {    private:      int some_private_stuff;      public:      Example();      void function_a();      void function_b();  };    Example::Example(){    some_private_stuff = 1;  }      void Example::function_a(){    std::cout << "this is function a" << std::endl;    some_private_stuff = 2;    std::cout << "changed private_stuff to " << some_private_stuff << std::endl;  }    void Example::function_b(){    std::cout << "this is function b" << std::endl;    function_a();  }    int main() {    Example e;    e.function_b();    return 0;  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »