Introduction to C++ (Extensions to C)

Similar documents
Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

3.1. Chapter 3: The cin Object. Expressions and Interactivity

CS2141 Software Development using C/C++ Stream I/O

C++ basics Getting started with, and Data Types.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

I/O Streams and Standard I/O Devices (cont d.)

C++ Input/Output: Streams

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Chapter 15 - C++ As A "Better C"

CS3157: Advanced Programming. Outline

Streams. Rupesh Nasre.

File I/O Christian Schumacher, Info1 D-MAVT 2013

Introduction to C++ Systems Programming

Expressions, Input, Output and Data Type Conversions

LECTURE 02 INTRODUCTION TO C++

Unit 1 : Principles of object oriented programming

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

The C++ Language. Arizona State University 1

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

Chapter 3 - Notes Input/Output

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lecture 4 Tao Wang 1

Computer Programming : C++

CS242 COMPUTER PROGRAMMING

Engineering Problem Solving with C++, Etter/Ingber

Understanding main() function Input/Output Streams

Window s Visual Studio Output

C++ Programming: From Problem Analysis to Program Design, Third Edition

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

BITG 1233: Introduction to C++

WARM UP LESSONS BARE BASICS

Getting started with C++ (Part 2)

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Introduction to Programming EC-105. Lecture 2

Definition Matching (10 Points)

Chapter 3. Numeric Types, Expressions, and Output

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Fundamentals of Programming CS-110. Lecture 2

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Programming with C++ as a Second Language

Week 3: File I/O and Formatting 3.7 Formatting Output

This watermark does not appear in the registered version - Slide 1

CSCI 1061U Programming Workshop 2. C++ Basics

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Streams - Object input and output in C++

Objectives. In this chapter, you will:

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

C++ Quick Guide. Advertisements

Fast Introduction to Object Oriented Programming and C++

UNIT- 3 Introduction to C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Object Oriented Design

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

G52CPP C++ Programming Lecture 18

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Sample Code: OUTPUT Daily Highs & Lows

2 nd Week Lecture Notes

Objects and streams and files CS427: Elements of Software Engineering

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 1 Introduction to Computers and C++ Programming

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Interview Questions of C++

CHAPTER 3 Expressions, Functions, Output

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

COMP322 - Introduction to C++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Introduction to C++ Introduction to C++ 1

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Chapter 2. Outline. Simple C++ Programs

Lab # 02. Basic Elements of C++ _ Part1

CMPS 221 Sample Final

CSE 333 Lecture 9 - intro to C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Transcription:

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!"; preprocessor directive which namespace to use } return 0; Note: C++ files have a.cpp extension and are compiled using g++

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!";

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";

Formatting Output Can control how output displays for numeric and string data size position number of digits Requires iomanip header file

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.

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

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 3-13

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;

The cin Object 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.

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.

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; }

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. using namespace std; int main() { cout<< "This is a test. " << endl; }

Type Casting Used for manual data type conversion Format Example: static_cast<data Type>(Value) 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;

Older Type Cast Styles double Volume = 21.58; int intvol1, intvol2; intvol1 = (int) Volume; // C-style // cast intvol2 = int (Volume); //Prestandard // C++ style // cast C-style cast uses prefix notation Prestandard C++ cast uses functional notation static_cast is the current standard

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 0-22