1. Abstract Data Type
-
Object Oriented Programming: encapsulation, polymorphism, inheritance
2. Class Example - string
-
#include <iostream> #include <string> using namespace std; int main() { string s = "abcd"; cout << "s = " << s << ", length = " << s.length() << endl; cout << "s.at(1) = " << s.at(1) << endl; s.at(1) = 'Z'; s.push_back('E'); cout << "s = " << s << ", length = " << s.length() << endl; } output: s = abcd, length = 4 s.at(1) = b s = aZcdE, length = 5
3. Defining a Class
-
output: My favorite restaurants: Central Deli -- 4 Friends Cafe -- 5
4. Inline Member Functions
-
- inline is a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call.
-
A function defined inside a class is implicitly an inline function.
The inline keyword explicitly declares a function to be an inline function.
5. Set and Get
-
Can also have private "helper" functions, used only within the class.
6. Data Initialization
-
Setting initial values for data members:
7. Constructors
8. Constructor Overloading
-
Can have multiple constructors with different arguments.
The constructor with matching parameters will be called:
class Restaurant { public: Restaurant(); Restaurant(string initName, int initRating); ... }; // Default constructor Restaurant::Restaurant() { name = "NoName"; rating = -1; } // Another constructor Restaurant::Restaurant(string initName, int initRating) { name = initName; rating = initRating; } int main() { Restaurant foodPlace; // Calls default constructor Restaurant coffeePlace("Joes", 5); // Calls another constructor ...
9. Constructor with Default Parameter Values
-
Like any function, a constructor's parameters may be assigned default values.
A constructor with default parameter values can serve as the default constructor.
class Restaurant { public: Restaurant(string initName = "NoName", int initRating = -1); ... }; Restaurant::Restaurant(string initName, int initRating) { name = initName; rating = initRating; } int main() { Restaurant foodPlace; // name "NoName", rating -1 Restaurant coffeePlace("Joes", 5); // name "Joes", rating 5 Restaurant fast("McD"); // name "McD", rating -1
10. Constructor Initializer Lists
-
An alternative approach for initializing data members in a constructor:
colon followed by comma-separated list of variableName(initValue) items
11. this
12. Operator Overloading
-
Fix... result should be: H:6, M:12
13. Vector Example
-
z2.11-vectors.cc
- compare with z1.5.2-arrays.cc
and z1.5.2-arrays-new.cc
- try access out of bounds
#include <iostream> #include <vector> using namespace std; int main() { const int NUM_VALS = 8; // Number of elements in vector vector<int> userVals(NUM_VALS); // User values unsigned int i; // Loop index cout << "Enter " << NUM_VALS << " integer values..." << endl; for (i = 0; i < userVals.size(); ++i) { // cout << "Value: "; cin >> userVals.at(i); } cout << "You entered: "; for (i = 0; i < userVals.size(); ++i) { cout << userVals.at(i) << " "; } cout << endl; return 0; }