pointers using; dynamic memory allocation

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