C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

30
C++ Workshop C++ Workshop Mark Hennessy Mark Hennessy Dept. Computer Science Dept. Computer Science 18 18 th th – 22 – 22 nd nd September 2006 September 2006

Transcript of C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Page 1: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

C++ WorkshopC++ Workshop

Mark HennessyMark Hennessy

Dept. Computer ScienceDept. Computer Science

1818thth – 22 – 22ndnd September 2006 September 2006

Page 2: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Week OutlineWeek Outline

• Monday – Intro to C++ Basics

• Tuesday – Classes, Overloading, Exceptions

• Wednesday – Defining Data Structures

• Thursday – OO Development in C++

• Friday – An introduction to the STL and templates.

Page 3: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Day outlineDay outline

• Lectures : 930 – 1100

• Assignment

• 1500: 90 mins later on in the day.

Page 4: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

WorkstationWorkstation

• We will use MS Windows but GNU/Linux has more support for C++ development.

• Compiler is g++ from the GNU Compiler Collection, gcc.

• This course is about development in ISO/ANSI standard C++

• -> We will not be using Microsoft Visual C++!

Page 5: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Practical InfoPractical Info

• Work on the C:\ disk can only be saved in the temp folder.

• WARNING!!! If you leave your work in the temp folder and then log out it will be lost!

• Therefore save all of your work to your personal X:\ disk or on some USB storage.

Page 6: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

C++ ResourcesC++ Resources

• Textbooks here in the lab.

• Online resources:

1. http://en.wikipedia.org/wiki/C++_standard_library

2. http://www.cplusplus.com/

3. http://www.sgi.com/tech/stl/

Page 7: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

5 Kinds of Programmers5 Kinds of Programmers

• Those who can’t

• those who can without pointers

• those who can with pointers

• those who know object technology: OO

• those who can use generics/templates

Page 8: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Motivation for OO with C++Motivation for OO with C++

• C++/Java most widely used languages,

• OO is the way of the present,• familiarity increases marketability,• viable for many years,• excellent expressivity, and• excellent speed.• Easy to learn another language

Page 9: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Lots of “stuff”Lots of “stuff”

• There is an intimidating amount of material in C++

• C++ was designed to be a powerful tool for professional programmers solving real problems in diverse domains

• C++ was NOT designed for academia!• C++ was NOT designed to be a nice, “pure”

language, good for teaching students how to program

Page 10: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

C++ IntroC++ Intro

• We need to learn the language, start with the syntax!

• Any Java programmers will be fine, C programmers will survive too!

• Lets get started….

Page 11: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Hello WorldHello World

#include <iostream>

int main(int argc, char * argv[])

{

std::cout<<“Hello World”<<std::endl;

}

Page 12: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

IdentifiersIdentifiers

• An identifier must start with a letter or underscore, and be followed by zero or more letters

• C++ is case sensitive

• VALID

age_of_dog TaxRate98

PrintHeading AgeOfHorse

• INVALID examples - try to find them yourselves using the compiler

Page 13: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

C++ Data TypesC++ Data Types

Structured

array struct union class

Address

pointer reference

Simple

Integral Floating

char short int long enum

float double long double

Page 14: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Typical size of data typesTypical size of data types

Type Number of Bytes

char 1

short (short int) 2

int 4

unsigned int 4

enum 2

long (long int) 4

float 4

double 8

long double 12

Page 15: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Finding size of data type - system dependent resultsFinding size of data type - system dependent results

• Use sizeofsizeof to find the size of a typee.g.std::cout << sizeof(int)

Page 16: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Enumerated types: Enumerated types: enumenum

Used to define constant values

enum days{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} yesterday, today;

days tomorrow;

QUESTION: what are the values of Tuesday, today, tomorrow, yesterday?

Default values may be overridden in the enum list.

Page 17: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Boolean typeBoolean type

C++ has a built-in logical or Boolean type

#include <iostream.h>

int main (){bool flag = true;std::cout << flag<< std::endl<<!flag}

This outputs:

1

0

Page 18: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Variation - booleans are really integers?

//

#include <iostream.h>

int main (){bool flag1 = 100, flag2 = false, flag3;std::cout << flag1<< std::endl<<flag2<<std::endl<<flag3;}

This outputs:

1

0

122

true is any non-zero int

false is zero

true is output as 1false is output as 0

This was not initialised

Page 19: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Giving a value to a variable

