Introduction to C++ (Extensions to C)

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
 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.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Chapter 3: Input/Output
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 15 - C++ As A "Better C"
Lecture 3 Expressions, Type Conversion, Math and String
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
Chapter 3: Expressions and Interactivity
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Chapter 3: Expressions and Interactivity
Basic Elements of C++ Chapter 2.
Expressions and Interactivity
2.1 Parts of a C++ Program.
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.
Chapter 5 Input and Output Streams
Expressions and Interactivity
Chapter 3: Input/Output
Introduction to cout / cin
Chapter 3 Input output.
Introduction to C++ Programming
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Chapter 15 - C++ As A "Better C"
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Basics
Introduction to cout / cin
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ for Engineers and Scientists Second Edition
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking. Missing casts that produce warnings in C produce errors in C++ the introduction of true O-O classes a formal inheritance mechanism in which derived classes can specialize parent classes formal support for polymorphic behavior in which a derived class may override a base class method simply by providing a method of the same name.

Extensions to C (cont'd) support for function overloading in which there may be different implementations with a single function name. an operator overloading mechanism that is analogous to function overloading the ability to pass parameters by reference in addition to the standard pass by value. yet another input/output library that may be used in addtion to standard and low level I/O

The Parts of a C++ Program // sample C++ program #include <iostream> using namespace std; int main() { cout << "Hello, there!"; return 0; } Note: C++ files have a .cpp extension and are compiled using g++ preprocessor directive which namespace to use

Interactive I/O using cin and cout C++ uses streams to perform input/output operations A stream is an object that allows a program to insert characters to it or extract characters from it. The standard input/output stream objects are defined in the header file iostream. <iostream> must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output.

Interactive I/O using cin and cout With <iostream> you automatically get the following objects: cout – a predefined object of the ostream class that displays data to standard output ; corresponds to stdout. Used cin – a predefined object of the istream class that reads data from standard input; corresponds to stdin cerr – a predefined object of the ostream class that represents the standard error stream; corresponds to stderr. clog – a predefined object of the ostream class represents the standard logging stream; is associated with stderr. endl – outputs an end of line character and flushes the buffer.

The cout Object Displays information on the computer screen Use << , the insertion operator, to send information to cout cout << "Hello, there!"; Can use << to send multiple items to cout cout << "Hello, " << "there!"; Or cout << "Hello, "; cout << "there!"; See pr2-02.cpp and pr 2-03.cpp

The cout object Example cout << "Hello"; cout << " C++ is great!"; cout << endl; Or cout << "Hello" << " C++ is great!" << endl; no ; until the last item

Starting a New Line To get multiple lines of output on screen - Use endl cout << "Hello, there!" << endl; Use \n in an output string cout << "Hello, there!\n"; See pr2-04.cpp, pr2-05.cpp, and pr2-06.cpp

Formatting Output Can control how output displays for numeric and string data size position number of digits Requires iomanip header file For an example without iomanip, see pr3-14.cpp

Stream Manipulators Used to control features of an output field Some affect just the next value displayed setw(x): Print in a field at least x spaces wide. It will use more spaces if specified field width is not big enough. See pr3-15.cpp and pr3-16.cpp

Stream Manipulators Some affect values until changed again fixed: Use decimal notation (not E-notation) for floating-point values. setprecision(x): When used with fixed, print floating-point value using x digits after the decimal. Without fixed, print floating-point value using x significant digits. showpoint: Always print decimal for floating-point values. left, right: left-, right justification of value See pr3-17.cpp, pr3-18.cpp, pr3-19.cpp, and pr3-20.cpp

Manipulator Examples const float e = 2.718; float price = 18.0; Displays cout << setw(8) << e << endl; ^^^2.718 cout << left << setw(8) << e << endl; 2.718^^^ cout << setprecision(2); cout << e << endl; 2.7 cout << fixed << e << endl; 2.72 cout << setw(6) << price; ^18.00

The cin Object Standard input object Like cout, requires iostream file Used to read input from the keyboard Often used with cout to display a user prompt first Data is retrieved from cin with >> Input data is stored in one or more variables See pr3-01.cpp

cout << "How tall is the room? "; cin >> height; The cin Object Line at a time input User input goes from keyboard to the input buffer, where it is stored as characters cin converts the data to the type that matches the variable int height; cout << "How tall is the room? "; cin >> height; See pr3-02.cpp

The cin Object cin >> height >> width; Can be used to input multiple values cin >> height >> width; Or cin >> height >> width; Multiple values from keyboard must be separated by spaces or [Enter] Must press [Enter] after typing last value Multiple values need not all be of the same type Order is important; first value entered is stored in first variable, etc. See pr3-03.cpp and pr3-04.cpp

Namespaces in C++ In C++, as in other languages, variables, functions, and objects are program entities that must have names. C++ uses namespaces to organize the names of program entities. Elements within a namespace may be accessed using the scope resolution operator (::). For example, std::cout refers to the cout stream that is a member of the std namespace.

cout<< "This is a test. " << std::endl; } Namespaces in C++ To avoid repeatedly having to type namespace qualifiers, C++ provides a directive called using, which creates an alias to some element of another namespace. For example: int sample() { using std::cout; cout<< "This is a test. " << std::endl; }

cout<< "This is a test. " << endl; } Namespaces in C++ For frequently-used namespaces, such as std, a special directive can be placed at the top of the file: using namespace std; This directive makes every element inside the std namespace available in the current (usually the global) namespace. int main() { cout<< "This is a test. " << endl; }

// Displays 4 Type Casting Used for manual data type conversion Format static_cast<Data Type>(Value) Example: cout << static_cast<int>(4.2); // Displays 4

More Type Casting Examples char ch = 'C'; cout << ch << " is stored as " << static_cast<int>(ch); gallons = static_cast<int>(area/500); avg = static_cast<double>(sum)/count; See pr3-07.cpp and pr3-08.cpp

Older Type Cast Styles double Volume = 21.58; int intVol1, intVol2; intVol1 = (int) Volume; // C-style // cast intVol2 = int (Volume); //Prestandard // C++ style C-style cast uses prefix notation Prestandard C++ cast uses functional notation static_cast is the current standard See pr3-09.cpp

The bool Data Type Represents values that are true or false bool values are stored as integers false is represented by 0, true by 1 bool allDone = true; bool finished = false; allDone finished 1 See pr2-16.cpp