C++ zyBooks Chapter 2 - Objects and Classes

ECE 2620, Fall 2019


Table of Contents


1. Abstract Data Type

Object Oriented Programming: encapsulation, polymorphism, inheritance


2. Class Example - string

z2.2-string.cc

#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

z2.3-Restaurant.cc

output:

My favorite restaurants:
Central Deli -- 4
Friends Cafe -- 5

4. Inline Member Functions

wikipedia:

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.

cppreference:

A function defined inside a class is implicitly an inline function.

The inline keyword explicitly declares a function to be an inline function.

z2.4-Restaurant-inline.cc


5. Set and Get

z2.5-Restaurant.cc

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

z2.10-TimeHrMn.cc

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;
}