A
A
Alexey Mairin2017-09-09 18:20:26
C++ / C#
Alexey Mairin, 2017-09-09 18:20:26

How to call the correct constructor?

Hello!
Actually, the task was to write several types of constructors
. I wrote it, but now how to implement it in such a way that by selecting an item in the menu, let's say the copy constructor is transferred to this particular constructor? All constructors are described in 1 class (this is how I try to do it)

do 
    {
      system("cls");
      menu_next();
      cin >> triger;
      switch (triger)
      {
      case 1:
      {
        WorkWithArray obj1;
        _getch();
        break;
      }
      case 2:
      {
        WorkWithArray obj2(WorkWithArray obj1);
        _getch();
        break;
      }
      case 3:
      {
        WorkWithArray obj3;
        WorkWithArray obj4(obj3.setSecondArraySize(), obj3.setFirstArraySize());
        _getch();
        break;
      }
      /*case 4:
      {
      WorkWithArray obj5(NULL, NULL);
      }*/
      case 5:
      {
        WorkWithArray obj6(NULL, NULL);
        break;
      }
    }
    } while (triger != 123);

And here is the usual constructor
WorkWithArray()
  {
  firstArray = nullptr;
  SecondArray = nullptr;
  firstArraySize=0;
  SecondArraySize=0;
  min =0;

  //simpleConstructer
  firstArraySize = setFirstArraySize(); //конструктор инициализации
  SecondArraySize = setSecondArraySize(); //конструктор инициализации
  firstArray = CreateFirstdMemory(firstArraySize);
  SecondArray = CreateSecondMemory(SecondArraySize);
  min = compare(firstArraySize, SecondArraySize);
  LastArray = CreateLastMemory(firstArraySize,SecondArraySize,min,firstArray,SecondArray);
  OutputLastArray(LastArray,min);
  }

------------
//work with coppy constructur
  WorkWithArray(const WorkWithArray &workinArray)
  {
    int FirstArraySize = workinArray.firstArraySize;
    int SecondArraySize = workinArray.SecondArraySize;
    firstArray = CreateFirstdMemory(FirstArraySize);
    SecondArray =CreateSecondMemory(SecondArraySize);
    min = compare(FirstArraySize, SecondArraySize);
    LastArray = CreateLastMemory(FirstArraySize, SecondArraySize, min, firstArray, SecondArray);
    OutputLastArray(LastArray, min);
  }

It also describes a constructor with parameters, with constant parameters and explicit
OOP, I began to study not long ago, and honestly, I don’t know if I wrote these constructors correctly (all this code is working: D)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question