Schedule‎ > ‎

Lesson 16 Overloading Functions, Final Exam, Project Presentations

  • C++ lets us have more than one function with the same name
  • Having two or more function definitions with the same name is known as function overloading
  • Overloaded functions must have different parameter types between them
  • As an example, consider our previous example function
    int myMultiplyFunction(int x, int y) {
      int result;
      result = x * y;
      return result;
    }
    
  • The above function only uses integer multiplication
  • We could add a second function to handle floating-point multiplication, like:
    double myMultiplyFunction(double x, double y) {
      int result;
      result = x * y;
      return result;
    }
    
  • Now we will get different results when multiplying numbers with decimal points
    3.50 * 4.50 = 15.00
    3 * 4 = 12
    

Overloading Resolution

  • Let us look at how function calls are matched with overloaded functions
  • The process the compiler follow is:
    1. Look for an exact signature match
    2. Look for a "compatible" signature
  • If an exact match is not found, C++ will try to convert the data type as follows:
    1. bytecharunsigned char, and short are promoted to int
    2. unsigned short can be promoted to int if it can hold its entire value range or unsigned int otherwise
  • Some conversions may cause a loss of precision, so care must be taken
  • Also, C++ only knows how to convert certain types, mostly the numeric types
  • Thus, C++ does not automatically convert something like a String to a double

Type Conversion

  • C++ can convert between numerical types
  • We can manually specify a type conversion by using a typecast
  • To typecast, we place the desire type in parenthesis in front of the variable, like:
    double c = myMultiplyFunction((int) a, (int) b);
    
  • The above calls the myMultiplyFunction(int x, int y) function even though a and b are of type double
  • Another way to convert types is to use the conversion functions listed on the Arduino Language Reference page
  • Also, to convert a String to a numeric type, use one of the String object functions
  • In the above Function Overloading code,  uncomment the last 2 code lines in the loop() function to see what happens when you call myMultiplyFunction with an int and float parameters.

Avoiding Function Overloading

  • Usually we make numeric parameters type double
  • Type double will accept any numeric type
  • However, we will come across cases where we need to overload functions
