Answer the question
In order to leave comments, you need to log in
How to create a dynamic array of pointers in the base class to derived class objects?
Good evening!
The essence of the task is this: Create a base class Array, declare an array field of the appropriate type and a variable that stores the size of the current array object in it. Implement the Person class as a derived class from the Array class, use public inheritance.
How can this be done? If I declare an array of the Person class in the Array class, I get errors:
Error C2065 list: undeclared identifier
Error C2061 syntax error: identifier 'Person'
#include <iostream>
#include "Class_Person.h"
class Class_Array
{
int count = 1;
public:
Class_Array()
{
for (std::size_t i = 0; i < count; i++)
list[i] = 0;
}
~Class_Array()
{
}
private:
Person** list = new Person * [count];//
};
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "Class_Date.h"
#include "Class_Array.h"
class Person:public Class_Array
{
bool check_create = false;
bool check_nam = true;
public:
Person(Person**& list, int &count)
{
for (std::size_t i = 0; i < count; i++)
list[i] = 0;
}
Person()
{
name = "";
}
~Person()
{
cout << "Сработал деструктор" << endl;
cout << "//===================================================" << endl;
}
void set_name(string name)
{
this->name = name;
}
string get_name()
{
return name;
}
bool check_name(Person**& list,int &count, string check_name_,bool &check_nam)
{
for (int i = 0; i < count; i++)
{
if (check_name_ == list[i]->name)
{
cout << "Такая фамилия уже существует в списке!" << endl;
return check_nam = true;
}
else
{
return check_nam = false;
}
}
}
void choose_fun(Person**& list, int &count)
{
bool check = true;
int choice = 0;
while (check)
{
cout << "Выберите функцию:\n1.Создать список\n2.Заполнить\n3.Показать\n4.Добавить\n5.Удалить\n6.Выход" << endl;
cin >> choice;
switch (choice)
{
case 1:
create(list,check_create, count);
break;
case 2:
system("cls");
fillin(list, count);
break;
case 3:
system("cls");
show(list, count);
break;
case 4:
system("cls");
add(list, count);
break;
case 5:
system("cls");
del(list,count);
break;
case 6:
clear(list, count);
cout << "Выход" << endl;
check = false;
break;
default:
cout << "Ошибка ввода!" << endl;
break;
}
}
}
void fillin(Person**& list, int& count)
{
cin.clear();
if (check_create)
{
for (int i = count-1; i < count; i++)
{
if (i>0)
{
while (check_nam)
{
cout << "Введите фамилию: ";
cin >> list[i]->name;
check_name(list,count,name,check_nam);
}
check_nam = true;
}
else
{
cout << "Введите фамилию: ";
cin >> list[i]->name;
cin.clear();
}
cout << "Введите дату: " << endl;
list[i]->data.read();
}
}
else
{
cout << "\n***Создайте сначала список!***\n" << endl;
}
}
bool create(Person**&list,bool & check_create, int &count)
{
for (int i = 0; i < count; i++)
{
list[i] = new Person;
}
cout << "Список создан! В списке " << count << " персона" << endl;
return check_create = true;
}
int add(Person** &list, int &count)
{
if (check_create)
{
Person** list_new = new Person * [count + 1];
for (int i = 0; i < count; i++)
{
list_new[i] = list[i];
}
count++;
list_new[count - 1] = new Person;
fillin(list_new, count);
delete[]list;
list = list_new;
cout << "Персона добавлена" << "(+" << count - 1 << ")" << endl;
return count;
}
else
{
cout << "\n***Создайте сначала список!***\n" << endl;
}
}
int del(Person**& list, int& count)
{
if (check_create)
{
int number;
cout << "Выберите кого необходимо удалить!(Введите порядковый номер)" << endl;
show(list, count);
cin >> number;
Person** list_new = new Person * [count - 1];
for (int i = 0; i < count; i++)
{
if (number == 1)
{
list_new[i] = list[i+1];
}
else if (i == (number - 1))
{
list_new[i - 1] = list[i];
}
}
count--;
delete[]list;
list = list_new;
return count;
}
else
{
cout << "\n***Создайте сначала список!***\n" << endl;
}
}
void show(Person**& list, int& count)
{
if (check_create)
{
for (int i = 0; i < count; i++)
{
cout << i + 1 << ". Фамилия - " << list[i]->name;
list[i]->data.display();
cout << endl;
}
}
else
{
cout << "\n***Создайте сначала список!***\n" << endl;
}
}
void clear(Person**& list, int &count)
{
for (int i = 0; i < count; i++)
{
delete[]list[i];
}
}
private:
std::string name;
Date data;
};
Answer the question
In order to leave comments, you need to log in
You clearly misunderstood the problem. You do not need to solve the question in the title.
You need to write a vector class and its successor - Person.
Nobody asked to create a vector containing pointers to Person.
If this is the entire text of the task, I can offer you int as a "suitable type".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question