Lab 7 CS120 CS120 Programming 2 1 • Equivalent functionally. CS120 Function with constant parameters • In the second definition, You can’t modify the parameter a . 2 CS120 Dynamic Memory Management 3 Dynamic Memory Management • There may be cases where the memory needs of a program can only be determined during runtime. • On these case, programs need to dynamically allocate memory, for which the C++ language integrates the operators new and delete. CS120 • Defining the variables before program execution determined memory needs. 4 Dynamic Memory Management (creating object at run-time) #include <iostream> int main() { int number = 1; switch(number){ case 1: { a a1; using namespace std; cout<<"obj created "<<a1.num<<endl; break; } case 2: { cout<<"nothing"; break;} } return 0; class a{ public: int num; a() {num=1;} }; } CS120 • EX: 5 Array of objects • The "identifier" used to refer the array of objects is an user defined data type. CS120 • Arrays of variables of type "class" is known as "Array of objects". 6 CS120 Example 7 8 CS120 Write C++ program that define the following classes: Employee - ID: int - name: string + Employee () + setID(in id:int) + setName(in n:string) + getID() + getName() In the main function: Define 3 Employee objects. Define EmployeeArray which is has the 3 objects. Let the user determine the id and name . Print all 3 employee's information. CS120 • 9