Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.

Slides:



Advertisements
Similar presentations
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
Advertisements

Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Program Input and the Software Design Process ROBERT REAVES.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
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++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Chapter 05 (Part II) Control Statements: Part II.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
Week 15 - Friday.  What did we talk about last time?  Review up to Exam 2.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
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.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 15 - C++ As A "Better C"
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
C++ Templates.
Introduction to C++ Systems Programming.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Arrays Arrays exist in almost every computer language.
Week 14 - Monday CS222.
Output Stream Formatting
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
week 1 - Introduction Goals
(5 - 1) Object-Oriented Programming (OOP) and C++
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Standard Input/Output Streams
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
CS 2308 Exam I Review.
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.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
CS2011 Introduction to Programming I Methods (II)
(5 - 1) Object-Oriented Programming (OOP) and C++
Chapter 15 - C++ As A "Better C"
Fundamental Programming
Arrays Arrays A few types Structures of related data items
C++ Programming Basics
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Week 13 - Friday

 What did we talk about last time?  Server communications on a socket  Function pointers

There are only two kinds of programming languages: those people always bitch about and those nobody uses. Bjarne Stroustrup Inventor of C++ There are only two kinds of programming languages: those people always bitch about and those nobody uses. Bjarne Stroustrup Inventor of C++

 C++ is based on C and easier to use  You can declare variables anywhere ▪ Not the case in the C89 standard (where all variables had to be declared right after a block starts), but our gcc is following the C99 standard  It has function overloading  Most people think the I/O is cleaner  The big addition is OOP through classes  It's an approximate superset of C that includes most C structures

 gcc used to stand for the GNU C Compiler  When it became a suite of tools used for things other than C, they changed the name to the GNU Compiler Collection  The compiler for C++ is called g++ and is part of gcc, but it may need to be installed separately and doesn't come on all Linux installations by default  C++ files have the extensions.cc,.cpp,.cxx,.c++, and.C  I prefer.cpp, but.cc is also common g++ thing.cpp –o program

 C has too many ways to do things, but C++ is an order of magnitude worse  Syntax is a big mess of overlapping, ambiguous ideas  Which only got worse in the C++11 standard, which we aren't talking about  C++ tried to be reverse compatible with C, but not strictly true  It tried to be object-oriented, but not strictly true  The Standard Template Libraries are hideous compared to the Java Collection Framework  At the time, it was the best choice available for OOP, and now we're stuck with it

 It's not too different from C  We need different headers for C++ I/O #include using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } #include using namespace std; int main() { cout << "Hello, world!" << endl; return 0; }

 Output uses the cout object (of type ostream )  Instead of using formatting strings, cout uses the idea of a stream, where objects are placed into the stream separated by the extraction operator <<  The endl object adds a newline to the stream  Of course, "\n" works too int x = 50; cout << "There are " << x << " ways to leave your lover." << endl; int x = 50; cout << "There are " << x << " ways to leave your lover." << endl;

 Basic output is easier  What about setting the width or precision?  You need to include the iomanip header  Put setw( width ) in the stream to make the items take up the specified width  Put setprecision ( precision ) in the stream to show a certain number of decimal places  Put fixed to force it to pad with zeroes when there isn't enough precision double dollars = 2.0; cout << "Give me $" << setw(10) << fixed << setprecision(2) << dollars << setw(0) << "!" << endl; //printf equivalent printf("Give me $%10.2f!\n", dollars); double dollars = 2.0; cout << "Give me $" << setw(10) << fixed << setprecision(2) << dollars << setw(0) << "!" << endl; //printf equivalent printf("Give me $%10.2f!\n", dollars);

 Input uses the cin object (of type istream )  cin also uses the idea of a stream, where items are read from the stream and separated by the insertion operator >>  It reads items using whitespace as the separator, just like scanf() int x = 0; int y = 0; int z = 0; cout << "Enter the x, y, and z values: "; cin >> x >> y >> z; int x = 0; int y = 0; int z = 0; cout << "Enter the x, y, and z values: "; cin >> x >> y >> z;

 Like Java, C++ has a class for holding strings, which makes life much easier  It's called string (with a lower case 's' )  You must include to use it  Unlike String in Java, string is mutable  You can use array-style indexing to get and set individual characters string a = "Can I kick it?"; string b = "Yes, you can!"; string c = a + " " + b; c[0] = 'D'; c[1] = 'i'; c[2] = 'd'; cout << c << endl; //prints Did I kick it? Yes, you can! string a = "Can I kick it?"; string b = "Yes, you can!"; string c = a + " " + b; c[0] = 'D'; c[1] = 'i'; c[2] = 'd'; cout << c << endl; //prints Did I kick it? Yes, you can!

 Java uses packages to keep different classes with the same name straight  C++ uses namespaces  The standard library includes I/O ( ), the string class ( ), STL containers (,,, and others)  If you use these in your program, put the following after your includes  The alternative is to specify the namespace by putting the it followed by two colons before the class name using namespace std; std::string name = "Ghostface Killah";

 Regular C++ functions are very similar to functions in C  A big difference is that prototypes are no longer optional if you want to call the function before it's defined  Unlike C, function overloading allowed: int max(int a, int b) { return a > b ? a : b; } int max(int a, int b, int c) { return max( a, max( b, c)); } int max(int a, int b) { return a > b ? a : b; } int max(int a, int b, int c) { return max( a, max( b, c)); }

 In C, all functions are pass by value  If you want to change an argument, you have to pass a pointer to the value  In C++, you can specify that a parameter is pass by reference  Changes to it are seen on the outside  You do this by putting an ampersand ( & ) before the variable name in the header void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void swap(int &a, int &b) { int temp = a; a = b; b = temp; }

 Pass by reference is a great tool  You don't have to pass nearly as many pointers  If you want to change a pointer, you can pass it by reference instead of passing a pointer to a pointer  It does allow more mistakes  Leave off the ampersand and your function does nothing  Change things you didn't intend to change  You cannot pass a literal by reference swap(3, 9); //doesn't compile

 C++ also allows you to specify default values for function parameters  If you call a function and leave off those parameters, the default values will be used  Default parameters are only allowed for the rightmost grouping of parameters void build(int width = 2, int height = 4) { cout << "We built this house with " << width << " by " << height << "s."; } void build(int width = 2, int height = 4) { cout << "We built this house with " << width << " by " << height << "s."; } build(); //We built this house with 2 by 4s. build(3); //We built this house with 3 by 4s. build(6, 8); //We built this house with 6 by 8s. build(); //We built this house with 2 by 4s. build(3); //We built this house with 3 by 4s. build(6, 8); //We built this house with 6 by 8s.

 More C++

 Keep working on Project 6