c++: an active learning approach todd breedlove & randal albert copyright © 2008 jones and...

18
C++: An Active Learning Approach C++: An Active Learning Approach Todd Breedlove & Randal Albert Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers Copyright © 2008 Jones and Bartlett Publishers All rights reserved. All rights reserved. Chapter 3 Getting Started with C++

Upload: noel-hart

Post on 26-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

C++: An Active Learning ApproachC++: An Active Learning Approach

Todd Breedlove & Randal AlbertTodd Breedlove & Randal Albert

Copyright © 2008 Jones and Bartlett PublishersCopyright © 2008 Jones and Bartlett PublishersAll rights reserved.All rights reserved.

Chapter 3

Getting Started with C++

Page 2: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.1 C++ BasicsC++ is case sensitive

Average is different than average and AVERAGE

All C++ reserved words are in lowercase

Reserved words - identifiers that have special meaning and are a part of the language

Page 3: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.1 C++ Basics

Most statements terminated by a semicolon except: • Function headers• Preprocessor directives• Control statements

Preprocessor directives - commands executed by the preprocessor

Preprocessor directives require the first character on the line to be a number sign (#)

Page 4: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

Whitespace - empty or non-visible character (i.e., space or tab), including blank lines• Aids readability

• Readability allows a program to be more easily modified and maintained

• Most whitespace is ignored by the compiler

3.2 Whitespace

Page 5: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

Comments - lines of C++ code that are ignored by the compiler• Used to document the source code for you or

for other programmers

• Aid in the readability and maintainability of source code

3.3 Comments

Page 6: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

Two forms• // inline comment• /* block comment */

A block comment can span multiple lines

3.3 Comments

Page 7: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

Function - a group of related statements that perform a specific task or job• The main function (commonly referred to as

“main”) is the starting point of a C/C++ program

• Every C/C++ program will have exactly one main

3.4 The main Function

Page 8: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.4 The main Function

Function header - first line in a function definition

Function definition - combination of the function header and a function body - the statements enclosed in curly braces

int main() // Function header, no semicolon{// Start of function body return 0; // C++ statement, requires semicolon}// End of function body

Page 9: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.4 The main Function

Curly braces and parentheses always come in pairs

Curly braces:• Group statements together • Define the beginning and ending of the

function body

Page 10: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5 The #include Preprocessor Directive

Predefined routine - not part of the core language but an extension to the language• Part of the C++ standard

• Accessed through header files

• Preprocessor directive #include allows access to predefined routines in external or separate files - called header files

Page 11: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5 The #include Preprocessor Directive

Two forms of the #include:1.#include <header_file>

2.#include “header_file”

• First example - looks in the “include” directory specified by the compiler

• Used with predefined header files

Page 12: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5 The #include Preprocessor Directive

Two forms of the #include:1.#include <header_file>

2.#include “header_file”

• Example 2 - looks for the header file in a user specified location

• Used with user defined header files

Page 13: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5 The #include Preprocessor Directive

The <iostream> header file contains predefined input and output routines

#include <iostream> // Allows access to I/O routines

int main(){ // Outputs "Hello World!" to the screen std::cout << "Hello World!";

return 0;}

Page 14: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5.1 Namespaces

namespace - allows grouping or structuring related entities inside one category

cout is located in “std” namespace

Page 15: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5.1 Namespaces

Ways to access routines in namespaces:1. Explicitly

std::cout << “Hello World”;

2. Using directive using namespace std;

3. Using declaration using std::cout;

Page 16: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.5.1 Namespaces

Explicit requires use of the namespace prefix every time a routine is used

Using directive allows access to all routines within a namespace

Using declaration allows access to only those routines specified

Page 17: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.7 C – The Differences

C programmers are limited to the block style comment (/* */) with older compilers

Many new C compilers allow the use of the inline comment (//) which has become part of the latest C standard

Page 18: C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett Publishers All rights reserved. Chapter 3 Getting Started

3.7 C – The Differences

C uses different header files and routines for I/O

Namespaces are not required to access predefined functions

#include <stdio.h> // Allows access to I/O routines

int main( void ) { // Outputs "Hello World!" to the screen printf( "Hello World!" ); return 0;}