C++ Classes Exercises

  Next >
  1. Write the complete implementation (.h & .cpp files) for a class of your choice.
    Share your implementation with a neighbor (or two).
  2. Think of a design problem.
    What would a Procedural Programming implementation look like?
    What would a Object-Oriented Programming implementation look like?
    Share your example with a neighbor (or two).
  3. Does the following code exemplify Procedural or Object-Oriented Programming?
    // From ADTs, Data Structures, and Problem Solving with C++, 2e by Larry Nyhoff
    #include <iostream>
    #include "Time.h"
    using namespace std;
    
    struct Time{
        unsigned int hour, minute;
        char AMorPM;        // 'A' or 'P'
        unsigned int milTime;   // military time equivalent
    };
    
    int main(){
        Time mealTime, goToWorkTime;
        set(mealTime, 5, 30, 'P');
        cout << "We'll be eating at ";
        display(mealTime, cout);
        cout << endl;
        set(goToWorkTime, 5, 30, 'P'); 
        cout << "You leave for work at ";
        display(goToWorkTime, cout);
        cout << endl;
        if (lessThan(mealTime, goToWorkTime)){
            cout << "If you hurry, you can eat first.\n";
        }else{
            cout << "Sorry you can't eat with us.\n";
        }
        advance(goToWorkTime, 0, 30);
        cout << "Your boss called.  You go in later at ";
        display(goToWorkTime, cout);
        cout << endl;
        if (lessThan(mealTime, goToWorkTime)){
            cout << "If you hurry, you can eat first.\n";
        }else{
            cout << "Sorry you can't eat with us.\n";
        }
        cout << endl;
    }
    

Last Modified: