COMP322 - Introduction to C++ Lecture 01 - Introduction

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

Introduction to Programming using C++

AN OVERVIEW OF C++ 1

Computer Science II Lecture 1 Introduction and Background

Scientific Computing

CSCE 110 PROGRAMMING FUNDAMENTALS

6.096 Introduction to C++ January (IAP) 2009

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

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

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

IS 0020 Program Design and Software Tools

COMP322 - Introduction to C++

CS 376b Computer Vision

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Chapter 2: Introduction to C++

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

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Computer Programming : C++

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

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

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

CE221 Programming in C++ Part 1 Introduction

Your first C++ program

MA400: Financial Mathematics

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

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:

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

EP241 Computer Programming

LECTURE 02 INTRODUCTION TO C++

Outline. 1 About the course

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

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

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Introduction to C++ Introduction to C++ 1

Chapter 1 Introduction to Computers and C++ Programming

A First Program - Greeting.cpp

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:

C++ for Python Programmers

Chapter 2 Basic Elements of C++

PIC 10A Objects/Classes

Introduction to C++ Systems Programming

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

Cours de C++ Introduction

Homework #3 CS2255 Fall 2012

UNIT- 3 Introduction to C++

CS

Chapter 2: Overview of C++

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

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

Fundamentals of Structured Programming

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

CSE 303: Concepts and Tools for Software Development

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

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

CSI33 Data Structures

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Introducing C++ to Java Programmers

Operators. Java operators are classified into three categories:

Introduction to Programming

Week 1: Hello World! Muhao Chen

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 106B, Lecture 1 Introduction to C++

Lecture 3 Tao Wang 1

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Unit 7. 'while' Loops

Introduction to C++ (Extensions to C)

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

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

ME 461 C review Session Fall 2009 S. Keres

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

VARIABLES & ASSIGNMENTS

Understanding main() function Input/Output Streams

Topic 6: A Quick Intro To C

Increment and the While. Class 15

Introduction to Programming

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

UEE1302 (1102) F10: Introduction to Computers and Programming

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

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

C++ Quick Guide. Advertisements

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

377 Student Guide to C++

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CSCE 206: Structured Programming in C++

