Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Similar documents
Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Pre- Defined Func-ons in C++ Review for Midterm #1

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Welcome to Solving Problems with Computers I

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

File I/O CS 16: Solving Problems with Computers I Lecture #9

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Ch 4. Parameters and Function Overloading

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Dynamic Arrays Makefiles and Mul3ple File Compiles

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS 106B, Lecture 1 Introduction to C++

Lab # 02. Basic Elements of C++ _ Part1

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

Fundamentals of Programming CS-110. Lecture 2

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

CSCI 1061U Programming Workshop 2. C++ Basics

Introduction to Programming EC-105. Lecture 2

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

Spring CS Homework 12 p. 1. CS Homework 12

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

CS2141 Software Development using C/C++ C++ Basics

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

Getting started with C++ (Part 2)

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

CIS 130 Exam #2 Review Suggestions

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Midterms Save the Dates!

Computer Science II Lecture 1 Introduction and Background

VARIABLES & ASSIGNMENTS

C++ For Science and Engineering Lecture 12

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

C++ Lab 02 - Command Line Arguments and Strings

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

PIC 10A. Review for Midterm I

Welcome to Computer Organization and Design Logic

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

Dynamic arrays / C Strings

Week 1: Hello World! Muhao Chen

CE221 Programming in C++ Part 1 Introduction

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

BITG 1113: Array (Part 2) LECTURE 9

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

CS Exam 2 Study Suggestions

The following program computes a Calculus value, the "trapezoidal approximation of

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 7. Log into Linux New documents posted to course webpage

Definition Matching (10 Points)

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Programmer-Defined Functions

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

Unit 14. Passing Arrays & C++ Strings

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

Fundamentals of Structured Programming

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Transcription:

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB

A reminder about Labs Announcements Please make sure you READ the lab description BEFORE going to lab Please make sure you understand the STYLING and REQUIREMENT parts of the lab Please make sure to SIGN IN (or you will be counted as absent) Your 1 st Midterm Exam is on THURSDAY (10/19)!!! You didn t forget, did you?! 10/17/2017 Matni, CS16, Fa17 2

Overloading Functions Command-line Arguments Lecture Outline Midterm Review 10/17/2017 Matni, CS16, Fa17 3

Material: Everything we ve done, incl. up to Tue. 10/17 Homework, Labs, Lectures, Textbook Thursday, 10/19 in this classroom Starts at 2:00pm **SHARP** (come early) Ends at 3:15pm **SHARP** BRING YOUR STUDENT IDs WITH YOU!!! Closed book: no calculators, no phones, no computers Only 1 sheet (single-sided) of written notes Must be no bigger than 8.5 x 11 You have to turn it in with the exam You will write your answers on the exam sheet itself. 10/17/2017 Matni, CS16, Fa17 4

What s on the Midterm#1? From the Lectures, including Intro to Computers, Programming, and C++ Variables and Assignments Boolean Expressions (comparison of variables) Input and Output on Standard Devices (cout, cin) Data Types, Escape Sequences, Formatting Decimal Arithmetic Operations and their Priorities Boolean Logic Operators Flow of Control & Conditional Statements Loops: for, while, do-while Types of Errors in Programming Multiway Branching and the switch command Generating Random Numbers Functions in C++: pre-defined, user-defined void functions, the main() function call-by-ref vs. call-by-value, overloading Command Line Inputs to C++ Programs No numerical conversions for Midterm 1!!! 10/17/2017 Matni, CS16, Sp17 5

10/17/2017 Matni, CS16, Fa17 6

A Note on Programming Style When naming variables, functions, etc Snake Case: Using underscore character ( _ ) Example: mortgage_amount function_fun() Associated with C, C++ programmers Styling Requirements for Labs See announcement on Piazza Not on Piazza yet? MUST TELL ME ASAP!!! Camel Case: Using upper-case letters to separate words Example: MortgageAmount FunctionFun() Associated with Java programmers For this class, YOU CAN USE EITHER! 10/17/2017 Matni, CS16, Fa17 7

Overloading Function Names C++ allows more than one definition for the same function name Called overloading Very convenient for situations in which the same function is needed for different numbers or different types of arguments Overloading a function name: providing more than one declaration and definition using the same function name 10/17/2017 Matni, CS16, Fa17 8

Overloading Examples double average(double n1, double n2) { return ((n1 + n2) / 2); } BIG Pro-Tip: Do not use the same function name for unrelated functions double average(double n1, double n2, double n3) { return (( n1 + n2 + n3) / 3); } Compiler checks the number and types of arguments in the function call and then decides which function to use automatically! So, with a statement like: cout << average( 10, 20, 30); the compiler knows to use the second definition 10/17/2017 Matni, CS16, Fa17 9

Overloading Rules in C++ Overloaded functions Must have different numbers of formal parameters, but must all be the same type OR e.g.: double average(int a, int b) vs. double average(int a, int b, int c) They can have the same number of parameters, but must have at least one of them be of a different type e.g.: void print(int a) vs. void print(double a) vs. void print(char a) You can not overload function declarations that differ only by return type. 10/17/2017 Matni, CS16, Fa17 10

Example from Textbook, Ch. 4 10/17/2017 Matni, CS16, Fa17 11

Automatic Type Conversion C++ will automatically converts types between int and double in multiple examples E.g. If I divide integers, I get integers: 13/2 = 6 E.g. If I make one of these a double, I get a double: 13/2.0 = 6.5 It does the same with overloaded functions, for example, given the definition: double mpg(double miles, double gallons) { return (miles / gallons); } what will happen if mpg is called in this way? cout << mpg(45, 2) << miles per gallon ; The values of the arguments will automatically be converted to type double (45.0 and 2.0): The answer will be: 22.5 miles per gallon 10/17/2017 Matni, CS16, Fa17 12

