Skip to content

Latest commit

 

History

History
88 lines (48 loc) · 5.09 KB

File metadata and controls

88 lines (48 loc) · 5.09 KB

Introduction to C++ & OOP

Video (5:57): C++ input & output - cout, cin, & getline

This file has code examples of using stream input & output in C++.

Video (9:24): Introduction to C++ syntax

This file has examples of variable declarations, initializations, auto, constants, a vector, and pointers in C++.

Video (11:42): C++ string examples

C++ includes a string type. Several examples of different string functions.

Video (10:00) Example of using bool type and ternary operator

Video (11:12) Examples of C++ references

Demonstration of how to work with references, including a function that returns a reference.

Video (4:32): Default arguments

This file contains an example of using default arguments in C++ functions.

Video (5:28): Example of const member methods

OOP Examples

The next few videos feature examples of inheritance, virtual/non-virtual functions, and polymorphism. The are included in increasing order of complexity, but if you are new to OOP you may find the first animals.cpp to be easier to understand at first.

VIDEO - Overview of Inheritance, Polymorphism, & Virtual Functions in C++ (9:02)

Video (6:18): Overview of deriving a class in C++

Video (9:31): Virtual & pure virtual methods

This file contains a simplified example of how non-virtual, virtual, & pure-virtual methods work. You may want to review the animals.cpp videos first if you want to get a review of inheritance first or a more specific explanation of how each of the types of functions work.

UPDATE: Line 29 has changed from the video. The string was changed to "SUB: nonVirtual2" to reflect that this was part of the derived class.

Video 1 (7:34): Inheritance with C++, pointers & references

Video 2 (13:07): Polymorphism, virtual, non-virtual, & pure virtual methods

This file includes examples of inheritance, polymorphism, and virtual and pure-virtual methods.

  • Video 1 (8:00) - Creating the abstract classes StaffMember & Employee
  • Video 2 (8:26) - Creating the concrete classes Hourly, Salaried, & Volunteer
  • Video 3 (15:29) - Working with collections of objects and virtual vs. non-virtual methods
  • Video 4 (3:22) - Working with collections of objects and virtual vs. non-virtual methods

Examples of inheritance, polymorphism, virtual vs non-virtual methods, C++ header (.hpp) files, and writing a makefile for a larger multi-file program. We use a vector to store a list of pointers to StaffMembers. We will cover vectors in a later module. I left a lot of the compile errors and some design changes in the video so that you can see the thought process behind it.

This program contains the following class hierarchy:

  • StaffMember (staffmember.cpp / staffmember.hpp)
    • Volunteer (volunteer.hpp - no cpp file needed)
    • Employee (employee.cpp / employee.hpp)
      • Hourly (hourly.cpp / hourly.hpp)
      • Salaried (salaried.cpp / salaried.hpp)

The main program is in staff.cpp and the makefile builds the whole program.

In the loop where the pay stubs are printed in staff.cpp, the StaffMember print method is called since print is not virtual. This is as intended - the only thing that should be printed is the name/address/phone of the employee, not additional things such as SSN. To print that information we would need to use a variable or pointer of the derived class type.

NOTE: Instead of writing the bonus method, I could have overloaded the pay method with a virtual method that took a parameter for the size of the bonus. Also, it probably would have been better to use variables instead of pointers for the 4 StaffMembers to avoid memory allocation.