Computer Components. Software{ User Programs. Operating System. Hardware

Dr. Md. Humayun Kabir CSE Department, BUET

Short Notes of CS201

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

CS11 Intro C++ Spring 2018 Lecture 1

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

Transcription:

COMP322 - Introduction to C++ Lecture 01 - Introduction Robert D. Vincent School of Computer Science 6 January 2010

What this course is Crash course in C++ Only 14 lectures Single-credit course

What this course is not An introduction to programming course A full OOP course A gentle tour of C and Java syntax

Course facts Course web page: http://www.cs.mcgill.ca/ rvince3/322-w2010/ Office hours: After class until further notice Academic Integrity: See http://www.mcgill.ca/integrity

Assessment Two short quizzes, 20% each Short-answer, multiple-choice, true/false Given in class Four homework assignments, 20% each One or more short programming problems 3 weeks per assignment 10% per day late penalty Use GNU C++ ( g++ ) Comments and style will be counted, in addition to correctness Final grade will be the sum of the best 5 scores, provided the work does not violate academic integrity standards!

Your friendly staff Instructor Robert Vincent, bert@cs.mcgill.ca Ph.D. candidate with Prof. Joelle Pineau Reasoning and Learning Lab, SOCS McConnell 111 Teaching assistant Onur Duman, onur.duman@mail.mcgill.ca M.Sc. student with Jörg Kienzle Software Engineering Lab, SOCS McConnell 322

Course schedule 1. 06 Jan - Course introduction, some basics of C++ 2. 13 Jan - Basic language features 3. 20 Jan - Pointers and references (Assignment 1 out) 4. 27 Jan - Memory management 5. 03 Feb - Input/output using the Standard Library 6. 10 Feb - Classes (Assignment 1 due, Assignment 2 out) 7. 17 Feb - Quiz 1, Overloading 8. 03 Mar - Inheritance (Assignment 2 due, Assignment 3 out) 9. 10 Mar - Inheritance continued 10. 17 Mar - Exceptions 11. 24 Mar - Templates (Assignment 3 due, Assignment 4 out) 12. 31 Mar - Quiz 2, Templates continued 13. 07 Apr - More on the Standard Library 14. 14 Apr - Finish the Standard Library (Assignment 4 due)

History of C++ Begun in 1979 by Bjarne Stroustrup Originally called C with Classes First used outside Bell Labs in the mid-80 s ANSI/ISO standard (ISO/IEC 14882:1998) Important ancestor of Java

Design principles Compiles to machine (binary) code Compile-time type checking Flexible programming styles Low runtime overhead Minimal development environment Mostly compatible with C

Differences from C Classes Overloading Templates Exceptions Namespaces

Differences from Java Compiles to machine code Multiple inheritance Pointers and references Templates No garbage collection

Pros and cons Like C, C++ is useful for systems programming Commercially important! Can seem complex and difficult Allows serious errors and security problems Not quite as standard as either C or Java Lots of missing features

C++ Standard Library Includes most of the C Standard Library Derived from Standard Template Library (STL) Data types: Strings, complex numbers, etc. Containers: Lists, sets, queues, stacks, etc. Algorithms: Sorting and searching

C++ basics Statements terminated with semicolon Comments either between /*.. */ or after // Basic constants and types largely borrowed from C Most operators identical to those in C Parentheses are used to group expressions: a * (b + c) All identifiers must be declared before use, e.g. int inch; float sum = 0.0;

C++ basics - Basic types The sizes and specific range values are typical for 32-bit systems. Type Bytes Min Max bool 1 false true signed char 1 SCHAR MIN (-128) SCHAR MAX (127) unsigned char 1 0 char 1 CHAR MIN CHAR MAX short [int] 2 SHRT MIN (-32768) SHRT MAX (32767) unsigned short [int] 2 0 USHRT MAX (65535) int 4 INT MIN INT MAX unsigned [int] 4 0 UINT MAX long [int] 4 LONG MIN LONG MAX unsigned long [int] 4 0 ULONG MAX float 4 -FLT MAX +FLT MAX double 8 -DBL MAX +DBL MAX long double 8 -LDBL MAX +LDBL MAX

C++ example - hello.cpp # include < iostream > int main () { std :: cout << " Hello, world!\n"; return 0; // Return code for success } This text, contained in the file hello.cpp, is the canonical trivial program, intended to print a friendly greeting.

C++ example - hello.cpp # include < iostream > int main () { std :: cout << " Hello, world!\n"; return 0; // Return code for success } #include is a preprocessor directive Preprocessor runs before the compiler The entire file iostream is incorporated No semicolon used in preprocessor statements Incorporates part of standard library

C++ example - hello.cpp # include < iostream > int main () { std :: cout << " Hello, world!\n"; return 0; // Return code for success } main() is a special function Control starts with this function It must be a global function returning int Must be defined only once per project Is not part of any class

C++ example - hello.cpp # include < iostream > int main () { std :: cout << " Hello, world!\n"; return 0; // Return code for success } std::cout refers to a global object It is an object of the class ostream It is similar to the stdout global from C The operator writes the object The :: is the scope operator

C++ example - hello.cpp # include < iostream > int main () { std :: cout << " Hello, world!\n"; return 0; // Return code for success } return specifies value of function main() Takes an (optional) value The number zero is an integer constant In this case, zero indicates success Returns control to calling function

C++ example - Compiling and running $ g++ -Wall -o hw hello.cpp $./hw Hello, world! $

C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types. + // Addition and unary plus - // Subtraction and unary negation * // Multiplication / // Division % // Integer remainder Another important operator is the assignment operator: = // Assignment

C++ basics - Comparison operators The result of a comparison operator is always a value of type bool : == // equal!= // not equal > // greater than < // less than >= // greater than or equal <= // less than or equal

C++ basics - Logical operators The logical && and operators use short-circuit evaluation. They execute the right hand argument only if necessary to determine the overall value. && // logical and // logical or! // logical negation

C++ basics - Bitwise operators These operators support logical operations on bits. For example, int x = 0 x1001 ^ 0 x2001 ; std :: cout << std :: hex << x << std :: endl ; would print 3000. & // bitwise and // bitwise or ^ // bitwise exclusive or ~ // bitwise complement << // left shift >> // right shift

C++ basics - if statement // Simplest form if ( response == y ) return true ; // Less simple if ( result > 0.0) { x = 1.0 / result ; y += x; } else { std :: cout << " Division by zero!"; }

C++ basics - switch statement int response ; std :: cin >> response ; // Get input switch ( response ) { case y : return true ; case n : return false ; case q : exit (0); default : std :: cout << "I didn t get that, sorry \n"; break ; }

C++ basics - while statement float array [10]; int i; i = 0; while (i < 10) { array [i] = 0; i ++; }

C++ basics - for statement Typically a shorthand for common forms of the while statement. float array [10]; for ( int i = 0; i < 10; i ++) { array [i] = 0; }

C++ basics - do while statement int response ; do { std :: cin >> response ; processcommand ( response ) } while ( response!= q );

C++ basics - Identifier scope int v = 1; // Global scope int main () { int c = 5; // Local scope } // Declare i in statement scope for ( int i = 0; i < c; i ++) { // do something } // i is now undefined c = c + v;

C++ basics - Functions /* Calculate the mean of an array */ double mean ( double data [], int n) { double sum = 0.0; // Initialization if ( n!= 0) return 0.0; for ( int i = 0; i < n; i ++) sum += data [i]; return sum / n; } /* Impractical recursive factorial */ long factorial ( long t) { if ( t <= 1) return 1; return t * factorial ( t - 1); }

Preprocessor The C++ preprocessor is inherited from C. It runs before the compiler, processing its directives and outputting a modified version of the input. #define #ifdef #if #else #line #error #include #ifndef #elif #endif #undef #pragma