#include <iostream> #include <conio.h> #include <windows.h> using namespace std; class Massiv1 { protected: float *A; int n; public: Massiv1(); ~Massiv1(); void EnterParam(); void DisplayParam(); void Product(); }; class Massiv2: public Massiv1 { public: Massiv2(); ~Massiv2(); void LoadFromFile(); }; int main() { setlocale(LC_ALL, "rus"); Massiv1 massiv1; Massiv2 massiv2; Massiv2 *mass; int key1; cout << "|--\t Main Menu\t--|" << endl; cout << "1 - Obicnuy massiv" << endl; cout << "2 - iz faula" << endl; cout << "ESC - exit" << endl; key1 = getch(); switch(key1) { default: system("cls"); cout << "---------------" << endl << "Error! Wrong tags" << endl << "---------------" << endl; break; case '1': system("cls"); mass = &massiv1; break; // Тут ошибка case '2': system("cls"); mass = &massiv2; break; } system("pause"); return 0; } 
  • @pavel task: Create the base class “array1”, describing in it the functions of inputting an array from the keyboard, outputting it to the screen, and finding the product of the elements of an array. To generate from it the class “array2”, redefining in it the function of inputting an array so that the values ​​of the elements of the array are loaded from a file. - kexozir
  • mass announce as Massiv1. But here the methods are strangely named. Logic to use virtual methods necessary. - pavel
  • one
    @pavel And why use virtual void if these methods are not redefined in the descendant class? - kexozir

1 answer 1

Pay attention to the fragment:

 Massiv1 massiv1; Massiv2 massiv2; Massiv2 *mass; //... 

And after that, remember that you can assign the address of the object (pointer) of the derived class to the pointer of the parent class, but not vice versa. Therefore:

 case '1': system("cls"); mass = &massiv1; break; 

is an error, as mass (pointer to Massiv2) cannot contain the address of an Massiv1 type Massiv1