Hibou
Hibou

Reputation: 194

Declaring an array inside a class. C++

I want to create a class that initialize an array, and a function push that adds an element to the array than print it. As a beginner, I know that something is wrong with the initialization, everything works fine except for the function push, it doesn't print the array. What's wrong with my class ?

#include <iostream>

class pile{
    public :
    int n;
    int p[]={};
    pile(int m){
        n=m;
        std::cout<<"Contructor is up .. ";
    }

    int init(int n){
        int z=n-1;
        for(int i;i<=z;i++){
            std::cin>>p[i];
        }
    }

    int push(int n, int x){
        int y=n-1;
        int p[]={};
        p[n]=x;
        for(int u=0;u<=n;u++){
            std::cout<<p[u]<<" ";
        }
    }


};

int main(){
    int a;
    std::cout<<"How many integers does your array got ?  >> ";
    std::cin>>a;

    pile p1(a);
    std::cout<<"\nEnter your array's integers >> ";
    p1.init(a);
    int j;
    std::cout<<"\nInteger that you want to add to the array ? >> ";
    std::cin>>j;
    std::cout<<"\nThe new array is >> ";
    p1.push(a,j);

    return 0;
}

Upvotes: 0

Views: 36509

Answers (2)

Egor Richman
Egor Richman

Reputation: 679

So, sometimes is better to working with memory stuff, that's why you code may seems like code below:

#include <iostream>
using namespace std; // std:: namespace usage is not needed after that

class pile{

    int n; // it's a not best practice to declare this as public
    int* p; // better way to hide this fields include them in to private

public :

    pile(int m){
        n=m;
        p = new int[n];
        cout<<"Constructor is up .. ";
    }
    ~pile(){
      if(p != nullptr){
        delete p;
      }
    }

    void init(int n){
        for(int i = 0;i < n; i++){
            cin>>p[i];
        }
    }

    int push(int j, int x){
        int oldValue = p[j];
        if(j >= 0 && j < n) { // this checking is necessary
          p[j]=x;
        } else if (j == n) {
          // you need to reset memory
          // there are different ways to do that, for example
          int* q = new int[n + 1];
          int i = 0;
          for(; i < n; i++) {
            q[i] = p[i];
          }
          q[i] = x;
          delete p;
          p = q;
          n++;
        }
        // you are forget the return statement
        return oldValue;
    }

    friend ostream& operator<<(ostream& os, const pile& pil);
};

// it's most common way to use standard output to draw your class content
ostream& operator<<(ostream& os, const pile& pil) {
  for(int i = 0; i < pil.n; i++){
    os << pil.p[i] << " ";
  }
  return os;
};
// also you may override >> operator to swap your init fun.

int main(){
    int a;
    cout<<"How many integers does your array got ?  >> ";
    cin>>a;

    pile p1(a);
    cout<<"\nEnter your array's integers >> ";
    p1.init(a);
    int j;
    cout<<"\nInteger that you want to add to the array ? >> ";
    cin>>j;
    p1.push(a,j);
    cout<<"\nThe new array is >> ";
    cout << p1 << endl; // looks very nice for now

    return 0;
}

Upvotes: 1

eerorika
eerorika

Reputation: 238341

I want to create a class that initialize an array,

An example of class with an array member with value initialisation:

struct foo {
    int member[10] = {};
};

What's wrong with my class ?

This is wrong:

int p[]={};

Firstly, a member may not be an array of unspecified length, even if it has an initialiser. Secondly, no variable may be a zero length array.

and a function push that adds an element to the array than print it

It is not possible to add elements into an array. An array has a constant number of elements through its lifetime.

What you're probably looking for is a data structure that allocates an array dynamically, and copies the elements into a new, larger array upon adding more elements. There is more to the data structure than this terse description. There is a standard container that implements this "resizable array" data structure: std::vector.

Upvotes: 7

Related Questions