При создании объекта выкидывает необработанное исключение. Подскажите, как правильно реализовать создание объектов через глобальный массив указателей(указатели на вновь созданные объекты 2х классов). Желательно с минимальным изменением кода.
+ я вобще не могу разобраться в этих глоб указ. (чо и как...)
Заранее спасибо. Вот код:
Код:
#include "stdafx.h" #include <iostream>
#include <cstring> #define Str 50
#define Max 5 #define Null 0
using namespace std;
bool globalCondition = false;
class Person// абстрактный базовый класс
{
static char namePers[Str];
public:
class Professor *arrayProfessor[Max];
class Student *arrayStudent[Max];
int numProfessor;
int numStudent;
static int amtPerson;
virtual ~Person(){ }
virtual void print() = Null; // чистая виртуальная функция
};
int Person::amtPerson = Null; // глобальная статическая переменная
class Person *arrayPerson[Max]; // глобальный массив указателей
class Person *ptrPerson; // глобальный указатель
class Student:public Person{
char specialty[Str]; // специальность
int course; // курс
public:
int copyCour;
char className[Str];
char copySpec[Str];
static int amtStudent;
Student(){
cout<<" Заполните информацию о студенте: "<<endl;
cout<<" Специальность: "; cin>>specialty;
cout<<" Курс: "; cin>>course;
copyCour = course;
strcpy_s(copySpec, specialty);
strcpy_s(className, "student");
}
~Student(){ cout<<endl<<" Удалён объект класса студент.";}
void print(){
cout<<endl<<" Информация о студенте: ";
cout<<endl<<" Специальность: "<<specialty;
cout<<endl<<" Курс: "<<course<<endl;
}};
int Student::amtStudent = Null;
class Professor:public Person{
int experience; // стаж работы
char degree[Str]; // учёная степень
public:
int copyExp;
char copyDeg[Str];
char className[Str];
static int amtProfessor;
Professor(){
cout<<" Заполните информацию о профессоре: "<<endl;
cout<<" Стаж работы: "; cin>>experience;
cout<<" Учёная степень: "; cin>>degree;
copyExp = experience;
strcpy_s(copyDeg, degree);
strcpy_s(className, "professor");
}
~Professor(){ cout<<endl<<" Удалён объект класса профессор. "; }
void print(){
cout<<endl<<" Информация о профессоре: ";
cout<<endl<<" Стаж работы: "<<experience;
cout<<endl<<" Учёная степень: "<<degree<<endl;
}};
int Professor::amtProfessor = Null;
int chClass(int chPerson)
{ int ch;
bool condition = false;
while(condition == false){
cout<<endl<<" Выберите класс: "<<endl;
cout<<" 1 Студент "<<endl;
cout<<" 2 Профессор "<<endl;
cout<<" "; cin>>ch;
if(ch==1 || ch==2)
condition = true;
else
cout<<endl<<" Некорректный ввод! Введите заново: ";}
return ch;
}
void addPerson(){
if(Person::amtPerson == Max){
cout<<endl<<" Вы не можете добавлять объекты! (создано 5 объектов)";
cout<<endl<<" Возврат в меню... ";
}
else{
// на этом этапе - ошибка!!!! :
int choise = Null;
if(chClass(choise) == 1){
class Student *ptrStudent = new Student;
arrayPerson[Person::amtPerson]->arrayStudent[Professor::amtProfessor] = ptrStudent;
ptrStudent->amtStudent++;
}
else{
class Professor *ptrProfessor = new Professor;
arrayPerson[Person::amtPerson]->arrayProfessor[Professor::amtProfessor] = ptrProfessor;
ptrProfessor->amtProfessor++;
}
ptrPerson->amtPerson++;
}}
void printAll(int chCondition){}/
void remowePerson(){}
void exitRemowe()// удаление глобального массива при выходе
{
for(int i=0; i<Max; i++){
delete arrayPerson[i]->arrayStudent[i];
delete arrayPerson[i]->arrayProfessor[i];
}
delete ptrPerson;
}
void menu(int chCondition)
{
cout<<endl<<endl;
cout<<" Пользовательское меню"<<endl;
cout<<" Выберите пункт из предложенных:"<<endl;
cout<<" 1 Добавить персону"<<endl;
cout<<" 2 Удалить персону"<<endl;
cout<<" 3 Содержимое объектов определённого класса"<<endl;
cout<<" 4 Содержимое всех объектов"<<endl;
cout<<" 0 Выход из программы"<<endl;
cout<<" ";
cin>>chCondition;
switch (chCondition){
case 1: { addPerson(); break;}
case 2: { remowePerson(); break;}
case 3: { printAll(chCondition); break;}
case 4: { printAll(chCondition); break;}
case 0: { exitRemowe(); globalCondition = true; break;}
default:{ cout<<endl<<" Некорректный ввод!!!"<<endl;}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "Russian");
while(globalCondition == false){
menu(Null);
}
cout<<endl<<" Работа программы окончена.";
cin.get();
cin.get();
return 0;
}
Или подскажите книжку, где этот динамический полиморфизм(с указателями ) описан максимально подробно, доступно, буду благодарен.