Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some...

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

Transcript of Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some...

Page 1: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

COMP322 - Introduction to C++

Lecture 01 - Introduction

Robert D. Vincent

School of Computer Science

6 January 2010

Page 2: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

What this course is

I Crash course in C++

I Only 14 lectures

I Single-credit course

Page 3: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

What this course is not

I An introduction to programming course

I A full OOP course

I A gentle tour of C and Java syntax

Page 4: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Course facts

I Course web page:http://www.cs.mcgill.ca/∼rvince3/322-w2010/

I Office hours: After class until further notice

I Academic Integrity: Seehttp://www.mcgill.ca/integrity

Page 5: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Assessment

I Two short quizzes, 20% eachI Short-answer, multiple-choice, true/falseI Given in class

I Four homework assignments, 20% eachI One or more short programming problemsI 3 weeks per assignmentI 10% per day late penaltyI Use GNU C++ (“g++”)I Comments and style will be counted, in addition to

correctness

I Final grade will be the sum of the best 5 scores, providedthe work does not violate academic integrity standards!

Page 6: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Your friendly staff

I InstructorI Robert Vincent, [email protected] Ph.D. candidate with Prof. Joelle PineauI Reasoning and Learning Lab, SOCSI McConnell 111

I Teaching assistantI Onur Duman, [email protected] M.Sc. student with Jorg KienzleI Software Engineering Lab, SOCSI McConnell 322

Page 7: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Course schedule1. 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)

Page 8: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

History of C++

I Begun in 1979 by Bjarne Stroustrup

I Originally called “C with Classes”

I First used outside Bell Labs in the mid-80’s

I ANSI/ISO standard (ISO/IEC 14882:1998)

I Important ancestor of Java

Page 9: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Design principles

I Compiles to machine (binary) code

I Compile-time type checking

I Flexible programming styles

I Low runtime overhead

I Minimal development environment

I Mostly compatible with C

Page 10: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Differences from C

I Classes

I Overloading

I Templates

I Exceptions

I Namespaces

Page 11: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Differences from Java

I Compiles to machine code

I Multiple inheritance

I Pointers and references

I Templates

I No garbage collection

Page 12: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Pros and cons

I Like C, C++ is useful for systems programming

I Commercially important!

I Can seem complex and difficult

I Allows serious errors and security problems

I Not quite as standard as either C or Java

I Lots of “missing features”

Page 13: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ Standard Library

I Includes most of the C Standard Library

I Derived from Standard Template Library (STL)

I Data types: Strings, complex numbers, etc.

I Containers: Lists, sets, queues, stacks, etc.

I Algorithms: Sorting and searching

Page 14: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics

I Statements terminated with semicolon

I Comments either between /* .. */ or after //

I Basic constants and types largely borrowed from C

I Most operators identical to those in C

I Parentheses are used to group expressions: a * (b + c)

I All identifiers must be declared before use, e.g.int inch; float sum = 0.0;

Page 15: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics - Basic types

The sizes and specific range values are typical for 32-bit systems.Type Bytes Min Maxbool 1 false truesigned char 1 SCHAR MIN (-128) SCHAR MAX (127)unsigned char 1 0 UCHAR MAX (255)char 1 CHAR MIN CHAR MAXshort [int] 2 SHRT MIN (-32768) SHRT MAX (32767)unsigned short [int] 2 0 USHRT MAX (65535)int 4 INT MIN INT MAXunsigned [int] 4 0 UINT MAXlong [int] 4 LONG MIN LONG MAXunsigned long [int] 4 0 ULONG MAXfloat 4 -FLT MAX +FLT MAXdouble 8 -DBL MAX +DBL MAXlong double 8 -LDBL MAX +LDBL MAX

Page 16: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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 canonicaltrivial program, intended to print a friendly greeting.

Page 17: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ example - hello.cpp

#include <iostream >

int main()

{

std::cout << "Hello , world !\n";

return 0; // Return code for success

}

I “#include” is a preprocessor directiveI Preprocessor runs before the compilerI The entire file “iostream” is incorporatedI No semicolon used in preprocessor statementsI Incorporates part of standard library

Page 18: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ example - hello.cpp

#include <iostream >

int main()

{

std::cout << "Hello , world !\n";

return 0; // Return code for success

}

I “main()” is a special functionI Control starts with this functionI It must be a global function returning intI Must be defined only once per projectI Is not part of any class

Page 19: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ example - hello.cpp

#include <iostream >

int main()

{

std::cout << "Hello , world !\n";

return 0; // Return code for success

}

I std::cout refers to a global objectI It is an object of the class ostreamI It is similar to the stdout global from CI The ’¡¡’ operator writes the objectI The ’::’ is the scope operator

Page 20: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ example - hello.cpp

#include <iostream >

int main()

{

std::cout << "Hello , world !\n";

return 0; // Return code for success

}

I return specifies value of function main()

I Takes an (optional) valueI The number zero is an integer constantI In this case, zero indicates successI Returns control to calling function

Page 21: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ example - Compiling and running

$ g++ -Wall -o hw hello.cpp

$ ./hw

Hello, world!

$

Page 22: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics - Arithmetic operators

Where possible, C++ will automatically convert among thebasic types.

+ // Addition and unary plus

- // Subtraction and unary negation

* // Multiplication

/ // Division

% // Integer remainder

Another important operator is the assignment operator:

= // Assignment

Page 23: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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

Page 24: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics - Logical operators

The logical && and || operators use short-circuit evaluation.They execute the right hand argument only if necessary todetermine the overall value.

&& // logical and

|| // logical or

! // logical negation

Page 25: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics - Bitwise operators

These operators support logical operations on bits. Forexample,

int x = 0x1001 ^ 0x2001;

std::cout << std::hex << x << std::endl;

would print 3000.

& // bitwise and

| // bitwise or

^ // bitwise exclusive or

~ // bitwise complement

<< // left shift

>> // right shift

Page 26: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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

}

Page 27: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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;

}

Page 28: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics - while statement

float array [10];

int i;

i = 0;

while (i < 10) {

array[i] = 0;

i++;

}

Page 29: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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;

}

Page 30: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

C++ basics - do while statement

int response;

do {

std::cin >> response;

processCommand(response)

} while (response != ’q’);

Page 31: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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;

}

Page 32: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

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

}

Page 33: Lecture 01 - Introductionrvince3/322-w2010/COMP322-L01.pdf · 1.06 Jan - Course introduction, some basics of C++ 2.13 Jan - Basic language features 3.20 Jan - Pointers and references

Preprocessor

The C++ preprocessor is inherited from C. It runs before thecompiler, processing its directives and outputting a modifiedversion of the input.

#define #include

#ifdef #ifndef

#if #elif

#else #endif

#line #undef

#error #pragma