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

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

Computer Programming : C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Increment and the While. Class 15

CS2141 Software Development using C/C++ C++ Basics

Pointers, Arrays and C-Strings

2 nd Week Lecture Notes

Chapter 1 Introduction to Computers and C++ Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

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++

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

Introduction to Programming using C++

UNIT- 3 Introduction to C++

A First Program - Greeting.cpp

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

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

Fundamentals of Programming CS-110. Lecture 2

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS242 COMPUTER PROGRAMMING

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

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

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

Programming with C++ as a Second Language

REVIEW. The C++ Programming Language. CS 151 Review #2

Introduction to C ++

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

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

Getting started with C++ (Part 2)

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

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

Introduction to Programming EC-105. Lecture 2

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

Scientific Computing

Tutorial-2a: First steps with C++ programming

Objectives. In this chapter, you will:

C++ Support Classes (Data and Variables)

Introduction to C++ Systems Programming

CE221 Programming in C++ Part 1 Introduction

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

In this chapter you will learn:

CS201 - Introduction to Programming Glossary By

Chapter 2: Introduction to C++

UEE1302 (1102) F10: Introduction to Computers and Programming

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

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

Numbers. John Perry. Spring 2017

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

CSCI 1061U Programming Workshop 2. C++ Basics

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

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

Short Notes of CS201

REPETITION CONTROL STRUCTURE LOGO

Introduction to Programming

Programming with C++ Language

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

BITG 1233: Introduction to C++

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

Creating a C++ Program

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Chapter 1 INTRODUCTION

LECTURE 02 INTRODUCTION TO C++

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

Variables. Data Types.

Computer Science II Lecture 1 Introduction and Background

C++ for Python Programmers

CHAPTER 3 BASIC INSTRUCTION OF C++

Understanding main() function Input/Output Streams

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

Programming. C++ Basics

4. Structure of a C++ program

CSCI 123 Introduction to Programming Concepts in C++

Programming, numerics and optimization

CS302 - Data Structures using C++

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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.

Input And Output of C++

Maciej Sobieraj. Lecture 1

6.096 Introduction to C++ January (IAP) 2009

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2: Overview of C++

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Computer Skills (2) for Science and Engineering Students

III. Classes (Chap. 3)

The C++ Language. Arizona State University 1

C++ Strings, Enums. Data Processing Course, I. Hrivnacova, IPN Orsay

COMP322 - Introduction to C++ Lecture 01 - Introduction

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Arrays. Week 4. Assylbek Jumagaliyev

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Fast Introduction to Object Oriented Programming and C++

Lecture 2 Tao Wang 1

Outline. 1 About the course

Visual C# Instructor s Manual Table of Contents

Introduction to C++ (Extensions to C)

Transcription:

C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1

C++ Programming language Features: C++ is C. C++ supports (almost) all the features of C. Like C, C++ allows programmers to manage the memory directly, so as to develop efficient programs. C++ is OO. C++ enhances the procedural-oriented C language with the objectoriented extension. The OO extension facilitates design, reuse and maintenance for complex software. Template C++. C++ introduces generic programming, via the so-called template. You can apply the same algorithm to different data types. STL. C++ provides a huge set of reusable standard libraries, in particular, the Standard Template Library (STL). 2

C++ Standards C++ is standardized as ISO/IEC 14882. Currently, there are two versions: C++98 (ISO/IEC 14882:1998): 1st standard version of C++. C++03 (ISO/IEC 14882:2003): minor "bug-fix" to C++98 with no change to the language. Commonly refer to as C++98/C++03 or First C++ standard. C++11 (ISO/IEC 14882:2011): 2nd standard version of C++. Formerly called C++0x, as it was expected to finalize in 200x. It adds some new features to the language; more significantly, it greatly extends the C++ standard library and standard template library (STL). We will use some features introduced in C++11 in our course, too 3

C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ #include <iostream> int main() { // print Hello, World! on the screen std::cout << Hello, World! << std::endl; 4

C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ #include <iostream> int main() { // print Hello, World! on the screen std::cout << Hello, World! << std::endl; Comments: /* */ - can span over several lines or be inserted in the code on one line // - lasts to the end of the line 5

C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ #include <iostream> int main() { // print Hello, World! on the screen std::cout << Hello, World! << std::endl; Include statement(s): Including all necessary declarations for input and output 6

C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ #include <iostream> int main() { // print Hello, World! on the screen std::cout << Hello, World! << std::endl; The main function called at the program startup, leaving main() ends the program 7

C++ Program /* The first C++ program * - just outputs 'Hello, World!' */ #include <iostream> int main() { // print Hello, World! on the screen std::cout << Hello, World! << std::endl; A statement ending with a semicolon; It writes the string Hello, World! followed by std::endl symbol, to the standard output 8

Function main() The function main is called at the program startup, leaving main() ends the program The function head: int the return type main the function name () the parameter list The function main() returns integer value If return statement is not present, the zero value is returned implicitly You can also write the statement explicitly: return 0; In C programs the return statement cannot be omitted 9

Input and Output (IO) Chua Hock-Chuan: Programming Notes C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). 10

Input and Output The input/output (I/O) functionality is provided in components of standard library Generally assigned to the keyboard (input) and the screen (output) Can be redirected by the operating system to a file std::cin - input channel (the keyboard) std::cout - output channel (the screen) #include <iostream> Standard channels to provide input/output: Symbols To get these components known to the program: << operator : std::endl - line break (new line) Send data to the output channel >> operator : Read data from an input channel 11

