STRUCTURES AND MEMORY MANAGEMENT Structures Structures as function parameters Overloading Structure Functions Declaring Variables (Almost) Anywhere The Global Scope Resolution Operator :: The linkage Specification Structures In arrays all elements must be same data type Structure allows data of different types to be stored, accessed, manipulated using one variable name struct struct_name; { member_1_type member_1_name; member_2_type member_2_name; ... member_n_type member_n_name; } struct employee { char name[50]; int employee_number; int begin_year; float salary; char history[5000]; }; Structure is new data type -- should be declared "at top of program" along with constants Each member name must be unique within a struct Member names may be re-used in other structs -- avoid this! Types can be int, float, char, enumerated, or other structs Declaring a variable of struct type Simple Variable employee Bob; Bob.employee_number = code; Bob.salary = rate; strcpy (Bob.name, emp_name); Array employee department [SIZE]; department[k].employee_number = code; if (department[k].begin_year > 1990) {...} strcpy (department[k].history, emp_hist); Pointer employee *person; ... person = new employee; Must use struct pointer person->employee_number = code; amount = person->salary; strcpy (person->name, emp_name); Nested Structures struct date { int month; int day; int year; } struct employee { char name[50]; int employee_number; date begin; float salary; char history[5000]; date terminated; }; employee Bob; Bob.begin.month = start_month; Bob.begin.day = start_day; Bob.begin.year = start_year; date start; ... Bob.begin = start; employee department [SIZE]; ... if (department[k].terminated.year > 1990) {...} employee *person; ... person->begin.day = today; Structures of function parameters void main () { employee foreman; ... which_employee (foreman); } void which_employee (employee& emp) { cout << emp.employee_number << endl; } Use of reference means that only address of employee is passed -- not all data (including 5500 characters) Structure member functions struct employee { void which_employee (); float salary_portion (int); char name[50]; int employee_number; date begin; float salary; char history[5000]; date terminated; }; void employee::which_employee () { cout << employee_number << endl; } float employee::salary_portion (int div) { return (salary/div); } void main () { employee foreman; ... foreman.which_employee; monthly = foreman.salary_portion (12); } Overloading Structure Functions struct employee { float salary_portion (int); float salary_portion (float); char name[50]; int employee_number; date begin; float salary; char history[5000]; date terminated; }; float employee::salary_portion (int div) {return (salary/div);} float employee::salary_portion (float pct) {return (pct * salary);} void main () { employee foreman; ... monthly = foreman.salary_portion (12); quarter = foreman.salary_portion (0.25); } Declaring Variables almost Anywhere Recommended that you declare variables "at top" of function BUT ... C++ allows declaration almost anywhere as long as a variable is declared before its first reference const int MAX_PARTS=100; void main () { int part_number [MAX_PARTS]; for (int part = 0; part < MAX_PARTS; part++) { cin >> part_number [part]; } ... } The Global Scope Resolution Operator If local variable has same name as global one, only local variable may be referenced within function int abc = 123; void main () { int abc = 456; cout << abc << endl; } Output will be 456 In C++ global variable available via Global Scope Resolution Operator (::) int abc = 123; void main () { int abc = 456; cout << abc << endl; cout << ::abc << endl; } Output will be 456 and 123 The linkage Specification C++ program can incorporate as much existing, debugged, reliable C code as possible C++ compiler needs to know linkage of any function called Needs to know if any linkages are not standard C++ linkage Accomplished by using extern linkage specification extern "C" float paycheck (float, float); void main () { float worked; float pay_rate; cin >> worked << pay_rate; cout << paycheck (worked, pay_rate) << endl; }