Cours de C++ Introduction

Similar documents
CS242 COMPUTER PROGRAMMING

6.096 Introduction to C++ January (IAP) 2009

Chapter 1 Introduction to Computers and C++ Programming

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

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Another Simple Program: Adding Two Integers

Partha Sarathi Mandal

Introduction to Programming using C++

Streams. Ali Malik

CS 376b Computer Vision

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

Chapter 2: Introduction to C++

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

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

Your first C++ program

Introduction to C++ Systems Programming

A First Program - Greeting.cpp

Computer Science II Lecture 1 Introduction and Background

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

Introduction to Programming

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

Computer Programming : C++

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

Data Structures and Programming with C++

CE221 Programming in C++ Part 1 Introduction

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Chapter 2. C++ Basics

COMP322 - Introduction to C++ Lecture 01 - Introduction

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

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

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

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

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

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

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

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

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

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

Introduction to C++ Introduction to C++ 1

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Chapter 2: Overview of C++

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

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

EP241 Computer Programming

A SHORT COURSE ON C++

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Dr. Md. Humayun Kabir CSE Department, BUET

IS 0020 Program Design and Software Tools

Add Subtract Multiply Divide

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.

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

G52CPP C++ Programming Lecture 18

Outline. 1 About the course

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Scientific Computing

Introduction to Programming

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

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

Computer Science II Lecture 2 Strings, Vectors and Recursion

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

C++ Code Structure. Cooperating with the Compiler

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

Programming. C++ Basics

Engineering Problem Solving with C++, Etter/Ingber

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

AN OVERVIEW OF C++ 1

Objectives. In this chapter, you will:

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

CS11 Introduction to C++ Fall Lecture 1

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Fast Introduction to Object Oriented Programming and C++

Types, Values, Variables & Assignment. EECS 211 Winter 2018

UNIT- 3 Introduction to C++

Introduction to C++ (Extensions to C)

Programmazione. Prof. Marco Bertini

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

VARIABLES & ASSIGNMENTS

Pointers, Arrays and C-Strings

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

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

C++ for Python Programmers

CS302 - Data Structures using C++

by Pearson Education, Inc. All Rights Reserved. 2

Introduction to the C++ Programming Language

Object Oriented Design

A Freshman C++ Programming Course

Numbers. John Perry. Spring 2017

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

C++ Quick Guide. Advertisements

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Introduction to C ++

A brief introduction to C++

Transcription:

Cours de C++ Introduction Cécile Braunstein cecile.braunstein@lip6.fr Cours de C++ 1 / 20

Généralité Notes Interros cours 1/3 Contrôle TP 1/3 Mini-projet 1/3 Bonus (Note de Participation) jusqu à 2 points Références H. GARRETA, Le langage et la bibliothèque C++, ellipses 2000 A. KOENIG, B. MOO, Accelerated C++, Addison Wesley 2006 http://www.cplusplus.com http://www.google.com http://www.cppreference.com Cours de C++ 2 / 20

Why C++? C++ Middle-level language Developed by Bjarne Stroustrup in 1979 at Bell Labs First view as an extension of C (C with classes) Philosophy C++ is designed to be a statically typed, general-purpose language that is as efficient and portable as C C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly C++ is object language it implies : Re-use Modularity Maintainability Cours de C++ 3 / 20

Hello world! helloworld.cpp // The first programm #include <iostream> int main() { std::cout << "Hello, world!" << std::endl ; } return 0; Cours de C++ 4 / 20

Call the compiler g++ -Wall -g helloworld.cpp -o hello Compile Options g++ accepts most options as gcc Wall : all warnings g : include debug code o : specify the outfile name (a.out by default) Cours de C++ 5 / 20

Program details #include Many fundamentals facilities are part of standard library rather than core language #include <iostream> #include directive + angle brackets refers to standard header main function Every C++ program must contain a function named main. When we run the program, the implementation call this function. The result of this function is an integer to tell the implementation if the program ran successfully Convention : 0 : success 0 : fail Cours de C++ 6 / 20

Using the standard library for output std::cout << "Hello, world!" << std::endl; < < : output operator std : : : namespace std std : :cout : standard output stream std : :endl : stream manipulator Cours de C++ 7 / 20

Expressions in C++ Type A variable is an object that has a name. An object is a part of the memory that has a type. Every object, expression and function has a type. Types specify properties of data and operations on that data. Primitive types bool char int unsigned long short float double To improve the code re-use it is important to use the right type at the right place! Cours de C++ 8 / 20

Expressions in C++ Type conversion Goal : bring the operands to each operator to a common type. Some basic rules 1 Preserve information 2 Prefer unsigned conversion 3 Promote values to larger type 4 Preserve precision Cours de C++ 9 / 20