Command Line Arguments with C++ In C++ you can accept command line arguments That is, when you execute your code, you can pass input values at the same time These are arguments that are passed into the program from the OS command line To use command line arguments in your program, you must add 2 special arguments in the main( ) function Argument #1 is the number of elements that you are passing in: argc (reserved name) Argument #2 is a full list of all of the command line arguments: *argv[ ] (reserved name, too) This is an array pointer more on those in a later class 10/17/2017 Matni, CS16, Fa17 13

Command Line Arguments with C++ The main() function should be written as: int main(int argc, char* argv[]) { } In the OS, to execute the program, the command line form should be: $ program_name argument1 argument2 argumentn example: $ sum_of_squares 4 5 6 10/17/2017 Matni, CS16, Fa17 14

DEMO: int main ( int argc, char *argv[] ) { cout << "There are " << argc << " arguments here:" << endl; for (int i = 0; i < argc; i++) cout << "argv[" << i << "] is : " << argv[i] << endl; } return 0; 10/17/2017 Matni, CS16, Fa17 15

argv[n] Is Always a Character Type! While argc is always an int (it s calculated by the compiler) all you get from the command-line is character arrays This is a hold-out from the early days of C So, the data type of argument being passed is always an array of characters (a.k.a. a C-string) To treat an argument as another type (like a number, for instance), you have to first convert it inside your program <cstdlib> library has pre-defined functions to help! 10/17/2017 Matni, CS16, Fa17 16

What If I Want an Argument That s a Number? <cstdlib> library has pre-defined functions to help! Examples: atoi( ), atol( ), and atof( ) Convert a character array into int, long, and double, respectively. Example: #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) { for(int i = 1; i < argc; i++) cout << atoi(argv[i]) << endl; return 0; } 10/17/2017 Matni, CS16, Fa17 17

Midterm Review 10/17/2017 Matni, CS16, Fa17 18

What s on the Midterm#1? From the Lectures, including Intro to Computers, Programming, and C++ Variables and Assignments Boolean Expressions (comparison of variables) Input and Output on Standard Devices (cout, cin) Data Types, Escape Sequences, Formatting Decimal Arithmetic Operations and their Priorities Boolean Logic Operators Flow of Control & Conditional Statements Loops: for, while, do-while Types of Errors in Programming Multiway Branching and the switch command Generating Random Numbers Functions in C++: pre-defined, user-defined void functions, the main() function call-by-ref vs. call-by-value, overloading Command Line Inputs to C++ Programs No numerical conversions for Midterm 1!!! 10/17/2017 Matni, CS16, Sp17 19

Midterm Prep 1. Lecture slides 2. Homework problems 3. Lab programs 4. Book chapters 1 thru 5* *check the lecture slides (from lectures 1 thru 6) with it!! 10/17/2017 Matni, CS16, Fa17 20

Sample Question Multiple Choice Complete the following C++ code that is supposed to print the numbers 2, 3, 4, 5, 6: int c = 0; while ( ) { cout << c+2 << ; c++; } A. c < 7 B. c > 5 C. (c + 2) < 6 D. (c + 2)!= 6 E. c < 5 10/17/2017 Matni, CS16, Sp17 21

Sample Question Multiple Choice What is the exact output of this C++ code? int prod(1); for (int m = 1; m < 7; m+=2) prod *= m; cout << Total product is: << prod << endl; A. Total product is: 720 B. Total product is: 105 C. Total product is: 48 D. Total product is: 15 E. Total product is: 1 10/17/2017 Matni, CS16, Sp17 22

Sample Question Multiple Choice Assuming precision is set to 1, the command cout << static_cast<double>(5/2) returns to the display: A. 5.0 B. 5.2 C. 2.0 D. 2 ½ E. 2.5 10/17/2017 Matni, CS16, Sp17 23

Sample Question Multiple Choice If a command line is used as follows ( $ is the command prompt): $ myprogram 6 0 JokersWild Then what is the value of argv[0]? A. 6 B. 0 C. 4 D. myprogram E. JokersWild 10/17/2017 Matni, CS16, Sp17 24

Sample Question Short-Answer Coding Write C++ code showing a function definition of distance( ) which takes 4 int values x 1, x 0, y 1, and y 0 and returns a double data type that s equal to (x 1 -x 0 ) 2 + (y 1 -y 0 ) 2. Assume that the cmath lib has been imported. double distance(int x1, int x0, int y1, int y0) { } double a = pow(x1 x0, 2); double b = pow(y1 y0, 2); double z = sqrt(a + b); return z; Note: When I ask for code, that means not a complete program. Otherwise I d ask for a program. Also, this would be clear from the question. 10/17/2017 Matni, CS16, Sp17 25

Sample Question Find at Least 10 Mistakes That is, just look for syntax and logic errors (ignore styling conventions) 2: Should be: <string> 3: Should be: using namespace std; 6: Should be: int number, x = 0; 9: Should be: \n 10: Missing ; at the end 11: Should be: cin >> word; 14: Must remove the ; at the end 16: Should be: cout << word << ; 17: Should be: x++ 19: Should be: cout << endl; return 0; 10/17/2017 Matni, CS16, Sp17 26

STUDY FOR YOUR MIDTERM!! Turn in HW3 on Thursday YOUR TO-DOs Lab 3 is NOT DUE UNTIL MONDAY 10/23!!!! Lab 4 follows the usual schedule. Starts Mon. 10/23; Due Fri. 10/27 HW4 will be released on Thursday, will be due in 1 week. Visit Prof s and TAs office hours if you need help! Good Luck!!!! 10/17/2017 Matni, CS16, Fa17 27

10/17/2017 Matni, CS16, Fa17 28