C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett...
Transcript of C++: An Active Learning Approach Todd Breedlove & Randal Albert Copyright © 2008 Jones and Bartlett...
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++
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
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 (#)
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
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
Two forms• // inline comment• /* block comment */
A block comment can span multiple lines
3.3 Comments
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
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
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
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
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
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
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;}
3.5.1 Namespaces
namespace - allows grouping or structuring related entities inside one category
cout is located in “std” namespace
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;
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
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
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;}