Expressions in C++ Type conversion Exercice #include<iostream> int main() { float a = 1.2; float c; int b; #include<iostream> int main () { int d = -1; unsigned int e = d; b = a; a = c; std::cout << c << std::endl; return 0; } std::cout << e << std::endl; return 0; } Cours de C++ 10 / 20

Variable definition #include <iostream> #include <string> int main() { std::cout <<"Your first name: "; std::string name; std::cin >> name; std::cout << "Hello, " << name << "!" << std::endl; return 0; } Variable can be define anywhere in the program. local variable are destroyed when an end of block is reached. Variable has a type and an interface The variable name is implicitly initialize How to define a variable type-name name ; type-name name = value ; type-name name(args) ; Cours de C++ 11 / 20

Expressions in C++ Definition An expression ask the implementation to compute something. The computation yields a result and may have side effects Example Result : 4+3 Side effect : std::cout << "Hello, world!" << std::endl; Result : Side effect : Cours de C++ 12 / 20

Expressions in C++ Definition An expression ask the implementation to compute something. The computation yields a result and may have side effects Example Result : 4+3 Side effect : std::cout << "Hello, world!" << std::endl; Result : Side effect : Cours de C++ 12 / 20

Expressions in C++ Definition An expression ask the implementation to compute something. The computation yields a result and may have side effects Example Result : 7 4+3 Side effect : std::cout << "Hello, world!" << std::endl; Result : Side effect : Cours de C++ 12 / 20

Expressions in C++ Definition An expression ask the implementation to compute something. The computation yields a result and may have side effects Example Result : 7 4+3 Side effect : none std::cout << "Hello, world!" << std::endl; Result : Side effect : Cours de C++ 12 / 20

Expressions in C++ Definition An expression ask the implementation to compute something. The computation yields a result and may have side effects Example Result : 7 4+3 Side effect : none std::cout << "Hello, world!" << std::endl; Result : std : :cout Side effect : Cours de C++ 12 / 20

Expressions in C++ Definition An expression ask the implementation to compute something. The computation yields a result and may have side effects Example Result : 7 4+3 Side effect : none std::cout << "Hello, world!" << std::endl; Result : std : :cout Side effect : writes Hello, world! and ends the output line Cours de C++ 12 / 20

Overloaded operators The effect of an operator depends on the type of its operands. Example #include<iostream> #include<string> int main() { // Example 1 int a = 2; int b = 3; std::cout << a + b << std::endl; } // Example 2 std::string s = "Hello,"; std::cout << s + "World!" << std::endl; return 0; Cours de C++ 13 / 20

Operators Precedence x.y The member y of object x a[k] The element in object a indexed by k ++x, - -x Incr/decr x, returning the original value of x x++, x- - Incr/decr x, returning the resulted value of x &x address of x *x value at the address x *, / Product, quotient % Remainder ; x-((x/y)*y) +, - Sum, Subtract ==,!= Yields a bool indicating whether x(is not) equal(s) y && logic AND logic OR x = y Assign the value y to x, yielding x as its result x op= y Compound assignment operator Cours de C++ 14 / 20

if/else statement if ( a == 0 ) std::cout << "a equals zero" << std::endl; use a block : if ( a == 0 ) { std::cout << "a equals zero" << std::endl; a = 1; } else{ std::cout << "a is not equal" << std::endl; } Cours de C++ 15 / 20

Other syntax While loop while ( a > 0 ) { a--; } do { a--; while ( a > 0 ); Cours de C++ 16 / 20

Other syntax For loop for ( a = 5 ; a > 0 ; a++ ) { std::cout << "a := " << a << std::endl; } for ( a = 5 ; a > 0 ; a++ ) { if ( a == 3 ) continue; if ( a == 1 ) break;; std:: cout << "a := " << a << std::endl; } Cours de C++ 17 / 20

switch statement for ( value=5 ; value>0 ; value-- ) { switch ( value ) { case 1: std::cout << "Case 1." << std::endl; break; case 2: std::cout << "Case 2." << std::endl; case 3: std::cout << "Case 3." << std::endl; break; default: std::cout << "Unknown." << endl; } } Cours de C++ 18 / 20

The type vector This a container Contains a collection of value. Each value has the same type. Different vectors can hold value of different types. Template classes A container is defined with the template classes. Separate the what it means to be vector from the type of the data inside. A lot of operation does not depend on the type of the element. Cours de C++ 19 / 20

The type vector Operations Action Insert an element Remove an element Remove all elements Returns a value that denotes the first element Returns a value that denotes (one past) the last element Returns a value that denotes the i th element Take the vector size Check emptiness Method v.push_back() v.pop_back() v.clear() v.begin() v.end() v[i] v.size() v.empty() Attention : The first element is indexed by 0 and the last by size-1. Cours de C++ 20 / 20