Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function."— Presentation transcript:

1 Chapter 0 Getting Started

2 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.

3 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

4 Hello, World! Consider the following C++ program: // A small C++ program #include int main() { std::cout << "Hello, world!" << std:: endl; return 0; }

5 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!

6 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

7 #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.

8 #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 The <> symbols mean the C++ should look for this file in a part of the library called the standard header.

9 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

10 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()

11 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.

12 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.

13 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.

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

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

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

17 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

18 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.

19 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.

20 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 – ::

21 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

22 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.

23 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.

24 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.

25 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

26 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

27 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) http://www.bloodshed.net/devcpp.html

28 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.

29 DevC++

30 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.

31 DevC++

32 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.

33 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.

34 DevC++

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

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

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


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

Similar presentations


Ads by Google