Namespaces The prefix std:: is used for all symbols of the standard library Using the namespace concept, symbols can be grouped logically within a package (or a component) Avoids names clashes in large program using namespace directive can be used to make all symbols from a namespace available without std:: prefix #include <iostream> using namespace std; cout << Hello, World! << std; 12

Four digits program We will discuss the Four digits program (from the C++ book) line by line to learn the basic language concepts: Variables, Types, Operators and Control Constructs The program will list all four digits numbers that fulfill the following condition If you split a four digits number into two parts each having two digits and add the squares of these numbers, you get the original four digit number: Eg. 1233 = 12*12 + 33*33 13

Code #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { // separate the first and last two digits int front = number / 100; // the first two digits int back = number % 100; // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 14

Variables #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { A variable counter is defined to count how many four-digit numbers were found: int ~ type counter ~ name = 0 ~ initialized to the value 0 // separate the first and last two digits int front = number / 100; // the first two digits int back = number % 100; // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 15

Variable NAME VALUE TYPE counter 0 int number pi 1556 3.141592 int double It allocates memory The current memory content is its value counter has the value 0 The size of memory allocated and the operations we can do with a variable are defined by its type A variable has a name, stores a value of the declared type 16

Variable Declaration Variable declaration = the instruction that creates the variable We can declare a variable without giving it an initial value; its value is undefined int counter; We can also declare a variable with giving it its initial value: int counter = 0; Other ways of initialization: int counter(0); int counter = { 0 ; We prefer to declare variables just before their use The const declaration of a variable - creates a variable that can not be changed later const double PI = 3.14; PI = 5.4; // error: cannot assign to variable 'PI' // with const-qualified type 'const double' 17

Variable Type Defines how the object (its binary configuration in memory) can be manipulated Fundamental types Numeric: integer (int), floating point numbers (float, double) Characters: char Enclosed in single quotes: 'a', '0' Special characters must be masked with a backslash: \', \ Boolean: bool: can have only two values: true, false Object types Defined in the C++ standard library and by the programmer Have their own defined functions; can also have their own defined operators enum: for enumeration types (names that represent integral value) void: nothing (for functions without a return value) 18

Loop For loop: iterates over certain values composed of 3 statements: 1)int number=1000; 2)number<10000; 3)++number 1) Initialize loop variable: executed once 2) Condition: loop continues as long as the condition is true 3) Increment loop variable: in each iteration #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { // separate the first and last two digits int front = number / 100; // the first two digits int back = number % 100; // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 19

Loops for (int i = 10; i < 10; ++i ) {... int counter = 10; while ( counter < 10 ) {... int counter = 10; do {... while ( counter < 10 ); For loop While loop = pre-test loop preferred when it is possible that the block of code will not be executed Do loop = post-test loop preferred when the block must always be executed (at least once) 20

Loops: break, continue for ( if if // int i = 0; i < 10; i++ ) { ( i == j ) continue; ( i > k ) break; do something break - allows to quit the loop earlier continue - allows to skip one iteration But be careful - it should not be abused (sign of bad programming style) 21

Operators #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { Using operators to separate first two and last two digits: Operator '/' ~ division Operator '%' ~ modulo // separate the first and last two digits int front = number / 100; int back = number % 100; // the first two digits // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 22

Operators Basic operators Basic operators + * / % addition, positive sign subtraction, negative sign multiplication division modulo operator < <= > >= ==!= less than less than or equal to greater than greater than or equal to equal to not equal to && logical AND logical OR! logical negation Increment and Decrement Operators a++ a-++a Assignment Operators: --a = simple assignment += addition -= subtraction *= multiplication /= division... postfix increment postfix decrement prefix increment prefix decrement Bit operators Special operators 23

Tests An if statement checks whether the sum of the squares of two-digits numbers produces the original number Inside if using multiplication (*), addition (+) and equal to (==) operators Note == is different from = #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { // separate the first and last two digits int front = number / 100; // the first two digits int back = number % 100; // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 24

Tests if ( x < 7 ) { cout << x is less than 7 << endl; if ( x < 7 ) { cout << x is less than 7 << endl; else { cout << x is greater than or equal to 7 << endl; The { are not necessary if the expression does not exceed one line But we prefer to include always braces in order to avoid a possible problems when extending the code on more lines 25

Flow Control Chua Hock-Chuan: Programming Notes 26

Code If the condition inside loop is satisfied: An appropriate message is written to the standard output channel: 1233 == 12*12 + 33*33 The counter is increased by one using increment operator: ++counter #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { // separate the first and last two digits int front = number / 100; // the first two digits int back = number % 100; // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 27

Code The result (the value of all numbers found) is then printed at the end of program: X numbers found #include <iostream> // C++ header file for I/O using namespace std; int main() { int counter = 0; // current number of found four-digit numbers // for every number from 1000 to 9999 for (int number=1000; number<10000; ++number) { // separate the first and last two digits int front = number / 100; // the first two digits int back = number % 100; // the last two digits // if the sum of the squares produce the original number, // output number and increment counter if (front*front + back*back == number) { cout << number << " == " << front << "*" << front << " + " << back << "*" << back << endl; ++counter; // output number of four-digit numbers found cout << counter << " numbers found" << endl; 28