C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++...

37
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++ input and output streams. First exposure to simple C/C++ variables. Basic C++ types. Control and Looping Functions in C Function/method signatures and scope

Transcript of C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++...

Page 1: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

C++ / G4MICE Course

Session 1 - Introduction

Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file.

Understand the C++ input and output streams. First exposure to simple C/C++ variables.

Basic C++ types.Control and Looping

Functions in CFunction/method signatures and scope

Page 2: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Course Format

• 10 Sessions (each one and a half hours) over five days.

• Each session consists of a mixture of lectures and guided hands-on work.

2

Page 3: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Editing Text Files• Hopefully everyone is already familiar with at

least one UNIX/linux text editor.• If not, there are several options, including:

– vi / vim– emacs– nano

• Use whichever editor you prefer. • If you aren’t already familiar with one, we will

start with that in the hands-on part of this session.

3

Page 4: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

g++ compiler

• The standard G4MICE installation currently comes with version 3.2.3 or g++.

• In a future release we will migrate to version 4.

• The way that the compiler works is the same in both cases.

4

Page 5: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

helloworld.cc// helloworld.cc

//

// Written for the C++ / G4MICE course

//

// M. Ellis - March 2008

#include <iostream>

int main()

{

std::cout << "Hello World!" << std::endl;

exit(1);

}5

Page 6: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

How to Compile

• g++ -o helloworld helloworld.cc

• This will produce an output executable file “helloworld” (helloworld.exe if you are using cygwin) from the source code helloworld.cc which can be run:

• ./helloworld

6

Page 7: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

helloworld.cc - explanation

7

// helloworld.cc

//

// Written for the C++ / G4MICE course

//

// M. Ellis - March 2008

#include <iostream>

int main()

{

std::cout << "Hello World!" << std::endl;

exit(1);

}

C++ allows two types of comments, this example uses the characters // atthe point in each line wherethe comments begin.

This is a pre-processor command thatTells the compiler to insert code from The file iostream.

Page 8: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

C++ Streams

• C++ streams can be used to send or receive data to/from files, the keyboard, terminal, other processes, devices, ...

• There are two key operators for streams:– <<– >>

• These can be thought of as reading or writing.

8

Page 9: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

cout / cin / cerr• There are 3 variables defined for you that refer

to specific streams that are commonly used:– cout is the “standard output”.– cin is the “standard input”.– cerr is the “standard error”.

• Writing to the cout stream will result in text appearing on the screen.

• Reading from the cin stream will store whatever is typed on the keyboard in some variable(s).

9

Page 10: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

echo.cc// echo.cc

/*

Written for the C++ / G4MICE course

M. Ellis - March 2008

*/

#include <iostream>

#include <string>

int main()

{

std::string message;

std::cout << "Please enter a message for me: ";

std::cin >> message;

std::cout << "Thank you, I have received your message: " << message << std::endl;

exit(1);

}

10

This is an example of the other kindof comment which can cover more than one line.

Additional #include so that we can use strings

Page 11: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

How to Compile

• As with the first example:

• g++ -o echo echo.cc

11

Page 12: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Some C/C++ Variables• int (unsigned int, long int, short int, ... )

– Integers of varying size (number of bits)

• bool – Boolean variable can only be true or false

• char– A single character (8 bits)

• double (float, long double, ... )– Floating point numbers

• string– String of characters 12

Page 13: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Variable Declaration

• <type> <variable name>;

• <type> <variable name> = <value>;

• int count;

• int numPlanes = 3;

• double pi = 3.141592654;

• bool ok;

• std::string name = “MICE”;13

Page 14: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

What are ; and std:: ???• Commands in C/C++ must end with a ;• C/C++ is free format, so you can have as

much or as little white space as you like.• Later on in the course we will look at

namespaces and scope.• For now, it is sufficient to know that a number

of types and variables that we are using are in a namespace called “std”:– std::cout (the standard output stream variable)– std::string (the string type)

14

Page 15: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

// variables.cc

/*

Written for the C++ / G4MICE course

M. Ellis - March 2008

*/

#include <iostream>

#include <string>

int main()

{

int age;

double height;

std::string name;

std::cout << "Please tell me your name: ";

std::cin >> name;

std::cout << "Thank you " << name << ", what is your age? ";

std::cin >> age;

std::cout << "Finally, how tall are you in inches? ";

std::cin >> height;

std::cout << std::endl << std::endl;

std::cout << name << " is " << age << " years old and is " << height << " inches tall" << std::endl;

exit(1);

}

variables.cc

15

Page 16: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Exercises

1. Compile and run helloworld.cc

2. Compile and run echo.cc

3. Compile and run variables.cc