Check Yourself
  1. Of the following function signatures, the two that are not allowed in the same scope are ________.
    1. int add(int a, int b)
    2. double add(int a, double b)
    3. double add(double a, double b
    4. double add(int x, int y)
  2. The reason the two function signature of the above are not allowed in the same scope is that ________.
    1. function names are the same
    2. parameter types are different
    3. parameter types are the same
    4. return types are different
  3. For the following function call, the function definition that is invoked is ________.
    fun(98, 99);
    
    1. void fun(int n, double m)
    2. void fun(double n, int m)
    3. void fun(int n, int m)
    4. void fun(double n, double m)


15.1: Final Exam Preparation

Learner Outcomes

At the end of the lesson the student will be able to:

  • Discuss how to prepare for the final exam
  • Describe how to take the final exam


15.1.1: About the Final Exam

Important Final Exam Information

Date and Time: Exam Schedule 
Day Class: Thursday, May 24 @ 1:00pm-3:50pm 
Location: regular classroom

  • You must attend the exam at the scheduled time or you will receive a score of zero (0)
    • Except by prior arrangement with the instructor for unforeseeable emergency and justifiable reasons
  • Bring a valid ID to the exam
  • Be on time as you can only work on the exam during the scheduled time


15.1.2: How the Final Exam Works

  • The final exam is a Lab Practical
  • This means that you must write code for the exam
  • You will be given a series of programming problems to solve
  • Successfully completing each problem is worth some stated number of points

Administration and Ground Rules

  • I am using Canvas to administer the test
  • The exam is closed books and closed notes
  • However, you may have one 3" x 5" card of notes for the exam
  • Also, you may have blank scratch paper
  • You must use a classroom computer for taking the exam and accessing Canvas
  • You may use both Cygwin and TextPad or NotePad++ to compile and run your code
    • However, your code must compile to receive more than half-credit on the entire exam
    • Partial credit is available if you comment out your problem code
  • You may NOT use the computer to search the Internet
  • You may NOT use any electronic device during the exam except the computer in the classroom
    • Thus, you cannot use your own computer to take the exam
  • You may NOT communicate with anyone but the instructor during the exam

3"x5" Card Requirements

  • Put your name on your card
  • Maximum card or paper size is 3 inches by 5 inches
  • You may use both sides of the card
  • Notes must be handwritten and NOT photocopied
  • No more than three statements in a code sequence on the card — only snippets
  • Any 3" x 5" cards violating these rules will be confiscated before the test
  • You must turn in your 3" x 5" card after the exam


15.1.3: What the Final Exam Covers

  • The final exam is cumulative -- you should know everything we have covered
  • However, the focus is writing code for what we have learned
  • Prior exam topics are listed here:
  • See below for newer exam topics

Code You Should be Capable of Writing

  1. Declaring class types (9.2.210.1.1)
  2. Declaring and defining member and non-member functions (9.2.412.3.4)
  3. Coding constructors with and without parameters (9.2.59.3.19.3.2)
  4. Constructing objects and calling their functions (9.2.39.3.3)
  5. Modifying (changing) values in an object (9.3.4)
  6. Writing set and get functions of classes (9.3.4)
  7. Defining and initializing vectors (10.2.2)
  8. Accessing vector elements (10.2.3)
  9. Changing the size of a vector (10.2.4)
  10. Coding vector parameters and return values (10.2.5)
  11. Processing vectors using loops, including vectors of objects (10.2.410.4.2)
  12. Coding common vector algorithms such as searching for, inserting and deleting elements (10.3.210.3.310.3.4)
  13. Opening a file stream for reading or writing (11.2.5)
  14. Reading different types of data from a file (11.2.512.1.312.1.4)
  15. Loading data from a file and saving it in variables, vectors and objects (11.2.512.1.2-512.3.5)
  16. Writing data from a vector and saving it in a file (12.1.612.3.6)
  17. Developing recursive functions (14.2Exercise 14.2)


15.1.4: Study Recommendations

  • Study over several sessions instead of one cram session
  • Review your homework assignments and solutions
  • Review the instructor's posted solutions to assignments:
    • Solutions are posted in Canvas
    • Understand how the instructor solved each problem
  • Work through the Practice Final questions in Canvas:
    • Work the problems in groups if it helps you
    • Get explanations for anything you do not understand

    Tip: Complete the entire practice exam.

Final Preparation Tips

  • Make notes on problems on the Practice Final that you had difficulty with
  • Make sure you know how to solve those types of problems
  • Review your notes and prepare your 3" x 5" card
  • Do a quick review just before bed to let your subconscious aid in long term memory.
  • Get plenty of rest before the exam


15.1.5: Exam Taking Tips

  • Arrive at the examination room a little ahead of time.
  • Listen carefully to any oral instructions for taking the exam and read instructions carefully.
  • Read every word in each test question
  • Note that you do not need to comment code for the final exam
    • Unless specifically instructed to in the exam question
  • Use the full time allowed


15.1.6: Review Materials, Questions and Answers


15.2: Sampler Demonstration

Learner Outcomes

At the end of the lesson the student will be able to:

  • Present their sampler assignment


15.2.1: Sampler Presentation

  • Here are the steps for an effective project presentation

Before the Presentation

  • Submit the following to Canvas before the presentation:
    1. README.txt file with project report
    2. All source code (i.e. .cpp files)
    3. Any other files needed to make your program function.
  • Remember that there are no late submittals accepted for the project
  • If you have problems completing the project, then turn in what you have on time and come to class
  • Bring a paper copy of your written report and give it to the instructor at the start of class
    • If you are using your paper for your presentation, be sure to bring an extra copy!
  • Also, be sure you have a quick way to access your project during class to minimize setup time
    • Flash drive (recommended in case of network problems)
    • Canvas
    • Online service

Project Setup

  • The best time to setup is before class
  • Copy your files to the instructor machine in your own folder on the Desktop
  • Verify your code compiles and your project loads its files
  • If you get stuck or do not know what to do, ask for help

During the Presentation

Present the following information:

  • Introduce yourself and state the purpose of your project
  • Compile your program completely and be sure to include all the warnings:
    g++ -W -Wall -pedantic -std=c++11 -o programName sourceFile.cpp
    
    • I recommend compiling with TextPad because it includes all the warnings
    • Compiling with CodeBlocks will not provide full credit unless you have a graphics project
  • Demonstrate your project, explaining aspects as you go
    • Include an explanation of all extra-credit features
    • Explain how the extra credit and interesting features work
    • Show the code for recursive functions and to help explain features
  • Limit the presentation to 5 minutes or less

After the Presentation

  • Feel free to leave (or stay) after your presentation
  • You can present to the instructor alone after the other presentations are through
  • Remember to study for the final!
Comments