C++ Intro CSE 333 Spring 2018 Instructor: Justin Hsia

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
C++ crash course Class 3 designing C++ programs, classes, control structures.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Working with Strings Lecture 2 Hartmut Kaiser
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Introduction to C++. Introducing C++ C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 15 - C++ As A "Better C"
Hank Childs, University of Oregon
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Computing Fundamentals
Session 1 - Introduction
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 3: Expressions and Interactivity.
C++ References, Const, Classes CSE 333 Spring 2018
C++ Standard Template Library CSE 333 Spring 2018
C++ Constructor Insanity CSE 333 Spring 2018
Client-side Networking CSE 333 Spring 2018
C++ Templates CSE 333 Spring 2018
C++ Encapsulation, Heap CSE 333 Spring 2018
1.13 The Key Software Trend: Object Technology
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Working with Strings Lecture 2 Hartmut Kaiser
C++ Inheritance I CSE 333 Spring 2018
C++ STL, Smart Pointers Intro CSE 333 Spring 2018
C++ Smart Pointers CSE 333 Spring 2018
Chapter 3 Input output.
Introduction to C++ Programming
Operator Overloading.
Your first C and C++ programs
References Revisited CSE 333 Spring 2018
References Revisited CSE 333 Summer 2018
C++ Templates CSE 333 Summer 2018
C++ Constructor Insanity CSE 333 Summer 2018
Programs written in C and C++ can run on many different computers
Chapter 15 - C++ As A "Better C"
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
C++ Intro CSE 333 Autumn 2018 Instructor: Hal Perkins
Capitolo 1 – Introduction C++ Programming
C++ Constructor Insanity CSE 333 Autumn 2018
C++ Constructor Insanity CSE 333 Winter 2019
References Revisited CSE 333 Autumn 2018
References Revisited CSE 333 Winter 2019
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ Templates CSE 333 Winter 2019
CS31 Discussion 1D Fall18: week 2
C++ Constructor Insanity CSE 333 Spring 2019
Presentation transcript:

C++ Intro CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon Huang Wei Lin

Administrivia Exercise 7 posted yesterday, due Monday Read a directory and open/copy text files found there Good warm-up for… Homework 2 due in two weeks (4/26) File system crawler, indexer, and search engine Spec and starter files will be pushed out today

Today’s Goals An introduction to C++ Give you a perspective on how to learn C++ Kick the tires and look at some code Advice: Read related section in the C++ Primer It’s hard to learn the “why is it done this way” from reference docs Lectures and examples will introduce the main idea, but aren’t everything you’ll want need to understand

Hello World in C You never had a chance to write this! 4/13/2018 Hello World in C helloworld.c You never had a chance to write this! Compile with gcc: Based on what you know now, describe to your neighbor everything that goes on in the execution of this “simple” program Be detailed! #include <stdio.h> // for printf() #include <stdlib.h> // for EXIT_SUCCESS int main(int argc, char** argv) { printf("Hello, World!\n"); return EXIT_SUCCESS; } gcc -Wall -g -std=c11 -o hello helloworld.c

Hello World in C++ Looks simple enough… helloworld.cc Looks simple enough… Compile with g++ instead of gcc: Let’s walk through the program step-by-step to highlight some differences #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; } g++ -Wall -g -std=c++11 -o helloworld helloworld.cc

