37
Chapter 0 Getting Started

Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Embed Size (px)

Citation preview

Page 1: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Chapter 0

Getting Started

Page 2: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Objectives

• Understand the basic structure of a C++ program including:– Comments– Preprocessor instructions– Main function– Return value

• Get acquainted with the development environment.• Compile and run a simple C++ program.• Become acquainted with expressions, operators and

scope.

Page 3: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

C++

• "Writing in C or C++ is like running a chain saw with all the safety guards removed,"    — Bob Gray

• "In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg."    — Bjarne Stroustrup

Page 4: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Hello, World!

• Consider the following C++ program:

// A small C++ program

#include <iostream>

int main()

{

std::cout << "Hello, world!" << std:: endl;

return 0;

}

Page 5: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Free Form

• C++ is a free form language (with a few exceptions).– All white space characters are treated the same (spaces,

tabs, returns, etc.).– Program lines can be any number of lines on the screen.– Several program lines can be placed on a single screen

line.– Indenting is not required

• The responsibility to create readable code lies with the programmer!

Page 6: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Comments

• The // characters begin a comment. Anything on a line after these characters will be ignored by the complier.

• Why include comments?– Indicated author and assignment (required in

this course)– Document tricky or confusing code– Describe modules (functions & classes) and

their interfaces

Page 7: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

#include

• In C++ the core language doesn’t include many important features.– Input and output– Math functions– String manipulation– etc.

• These features are brought as header files in using #include directives.

Page 8: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

#include

• #include directives normally appear at the beginning of a program in what is called the preamble.

• We request the use of stream input and output using the line

#include <iostream>• The <> symbols mean the C++ should look for

this file in a part of the library called the standard header.

Page 9: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

main function

• A function is a piece of code that has a name and can be called from another part of the program.

• Every C++ program must have a main function.• This function is called by the operating system when

the program is run.• This function must produce and integer return value

that tells the operating system if it terminated successfully. – 0 mean success– Any other value means there was a problem

Page 10: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

main function

• Every function can return at most one variable and its type must be specified.

• The operating system can also pass pass parameters to main when the program is run.

• Our program does not need any parameters but we still need parentheses to show where they would go.

int main()

Page 11: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Curly braces

• The main function consists of a sequence of statements.

• Curly braces {} are used indicate where the statements that belong to main begin and end.

• In general braces tell C++ to treat everything between them as a single unit.

Page 12: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Stream Output

• The program needs to tell the operating system to print “Hello, world!” but it needs to specify where and how.

• Possible options include– A terminal window – Some other graphics window– A file– etc.

Page 13: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Stream Output

• The name std::cout refers to the standard output stream.

• This output is system dependent but will usually print in a text window associated with the program.

• std:: indicates this is part of the standard namespace.

• A namespace is a collection of related names.

Page 14: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Stream Output

• Information is sent to the output stream using the output operator <<.

• std::endl ends the current line of output by inserting a newline character at the end.

• Putting this all together: std::cout << "Hello, world!" << std::endl;

Page 15: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Semicolon

• In C++ all statements can use multiple lines and end with a semicolon. std::cout << "Hello, world!" << std::endl;

Page 16: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Return statement

• A return statement ends the execution of a function and passes the return value back to the location from which the function was called.

• When main began it said it would return an int so there must be a return with an int return value.

• Returning 0 indicates successful completion.return 0;

Page 17: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Return statement

• "... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs."

— Robert Firth

Page 18: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Expressions

• An expression is a combination of commands and values that can be evaluated to produce a result.

3+7• C++ often uses expressions in surprising places.

std::cout << “Hello, world!”• This evaluates to be std::cout.• This is the mechanism that allows us to print

several items on the same line.

Page 19: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Expressions

std::cout << "Hello, world!" << std::endl;• The operator << has a side effect. It not only

evaluates to a value. It also modifies the standard output stream (prints “Hello, world!” to the screen).

• When it is evaluated it is replaced by its value.

std::cout << std::endl;• Next, this expression is evaluated.

Page 20: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Operators

• Operators are used in expressions to indicate how they are to be evaluated.

• They may combine 1 (unary), 2 (binary) or 3 (ternary) different values (called operands).

• The usual mathematical operations are operators+, -, *, /

• C++ uses many more operators to do different jobs.– Output operator – <<– Scope resolution operator – ::

Page 21: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Operators

• Some operators have side effects like <<.• Others do not

3+7• Operands are not modified here, the

expression is simply evaluated and replaced with its value.

10

Page 22: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Operators

• Operators also have specific types of operands they can work on.

• + requires two numeric values (int, float, etc.)

• << requires operands of two different types– The one on the left must be an output stream

(called an ostream).– The one on the right must be a string.

Page 23: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Scope

• The scope of a variable is the part of the program where the variable has meaning.

• There are several different types of scope.– Block scope (indicated by braces {})– Namespace– Global– etc.

Page 24: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Scope

• When we use names from the standard library we must indicate this using the scope operator – ::.

• This operator expects a left operand that is a namespace and a right operand which is an identifier (name).

• The result is a qualified name.• std::cout means the cout in the namespace

std.

Page 25: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Control Characters

• The following can be inserted into a string to format the output.\n newline character\t tab character\b backspace character\” includes a “ in the string\\ includes a single \ in the string

Page 26: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Development Environments

• There are many options for C++ compilers and development environments.– Command line (Macintosh, Windows, Linux)

• cc• gcc

– Windows • Visual C++• DevC++

– Macintosh• Xcode

Page 27: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

• In this lecture we will cover compiling your code in DevC++.

• DevC++ should be available on CN computers.

• To install it on your personal machine go to the site http://www.bloodshed.net/devcpp.html and get the latest version (avoid beta versions)

Page 28: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

• Run DevC++– The first time it may ask about setting up some

tables. This is not necessary but it will make everything faster if you do.

• From the File menu select New Source File.– You could also open an existing file.

• Type your code in the resulting window.

Page 29: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

Page 30: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

• You will need to save your file before you can compile it.– The name should end with the extension .cpp

hello.cpp– This is true for all environments and indicates

this is a C++ program.

Page 31: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

Page 32: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

• From the Execute menu select Compile and Run.– You can separate these with Compile followed

by Run.

• You may see a black window flash quickly in and out of existence.– This is because the window closes once the

program terminates.

Page 33: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

• To avoid this problem, insert the line system(“pause”);

• This should come immediately before the line return 0;.

• It will force the window to wait for the user to press any key before closing.

• This way you get to admire your work before the window closes.

Page 34: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

Page 35: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

DevC++

• Now you can compile and run the program again to see the output.

Page 36: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

system(“pause”)

• Pressing any key will close the window.• Using system(“pause”) is not necessary if

you run your program from a command line.• In general system commands should be

avoided.– Slow– System dependent (Windows only)– Not secure (virus protection software may

complain)

Page 37: Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function

Homework Chapter 0 (page 7)

• Hint, errors can be subtle. Type these in and see what happens. (25 pts total possible)– 0-0– 0-2 (email, 5 pts)– 0-5 (paper, 5 pts)– 0-6 (paper, 5 pts)– 0-7 (paper, 5 pts)– 0-8 (paper, 5 pts)– 0-9 (paper, 5 pts)