4. Create your own code from scratch, have it use the standard input and standard output and at least 3 variables of different types.

16

Page 17: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Types• Quick reminder:

– int, bool, char, double, float, string, ...• Some types can also be long or short and

some can be unsigned:– unsigned long int x;

• Classes are new types that we can get from elsewhere (e.g. Standard Template Library or G4MICE) or which we can make ourselves.

• string is a class that the STL provides for us.

17

Page 18: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Control and Looping

• We will look at the following:– If– Else– While– For

• C/C++ also provides:– Switch– Case

• Which we will not cover in this course.18

Page 19: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Operators• Mathematical:

+ (addition)

- (subtraction)

* ( multiplication)

/ (division)

% (modulus)

• Logical:& (bitwise AND)

| ( bitwise OR)

! (NOT)

19

Page 20: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Logical Operators• == equal to, this is different from =, which

is the “assignment operator”!)

• != not equal to

• < less than

• > greater than

• <= less than or equal to

• >= greater than or equal to

• && logical AND

• || logical OR

20

Page 21: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Statement Blocks

• In all of the control and looping commands you may wish to have more than one command executed.

• To do this, we put the commands inside a pair of { }

• Any block of code can be collected in this manner (we will come back to this with scope later on).

21

Page 22: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

if Statement

• if( condition ) expression;

• if( condition ) { expression1; expression2; ... expressionN; }

• Note that the if() does not have a semicolon after it, that would be a NULL command:– If( condition );

• This would do nothing.

22

Page 23: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

else

• else can be added as many times as required:

• if( condition 1 ) expression 1;

• else if( condition 2) expression 2;

• ...

• else expression N;

• As before, { } can be used as required.

23

Page 24: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

while

• while will loop over the code so long as the condition is true:

• while( condition ) expression;

• while( condition ) { expression1; expression2; ... expressionN; }

24

Page 25: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

for

• A loop that allows you to set some initial values, specify the condition that must be true for looping to continue and specify code to be executed at the end of each loop:– for( x = 0; x < 10; x = x + 1 ) { ... }

• This will start with x = 0 and execute the code in the {} and then repeat with x = 1, 2, 3, ... up to 9.

25

Page 26: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

++ -- += -=

• Several operators in addition to those we’ve already seen:++x; increment x

--y; decrement y;

x += 5; add 5 to x;

z -= 10; subtract 10 from z;

• These can be useful in loops, e.g.:– for( x = 0; x < 10; ++x )

26

Page 27: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Control and Looping Example

• Download loop.cc, compile it and run it.

• Read through the code and let me know if anything is unclear.

27

Page 28: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Functions• Functions in C are declared by

specifying:– The return type (can be void if nothing is

returned)– The function name– Whether it is const (we will get to this later)– Arguments to the function

• Examples:– void dump();– double absoluteValue( double x );

28

Page 29: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Absolute Value

• The file absolute.cc shows an example of a function that returns the absolute value of the parameter that is passed to it.

• Compile and run this and ensure that you understand how it works.

29

Page 30: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Parameter Passing

• Parameters to functions are only ever passed “by value”.

• That is, a copy of the value of the variable is sent to the function.

• There is a way to pass by reference, we will cover that in the next session.

• For now, you can’t change the value of a variable that is passed to a function.

30

Page 31: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Signatures

• The complete set of information about a function that uniquely identifies it is known as its signature.

• The signature consists of the function name, return type, constness and arguments.

• You can have more than one function with the same name, so long as they have different signatures.

31

Page 32: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

signature.cc

• The file signature.cc shows an example of different functions with the same name, but different signatures.

• Compile and run this and ensure that you understand how it works.

32

Page 33: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

scope

• Functions, variables and types may not be visible from all code.

• We have seen an example of the std namespace, within which variables such as cout and types such as string are defined.

• This is why we refer to them with std:: preceding the variable or type name.

33

Page 34: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

variables inside { }• If a variable is declared inside a { } block,

it is only visible to the code inside that block.

• This means that you can temporarily declare a variable that will only exist while the code in that block is being executed.

• It also means that you can run the risk of having two different variables of the same name at the same time!

34

Page 35: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

scope example

• The file scope.cc shows a range of examples of variables being visible to different fractions of the code.

• Compile and run this and ensure that you understand how it works.

• Note that it will NOT compile at first, you will need to understand why...

35

Page 36: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

Final Exercise

• If you have time, add a function to the now working scope example.

• Experiment to understand whether the variables defined inside main() are visible inside the new function and vice versa.

36

Page 37: C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++

After The Break

• Classes

• Pointers and References

• Makefiles

• Standard Template Library

• Unit testing

• Documentation

• Homework assignment for tomorrow!

37