Introduction to C++ Programming: Winter 2024 Course Overview
This content covers the introductory lecture of a C++ course for Winter 2024, including important information, logistics, lab exercises, and code review. Topics include parameters, value semantics, and example functions like trimming whitespace in a string.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CS 132, Winter 2024 Lecture 1: Introductions; C++ review Thank you to Marty Stepp and Stuart Reges for parts of these slides
Important Information Course website: http://allisonobourn.com/edmonds/132 Instructor email: allison.obourn@edmonds.edu 2
Logistics General course setup: Outside of class graded work: small problems projects quizzes midterm and final exams In class graded work: labs Help available: Discord - the official message board Office hours Study centers on campus End the quarter with a bigger creative project: Group final project 3
Lab exercise 1 Go to the lab section of the course website Click the topic survey link Fill out the survey 4
Review: parameters void print_number(int number, int count) { for (int i = 1; i <= count; i++) { cout << number << " "; } cout << endl; } void main() { print_number(4, 9); print_number(17, 6); print_number(8, 0); print_number(0, 8); } Output: 444444444 171717171717 00000000 5
A "Parameter Mystery" problem void mystery(int x, int z, int y) { printf("%d and %d", z, (y - x)); } int main() { int x = 9; int y = 2; int z = 5; mystery(z, y, x); mystery(y, x, z); } 6
Value semantics value semantics: by default, when variables are passed as parameters, their values are copied. Modifying the parameter will not affect the variable passed in. void strange(int x) { x = x + 1; cout << "1. x = << " << x << endl; } int main() { int x = 23; strange(x); cout << "2. x = " << x << endl; ... } Output: 1. x = 24 2. x = 23 7
Example: trim nameDiamond Write a function trim that accepts a string as a parameter and removes all the whitespace at the beginning and the end of it. Leave any whitespace in the middle in the string. For example, after the following code is run string saying = " computer s "; trim(saying); saying should store "computer s". 8
Why doesn't this work? void removeFirst(string s) { s = s.substr(1); } C++ defaults to pass by value unless you specify otherwise Solutions? return a new copy pass in a reference pass in a pointer 9
Reference semantics reference semantics: If you declare a parameter with an & after its type, it will make it a reference and so link the functions to the same place in memory. Modifying a parameter will affect the variable passed in. void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int x = 17; int y = 35; swap(x, y); cout << x << "," << y << endl; // 35,17 return 0; } 10
Ref param mystery parameterMysteryBCA What is the output of this code? void mystery(int& b, int c, int& a) { a++; b--; c += a; // A. 5 2 8 } // B. 5 3 7 // C. 6 1 8 int main() { // D. 6 1 13 int a = 5; // E. other int b = 2; int c = 8; mystery(c, a, b); cout << a << " " << b << " " << c << endl; return 0; } 11
Output parameters xkcdDatingRange What is the minimum and maximum non-creepy age to date? void datingRange(int age, int& min, int& max) { min = age / 2 + 7; max = (age - 7) * 2; } int main() { int young; int old; datingRange(48, young, old); count << "A 48-year-old could date someone from " << young << " to " << old " years old." << endl; } http://xkcd.com/314/ // A 48-year-old could date someone from // 31 to 82 years old. 12
Example: quadratic quadratic Write a function quadratic to find roots of quadratic equations. ax2 + bx + c = 0, for some numbers a, b, and c. 2 Find roots using the quadratic formula. Example: x2 - 3x - 4 = 0 roots: x = 4 , x = -1 4 b b 2 ac a What parameters should our function accept? What should it return? Which parameters should be passed by value, and which by reference? 13
Quadratic solution /* * Solves a quadratic equation ax^2 + bx + c = 0, * storing the results in output parameters root1 and root2. * Assumes that the given equation has two real roots. */ void quadratic(double a, double b, double c, double& root1, double& root2) { double d = sqrt(b * b - 4 * a * c); root1 = (-b + d) / (2 * a); root2 = (-b - d) / (2 * a); } 2 4 b b 2 ac a 14
Reference pros/cons benefits of reference parameters: a useful way to be able to 'return' more than one value often used with objects, to avoid making bulky copies when passing downsides of reference parameters: hard to tell from call whether it is ref; can't tell if it will be changed foo(a, b, c); // will foo change a, b, or c? :-/ slightly slower than value parameters for small types can't pass a literal value to a ref parameter; must "refer" to a variable grow(39); // error 15
Showing a caller their data is safe: const What if we want to pass a huge array to a method? Should we pass it by value or reference? We almost always want to use references because copying lots of data is really time and space consuming What if we want to see if an array contains a value, print it or do some other operation that doesn t change it? The user may need a guaranteetheir data won t be changed. Solution: make the array parameter const int countInRange(const int& list[], int min, int max) { const parameters can't be changed even if they are references or pointers It is considered best practice to make any reference or pointer you pass to a function const if you are not changing its value 16