pointers using; dynamic memory allocation

This commit is contained in:
gandc 2024-05-23 02:08:14 +03:00
parent 256a939081
commit 09064192ef
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

31
pr6.cpp
View File

@ -519,31 +519,37 @@ int main() {
}
cout << endl;
//StateList
StateList states;
//StateList for 6.1
/*StateList states;*/
// Создание указателя на объект StateList for 6.2
StateList* states = new StateList();
// Пример использования
states.addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
states.addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
// for 6.1
/*states.addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
states.addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));*/
//stateList.addState(usa);
//stateList.addState(india);
//for 6.2
states->addState(State("USA", "Washington D.C.", "English", 328000000, 9833510));
states->addState(State("India", "New Delhi", "Hindi", 1380000000, 3287263));
cout << "List of states:" << endl;
cout << states.findStateByName("USA") << endl;
cout << states->findStateByName("USA") << endl;
//cout << "Removing state:" << endl;
//cout << stateList.removeState() << endl;
cout << "States with area between 9800500 and 9987263:" << endl;
states.printStatesByArea(9800500, 9987263);
states->printStatesByArea(9800500, 9987263);
cout << "States with population greater than 1 billion:" << endl;
states.printStatesByCondition([](const State& state)
states->printStatesByCondition([](const State& state)
{
return state.getPopulation() > 1000000000;
});
State* foundState = states.findStateByName("USA");
State* foundState = states->findStateByName("USA");
if (foundState) {
cout << "Found state: " << *foundState << endl;
}
@ -552,7 +558,7 @@ int main() {
}
try {
State removedState = states.removeState();
State removedState = states->removeState();
cout << "Removed state: " << removedState << endl;
}
catch (const out_of_range& e) {
@ -560,7 +566,10 @@ int main() {
}
cout << "States after removal:" << endl;
states.printStatesByArea(0, 10000000);
states->printStatesByArea(0, 10000000);
// Освобождение памяти, выделенной для объекта StateList
delete states;
return 0;
}