Hello World in C++ iostream is part of the C++ standard library helloworld.cc iostream is part of the C++ standard library Note: you don’t include “.h” when you include C++ standard library headers But you do for local headers (e.g. #include "ll.h") iostream declares stream object instances in the “std” namespace e.g. std::cin, std::cout, std::cerr #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

Hello World in C++ cstdlib is the C standard library’s stdlib.h 4/13/2018 Hello World in C++ helloworld.cc cstdlib is the C standard library’s stdlib.h Nearly all C standard library functions are available to you For header foo.h, you should #include <cfoo> We include it here for EXIT_SUCCESS, as usual #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

4/13/2018 Hello World in C++ helloworld.cc std::cout is the “cout” object instance declared by iostream, living within the “std” namespace C++’s name for stdout std:cout is an object of class ostream http://www.cplusplus.com/reference/ostream/ostream/ Used to format and write output to the console The entire standard library is in the namespace std #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

4/13/2018 Hello World in C++ helloworld.cc C++ distinguishes between objects and primitive types These include the familiar ones from C: char, short, int, long, float, double, etc. C++ also defines bool as a primitive type (woo-hoo!) #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

4/13/2018 Hello World in C++ helloworld.cc “<<” is an operator defined by the C++ language Defined in C as well: usually it bit-shifts integers (in C/C++) C++ allows classes to overload operators! Here, the ostream class overloads “<<” i.e. it defines different member functions (methods) that are invoked when an ostream is the left-hand side of the << operator #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

4/13/2018 Hello World in C++ helloworld.cc ostream has many different methods to handle << The functions differ in the type of the right-hand side (RHS) of << e.g. if you do std::cout << "foo"; , then C++ invokes cout’s function to handle << with RHS char* #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; } std::cout << "foo";

4/13/2018 Hello World in C++ helloworld.cc The ostream class’ member functions that handle << return a reference to themselves When std::cout << "Hello, World!"; is evaluated: A member function of the std::cout object is invoked It buffers the string "Hello, World!" for the console And it returns a reference to std::cout #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; } std::cout << "Hello, World!";

4/13/2018 Hello World in C++ helloworld.cc Next, another member function on std::cout is invoked to handle << with RHS std::endl std::endl is a pointer to a “manipulator” function This manipulator function writes newline ('\n') to the ostream it is invoked on and then flushes the ostream’s buffer This enforces that something is printed to the console at this point #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

Wow… You should be surprised and scared at this point 4/13/2018 Wow… helloworld.cc You should be surprised and scared at this point C++ makes it easy to hide a significant amount of complexity It’s powerful, but really dangerous Once you mix everything together (templates, operator overloading, method overloading, generics, multiple inheritance), it gets really hard to know what’s actually happening! #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return EXIT_SUCCESS; }

Let’s Refine It a Bit C++’s standard library has a std::string class 4/13/2018 Let’s Refine It a Bit helloworld2.cc C++’s standard library has a std::string class Include the string header to use it Seems to be automatically included in iostream on CSE Linux environment (C++11) http://www.cplusplus.com/reference/string/ #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello, World!"); cout << hello << endl; return EXIT_SUCCESS; }

4/13/2018 Let’s Refine It a Bit helloworld2.cc The “using” keyword introduces a namespace (or part of) into the current region using namespace std; imports all names from std:: using std::cout; imports only std::cout (used as cout) #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello, World!"); cout << hello << endl; return EXIT_SUCCESS; } using namespace std; using std::cout;

Let’s Refine It a Bit Note the benefits of 4/13/2018 Let’s Refine It a Bit helloworld2.cc Note the benefits of Can now refer to std::string by string, std::cout by cout, and std::endl by endl #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello, World!"); cout << hello << endl; return EXIT_SUCCESS; } using namespace std;

4/13/2018 Let’s Refine It a Bit helloworld2.cc Here we are instantiating a std::string object on the Stack Passing the C string "Hello, World!" to its constructor method hello is deallocated (and its destructor invoked) when main returns #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello, World!"); cout << hello << endl; return EXIT_SUCCESS; }

4/13/2018 Let’s Refine It a Bit helloworld2.cc The C++ string library also overloads the << operator Defines a function (not an object method) that is invoked when the LHS is ostream and the RHS is std::string http://www.cplusplus.com/reference/string/string/operator<</ #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello, World!"); cout << hello << endl; return EXIT_SUCCESS; }

String Concatenation The string class overloads the “+” operator 4/13/2018 String Concatenation concat.cc The string class overloads the “+” operator Creates and returns a new string that is the concatenation of the LHS and RHS #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello"); hello = hello + ", World!"; cout << hello << endl; return EXIT_SUCCESS; }

String Assignment The string class overloads the “=” operator 4/13/2018 String Assignment concat.cc The string class overloads the “=” operator Copies the RHS and replaces the string’s contents with it #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello"); hello = hello + ", World!"; cout << hello << endl; return EXIT_SUCCESS; }

String Manipulation This statement is complex! 4/13/2018 String Manipulation concat.cc This statement is complex! First “+” creates a string that is the concatenation of hello’s current contents and ", World!" Then “=” creates a copy of the concatenation to store in hello Without the syntactic sugar: hello.operator=(hello.operator+(", World!")); #include <iostream> #include <cstdlib> #include <string> using namespace std; int main(int argc, char** argv) { string hello("Hello"); hello = hello + ", World!"; cout << hello << endl; return EXIT_SUCCESS; } hello.operator=(hello.operator+(", World!"));

4/13/2018 Stream Manipulators manip.cc iomanip defines a set of stream manipulator functions Pass them to a stream to affect formatting http://www.cplusplus.com/reference/iomanip/ http://www.cplusplus.com/reference/ios/ #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; int main(int argc, char** argv) { cout << "Hi! " << setw(4) << 5 << " " << 5 << endl; cout << hex << 16 << " " << 13 << endl; cout << dec << 16 << " " << 13 << endl; return EXIT_SUCCESS; }

Stream Manipulators setw(x) sets the width of the next field to x 4/13/2018 Stream Manipulators manip.cc setw(x) sets the width of the next field to x Only affects the next thing sent to the output stream (i.e. it is not persistent) #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; int main(int argc, char** argv) { cout << "Hi! " << setw(4) << 5 << " " << 5 << endl; cout << hex << 16 << " " << 13 << endl; cout << dec << 16 << " " << 13 << endl; return EXIT_SUCCESS; }

4/13/2018 Stream Manipulators manip.cc hex, dec, and oct set the numerical base for integers output on the stream Stays in effect until you set the stream to another base (i.e. it is persistent) #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; int main(int argc, char** argv) { cout << "Hi! " << setw(4) << 5 << " " << 5 << endl; cout << hex << 16 << " " << 13 << endl; cout << dec << 16 << " " << 13 << endl; return EXIT_SUCCESS; }

C and C++ C is (roughly) a subset of C++ helloworld3.cc C is (roughly) a subset of C++ You can still use printf – but bad style now! Can mix C and C++ idioms if needed to work with existing code, but avoid mixing if you can Use C++(11) #include <cstdio> #include <cstdlib> int main(int argc, char** argv) { printf("Hello from C!\n"); return EXIT_SUCCESS; }

Reading std::cin is an object instance of class istream 4/13/2018 Reading echonum.cc std::cin is an object instance of class istream Supports the >> operator for “extraction” Can be used in conditionals – (std::cin>>num) is true if successful Has a getline() method and methods to detect and clear errors #include <iostream> #include <cstdlib> using namespace std; int main(int argc, char** argv) { int num; cout << "Type a number: "; cin >> num; cout << "You typed: " << num << endl; return EXIT_SUCCESS; }

Peer Instruction Question How many different versions of << are called? For now, ignore manipulator functions Vote at http://PollEv.com/justinh Also, what is output? A. 1 B. 2 C. 3 D. 4 E. We’re lost… msg.cc #include <iostream> #include <cstdlib> #include <string> #include <iomanip> using namespace std; int main(int argc, char** argv) { int n = 172; string str("m"); str += "y"; cout << str << hex << setw(2) << 15U << n << "e!" << endl; return EXIT_SUCCESS; }

Extra Exercise #1 Write a C++ program that uses stream to: Prompt the user to type 5 floats Prints them out in opposite order with 4 digits of precision