N
N
Nonobeki2022-04-02 11:42:34
C++ / C#
Nonobeki, 2022-04-02 11:42:34

Empty C++ array, how to solve?

I wrote a program to learn OOP.
There is a field 5 by 5, at the beginning there is an installation of all objects on the field.
Question: why is the array empty?

My code
#include <iostream>
class Poligon {
private: int G[5][5] = { 0 };
public: Poligon() {

}
        
        void PoligonClear() {
        //..обнулить G
            for (int i = 0; i != 5; i++) {
                for (int d = 0; d != 5; d++) {
                    Poligon::G[i][d] = 0;
                }
            }
        };
      
      

      void PrintPoligon() {
          for (int i = 0; i != 5; i++) {
              printf("%d | ", i + 1);

              for (int d = 0; d != 5; d++) {
                  printf("%d ", G[i][d]);
              }

              printf("\n");
          }
      }

      void Postavit(int i, int j, int x) {
          G[i][j] = x;
          printf("i: %d, j: %d, x: %d\n", i, j, x);
          //PrintPoligon();
      }
      
      void GetInfo() {

          PrintPoligon();
      }
};

class Cat {

public: int Age = 1;
    void Setage(int v) {
    Age = v;
};
      int Getage(void) {
          return Age;
      };
      void Gulat(Poligon X, int i, int j) {
          X.Postavit(i, j, Age);
      }
};

class Dog {

public: int DogAge = 1;
      void Setage(int v) {
          DogAge = v;
      };
      int Getage(void) {
          return DogAge;
      };
      void Gulat(Poligon X, int i, int j) {
          X.Postavit(i, j, DogAge);
      };
};

int main() {
    Poligon W;

    Cat Barsik;
    Barsik.Setage(2);
    Barsik.Gulat(W, 1, 2);

    Dog Tusik;
    Barsik.Setage(3);
    Tusik.Gulat(W, 1, 3);

    W.GetInfo();

    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
15432, 2022-04-02
@Nonobeki

Because you don't have Setage calls, and in Gulat() you get Postavit(i, j, 0)

R
Rsa97, 2022-04-02
@Rsa97

An empty array is an array with no elements. And your array is not empty, but filled with zeros.
Well, the problem is that the initial Age value is not set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question