In your program you can assign (give) a value to the variable by using the assignment operator =

Age_Of_Dog = 12;

or by another standard method, such as

std::cout << “How old is your dog?”;std::cin >> Age_Of_Dog;

Page 20: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

What is a Named Constant?What is a Named Constant?

• A named constant is a location in memory which we can refer to by an identifier, and in which a data value that cannot be changed is stored.

VALID CONSTANT DECLARATIONS• const char STAR = ‘*’ ;• const float PI = 3.14159 ;• const int MAX_SIZE = 50 ;

Page 21: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

keywords: words reserved to C++keywords: words reserved to C++

• bool, true, false,• char, float, int, unsigned, double, long• if, else, for, while, switch, case, break,

• class, public, private, protected, new,

delete, template, this, virtual,

• try, catch, throw.

Note: there are many more … you may see them in the examples that follow

Page 22: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Example 6: prefix and postfix

//

#include <iostream.h>

int main (){int x =3, y=3;std::cout << ++x << std::endl;std::cout << y++ << std::endl;}

Output: 43

++ (prefix) is a unary operator

++ (postfix) is a unary operator

<< is a binary operator

Page 23: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Example 7: a C+ ternary operator

// Example 7

#include <iostream.h>

int main (){int x =3, y=4;std::cout <<"The max. of x and y is: " << (x>y?x:y);}

The max. of x and y is: 4

expression1?expression2:expression3

Output:

Means: if expression1 then expression2 else expression3

Page 24: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Program with Three Functions

// #include <iostream>// declares these 2 functionsint Square ( int );int Cube ( int ) ;

int main ( void ){ std::cout << "The square of 27 is "<< Square (27) << std::endl ;// function call

std::cout << "The cube of 27 is "<< Cube (27) << std::endl ;// function call

return 0 ;}int Square ( int n ){return n * n ;}int Cube ( int n ){return n * n * n ;}The square of 27 is 729

The cube of 27 is 19683Output:

Page 25: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Precedence of Some C++ OperatorsPrecedence of Some C++ Operators

Precedence Operator Description

Higher ( ) Function call

+ Positive

- Negative

* Multiplication

/ Division

% Modulus (remainder)

+ Addition

- Subtraction

Lower = Assignment

NOTE: Write some programs to test your understanding of precedence

Page 26: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Type Casting is Explicit Conversion of TypeType Casting is Explicit Conversion of Type

// Example #include <iostream>

int main ( void ){ std::cout << "int(4.8) ="<<int(4.8)<<std::endl;std::cout << "float(5) ="<<float(5)<<std::endl;std::cout <<”float(2/4)="<<float(2/4)<<std::endl;std::cout<<"float(2)/float(4)

="<<float(2)/float(4)<<std::endl;}

int(4.8) =4float(5) =5float(2/4)=0float(2)/float(4)=0.5

Output:Output of float may look like an int

Page 27: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Parts of a FunctionParts of a Function

Every function has 2 parts

int main (void)

{

return 0;

}

signature

body

Page 28: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

HEADER FILE FUNCTION EXAMPLEHEADER FILE FUNCTION EXAMPLE VALUE OF CALL VALUE OF CALL

std::fabs(x) std::fabs(-6.4) 6.4

<math> std::pow(x,y) std::pow(2.0,3.0) 8.0

std::sqrt(x) std::sqrt(100.0) 10.0

<iomanip> std::setprecision(n) std::setprecision(3)

std::log(x) std::log(2.0) 0.693147

std::sqrt(2.0) 1.41421

<stdlib> std::abs(i) std::abs(-6) 6

Page 29: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

C++ I/O BasicsC++ I/O Basics

29

I/O - Input/Output is one of the first aspects of programming that needs to be mastered:

•formatting

•whitespace

•structured inputs - getting data into the correct internal form

However, do not fall into the beginner’s traps:

•brilliant I/O with incorrect functionality

•thinking functionality is wrong because I/O is too complicated to get right

•forgetting that random tests are often better than user dependent I/O

Page 30: C++ Workshop Mark Hennessy Dept. Computer Science 18 th – 22 nd September 2006.

Whitespace Characters Include . . .Whitespace Characters Include . . .

• blanks• tabs ‘\t’• end-of-line (newline) characters ‘\n’

The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or “\n” in a program.

E.g

std::cout << std::endl;

std::cout << “\n”;