COMP 2012 Object-Oriented Programming and Data Structures

Lab 3 Namespace

Introduction

In this lab, we are going to design an event query system for visitors to check events held by different Schools and Departments for the HKUST 30th Anniversary! To do that, we will use C++ Namespaces. We will define a namespace for each School or Department, which contains some necessary variables and functions. We will implement a function to let the users select interested School or Department, and then call the correct function to list available events according to the user inputs.

We will finish this task based on a skeleton code provided in source files. Before getting started, please read the Namespace Lecture Notes.

Overview

In this lab, we have six namespaces:

  • Namespace engineering (Denote the School of Engineering).
    • Nested namespace cse (Denote the Department of CSE).
    • Nested namespace ece (Denote the Department of ECE).
  • Namespace science (Denote the School of Science).
    • Nested namespace math (Denote the School of Mathematics).
    • Nested namespace physics (Denote the School of Physics).
Each of the above namespaces (a school or department) will contain some events, defined as variables in the namespace, and a function to print out these events. Therefore, when a user specifies a school of department, the system will be able to print out the corresponding events. The following code shows the definitions of these namespaces:

  1. The engineering namespace and nested namespaces cse, ece (in school-namespaces.h):
    namespace engineering
    {
        void show_events();
        namespace cse {
            extern std::string events[];
            void show_events();
        }
        namespace ece {
            extern std::string events[];
            void show_events();
        }
    }
    
    Note: the extern keyword used in the above code means we only declare the events variable in this head file, and will define it in other places. For more usages, please refer to extern.
  2. The science namespace and nested namespaces math, physics (in school-namespaces.h):
    namespace science
    {
        void show_events();
        extern std::string events[];
        namespace math {
            extern std::string events[];
            void show_events();
        }
        namespace physics {
            extern std::string events[];
            void show_events();
        }
    }
    
Note that the content of events variables and the body of show_events functions are defined in in school-namespaces.cpp. For example, the events of the Department of CSE and the Department of ECE are respectively defined as follows:
  • The events of the Department of CSE (in school-namespaces.cpp):
    string events[] {
        "9:00 Making Machine Learning Automated and Trustworthy.",
        "11:00 The Next Frontier in Type Inference.",
        "14:00 AI-Human Teaming for Decision Making."
    }; 
  • The events of the Department of ECE (in school-namespaces.cpp):
    string events[] {
        "15:00 2D Materials, from Academia to Industry.",
        "16:00 Bridging Vision and Language for Cross-Modal Understanding and Generation."
    }; 
However, the variable and function names are shared across namespaces (the compiler/linker will complain about multiple definitions of events in the above code). To avoid ambiguities, you will need to fix the above code by specifying the namespace for each variable or function definitions.
Finally, we will implement a query function in events-query.cpp, which asks the users to select the interested Department or School, and call the correct show_events function as defined in school-namespaces.cpp. We have provided most parts of the code, you need to fill in the gaps to speficy namespaces and call the corresponding printing functions:
  • Definition of the query function (in events-query.cpp):
    #include 
    #include "school-namespaces.h"
    
    // Note: You are supposed to insert new lines in this file, but not modify/delete existing lines!
    // TODO 1: Specify namespaces to be used in this file.
    
    
    void QueryEvents() {
        cout << "Select the school: 1. School of Engineering; 2. School of Science:";
        char choice;
        while(cin >> choice) {
            if (choice == '1' || choice == '2')
                break;
            else
                cout << "Select from 1 or 2." << endl;
        }
    
        if(choice == '1') {
            cout << "Select the department: 1. Department of CSE; 2. Department of ECE; 3. All Departments:";
            // TODO 2: Call the corresponding show_events function according to input.
    
        }
        else {
            cout << "Select the department: 1. Department of Mathematics; 2. Department of Physics; 3. All Departments:";
            // TODO 3: Call the corresponding show_events function according to input.
    
        }
    } 

Lab tasks

  1. Finish the TODOs in school-namespaces.cpp.
    • Fix the definitions of events variables and show_events functions to eliminate ambiguities.
    • Complete the function body of show_events for the School of Engineering. Note that in this function, you need to print out events of both the CSE and ECE Departments.
    • Complete the function body of show_events for the School of Science. Note that the School of Science has events independent of departments. In this function, you need to print out events of the School and events of both the Mathematics and Physics Departments.
  2. Finish the TODOs in events-query.cpp (You should only insert new lines to this file, but not modify/delete existing lines!).
    • Specify namespaces to be used in this file.
    • Call the correct show_events function according to user input. When selecting the departments, the option 3. ALL means printing all events of the school.
  3. Run the Makefile by make to build the executable (i.e., main.exe).
  4. Run main.exe, check with the input selections and expected output below (inputs are in red):
    1. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:1
      Select the department: 1. Department of CSE; 2. Department of ECE; 3. All Departments:1
      Welcome to Department of CSE!
      9:00 Making Machine Learning Automated and Trustworthy.
      11:00 The Next Frontier in Type Inference.
      14:00 AI-Human Teaming for Decision Making.
    2. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:1
      Select the department: 1. Department of CSE; 2. Department of ECE; 3. All Departments:2
      Welcome to Department of ECE!
      15:00 2D Materials, from Academia to Industry.
      16:00 Bridging Vision and Language for Cross-Modal Understanding and Generation. 
    3. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:1
      Select the department: 1. Department of CSE; 2. Department of ECE; 3. All Departments:3
      Welcome to School of Engineering!
      9:00 Making Machine Learning Automated and Trustworthy.
      11:00 The Next Frontier in Type Inference.
      14:00 AI-Human Teaming for Decision Making.
      15:00 2D Materials, from Academia to Industry.
      16:00 Bridging Vision and Language for Cross-Modal Understanding and Generation.
    4. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:1
      Select the department: 1. Department of CSE; 2. Department of ECE; 3. All Departments:4
      Invalid input.
    5. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:2
      Select the department: 1. Department of Mathematics; 2. Department of Physics; 3. All Departments:1
      Welcome to Department of Mathematics!
      16:00 Rate of blow up in the thin obstacle problem.
    6. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:2
      Select the department: 1. Department of Mathematics; 2. Department of Physics; 3. All Departments:2
      Welcome to Department of Physics!
      11:00 Interfacial Dynamics between a Hydrogel and a Fluid: Modeling and Simulation.
      14:00 Application of Flow Model to Theoretical Physics.
    7. Welcome to 30th Anniversary of HKUST! Check Events by Department:
      Select the school: 1. School of Engineering; 2. School of Science:2
      Select the department: 1. Department of Mathematics; 2. Department of Physics; 3. All Departments:3
      Welcome to School of Science!
      10:00 A variational approach to describe the moduli space of minimal immersions in hyperbolic.
      16:00 Rate of blow up in the thin obstacle problem.
      11:00 Interfacial Dynamics between a Hydrogel and a Fluid: Modeling and Simulation.
      14:00 Application of Flow Model to Theoretical Physics.

Submission Deadline and Guidelines:

The due date and time will be 10-min after your lab session. You are supposed to upload the following files in an archive to ZINC:

- school-namespaces.cpp
- events-query.cpp
You can submit your codes multiple times by the deadline. Only the last submission will be graded.
Page maintained by
  • Mingyang Zhang
  • Last Modified:
Homepage