Vectors, Enumeration, Overloading

Vectors, Enumeration, Overloading
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Your Website URL(Optional)
Comment
CIS 190: C/C++ Programming Lecture 9 Vectors, Enumeration, Overloading, and More 1 Outline • Access Restriction • Vectors • Enumeration • Operator Overloading • New/Delete • Destructors • Homework & Project 2 Principle of Least Privilege • what is it? 3 Principle of Least Privilege • every module – process, user, program, etc. • must have access only to the information and resources – functions, variables, etc. • that are necessary for legitimate purposes – (i.e., this is why variables are private) 4 Access Specifiers for Date Class class Date public: void OutputMonth(); int GetMonth(); int GetDay(); int GetYear(); void SetMonth(int m); void SetDay (int d); void SetYear (int y); private: int m_month; int m_day; int m_year; ; 5 Access Specifiers for Date Class class Date public: void OutputMonth(); int GetMonth(); int GetDay(); int GetYear(); should all of these void SetMonth(int m); functions really be void SetDay (int d); publicly accessible? void SetYear (int y); private: int m_month; int m_day; int m_year; ; 6 Outline • Access Restriction • Vectors • Enumeration • Operator Overloading • New/Delete • Destructors • Homework & Project 7 Vectors • similar to arrays, but much more flexible – C++ will handle most of the “annoying” bits • provided by the C++ Standard Template Library (STL) – must include vector to use 8 Declaring a Vector vector int intA; – empty integer vector, called intA intA 9 Declaring a Vector vector int intB (10); – integer vector with 10 integers, initialized (by default) to zero 0 0 0 0 0 0 0 0 0 0 intB 10 Declaring a Vector vector int intC (10, -1); – integer vector with 10 integers, initialized to -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 intC 11 Vector Assignment • unlike arrays, can assign one vector to another – even if they’re different sizes – as long as they’re the same type intA = intB; 12 Vector Assignment • unlike arrays, can assign one vector to another – even if they’re different sizes – as long as they’re the same type intA = intB; size 0 size 10 (intA is now 10 elements too) 13 Vector Assignment • unlike arrays, can assign one vector to another – even if they’re different sizes – as long as they’re the same type intA = intB; size 0 size 10 (intA is now 10 elements too) 0 0 0 0 0 0 0 0 0 0 intA 14 Vector Assignment • unlike arrays, can assign one vector to another – even if they’re different sizes – as long as they’re the same type intA = intB; size 0 size 10 (intA is now 10 elements too) intA = charA; 15 Vector Assignment • unlike arrays, can assign one vector to another – even if they’re different sizes – as long as they’re the same type intA = intB; size 0 size 10 (intA is now 10 elements too) intA = charA; NOT okay 16 Copying Vectors • can create a copy of an existing vector when declaring a new vector vector int intD (intC); -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 intC 17 Copying Vectors • can create a copy of an existing vector when declaring a new vector vector int intD (intC); -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 intC -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 intD 18 Accessing Vector Members • we have two different methods available • square brackets: intB2 = 7; • .at() operation: intB.at(2) = 7; 19 Accessing Vector Members with • function just as they did with arrays in C for (i = 0; i 10; i++) intBi = i; 20