
Welcome to CS 1342 Programming Concepts C++ Syllabus Review • Syllabus: https://erikgabrielsen.com/courses/ cs1342spring2020 • Labs start week of 1/27 • CS Help Desk available (will be posting schedule on website) • Make sure you join Slack! Why Computer Science • CS is now the most popular major at Stanford, MIT, Princeton, Duke, UC San Diego, Tufts, and many more • In 2020, there will be more than 1 million open jobs for CS Majors • In 2020, there will be less than 50,000 graduates to fill those positions • CS is currently the highest paid entry level job in America • Avg Starting Salary: $66,000 Week 1 Assignments • Join the course Slack Due Wednesday • Post a short bio in your sections slack channel. See my post for an example. • Upload a profile picture so we know who you are! • Homework 1 - ZyBook Chapter 1 and 2 Due Next Friday • LabQuiz 1 Due Jan. 31st Chapter 1 Introduction to C++ C++ History and Overview • C++ is a statically typed, general-purpose programming language that supports Object Oriented Programming and memory control / manipulation • 4th Most Popular Programming Language (IEEE 2016) • Considered a “Middle Level” Language • C++ interacts directly with hardware of the computer History of C++ • C - Developed in 1978 by Brian Kernighan and Dennis Ritchie at AT&T Bell Labs. • C was named after a language called B (short for BCPL) • C++ - Developed in 1985 by Bjarne Stroustrup • C++ added support for Object Oriented Programming • A Brief History of C++: https://s2.smu.edu/~devans/2341/ A%20Brief%20History%20of%20C.htm Major Versions of C++ • C++98 - Released in 1998 and released the STL (Standard Template Library) • C++11 - Released in 2011. Added improvements to the STL including many BOOST Libraries • C++14 - Released 2014 - Introduction of polymorphic lambdas, digit separators, generalized lambda capture, variable templates, binary integer literals, quoted strings etc. • C++17 - Released 2017 - Introduction of fold expressions, hexadecimal floating point literals, a u8 character literal, selection statements with initializer, inline variables etc. • C++20 - Upcoming Release in 2020. Will include Concepts, Coroutines, and many other features. In this class we will focus mostly on C++98 and C++11 features. Who uses C++ • Operating Systems (Mobile and Computer) • Embedded Systems • Drivers • Browsers • Gaming • Cloud Computing • Timeline of Programming Languages: https:// en.wikipedia.org/wiki/Timelineofprogramming_languages Java vs C++ • Both Statically typed • Portability • Java uses the JVM • C++ compiles directly to byte code • Memory Management • Java - JVM manages memory • C++ - user must manage memory Java vs C++ • In C++, the basic unit of code is the function • In Java, everything is a class C++ Java #include <iostream> public class HelloWorld { using namespace std; public static void main(String[] args) { System.out.println("Hello, World"); int main() { } cout << "Hello World!" << endl; } return 0; } examples: helloWorld.cpp helloWorld.java Introduction to C++ Programming, I/O and Operators Programming Basics • Every C++ program starts in main() • Each statement is followed with a semicolon • main() returns 0 • A non-zero return notifies the operating system that something went wrong Example 1.1 #include <iostream> int main() { // code goes here return 0; } Programming Basics Comments • Single Line // • Block Comments /* comments */ /** * This is a * multiline comment */ Including Header Files In C++, we often times need to include other libraries or files into our program so that we can have access to additional functionality • The #include keyword is used to indicate importing a file into our program (similar to Java import ) • This gives you access to things implemented in the c++ standard library • Ex: strings, cout, cin, etc… #include <iostream> Includes the iostream library int main() { // code goes here return 0; } Basic I/O (cout) cout is part of the iostream c++ standard library. To use cout in a program you must include the header #include<iostream> • cout - characters out - prints characters to console • In Java this was System.out.print( … ); • << - the stream insertion operator • String Literal “” ex: “cat” • A string literal is different than a string in that it does NOT have a memory address. • endl - denotes a new line • cout - an object responsible for printing out to the console example: outputExample.cpp Basic I/O (cin) cin is part of the iostream c++ standard library. To use cin in a program you must include the header #include<iostream> • cin - characters in - reads in characters entered from the keyboard • In Java this was where you used Scanner • >> - the stream extraction operator • Rules to be aware of with cin • When reading into an integer - will ignore leading whitespace and read until the first non-decimal character • When reading into a floating point - will ignore leading whitespace then read until the first non-decimal character (will include decimal place) • When reading into a single character - will ignore leading whitespace then read first character example: inputExample.cpp Example 1.2 #include <iostream> using namespace std; int main() { int age; cout << "What is your age?: "; cin >> age; cout << "You are " << age << " years old" << endl; return 0; } I/O Exercise What will the output look like for each statement? int score = 20; 1. cout << “George’s score is” << score; cout << “Elroy’s score is” << score; 2. cout << “Welcome \n to Dunder Mifflin”; I/O Exercise What will the output look like for each statement? int score = 20; 1. cout << “George’s score is” << score; cout << “Elroy’s score is” << score; George’s score is20Elroy’s score is20 2. cout << “Welcome \n to Dunder Mifflin”; Welcome to Dunder Mifflin I/O Exercise int a; char b; float c; cin >> a >> b >> c; cout << a << endl << b << endl << c; What would be printed if the user entered the following: • 123b13.2 ? • 129.11.129 ? Operators • An operator allows us to perform mathematical or logical computations one or more operands or values Ex: x = 5; // = operator y = x + 2; // = and + operator Assignment Operator Used to assign a value to a variable from right to left. • The assignment operator is denoted by = Ex: int x = 5; • Variables support reassignment to different values • Other variables are not affected by changing the value of another Ex: int x = 5; // x = 5 int y = x; // y = 5 x = 20; cout << “x = ” << x; cout << “y = ” << y; Arithmetic Operators Arithmetic Operators perform mathematical operations on their operands. The 5 arithmetic operators supported in C++ are: + Addition - Subtraction Ex: int x = 1 + 2; // x = 3 * Multiplication / Division int x = 10 % 3; // x = 1 % Modulo Increment and Decrement Ex: int x = 10; // x = 10 x++; ++x; // x = 12 x--; --x; // x = 10 examples: modulo.cpp increment.cpp Compound Operators Compound Operators are equivalent to an assignment with one operation. They modify the current variable with some operation. (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) x += 3; // x = x + 3 y *= 2; // y = y * 2 Relational Operators Relational and Comparison Operators are used to compare two expressions. They will evaluate to true or false. (==, !=, <, >, <=, >=) (1 == 1); // true (1 != 1); // false (6 >= 6); // true (6 > 6); // false Logical Operators Logical Operators are used along with Relational Operators to return a boolean result. (!, &&, ||) !(1 == 1) logical NOT (true && true) logical AND (true || false logical OR Bitwise Operators Bitwise Operators modify variables using bit wise patterns Operator Bitwise Description & Bitwise AND | Bitwise Inclusive OR ^ Bitwise Exclusive OR ~ NOT << Shift Left >> Shift Right examples: binary.cpp Other C++ Operators The full list of operators can be found at http:// www.cplusplus.com/doc/tutorial/operators/ Exercise Write a program that asks a user to enter how many classes they are taking this semester. Then output the total amount of hours they are enrolled in. Assume each class is 3 hours solution: class_hours.cpp.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages32 Page
-
File Size-