Intro to C++
Chapter 8
Objectives
Examine a Sample C++ Program Compiling a C++ Program Program Development Process
Introduction C++
Sample C++ Program: Examine Program Attributes
Basic Concept: Divide and Conquer: How ? Functions
Definition Assigns a unique Name to a set of instructions (Statements) Call When you use the function name, the set of instructions associated with the Name are executed
Introduction C++
Sample C++ Program: Functions Definition Format
Specifies returned value type
Mandatory Space
Specifies unique Name
Optional for passing data
return value type function name (argument list) { Function body // C++ statements }
Did you notice: The left and right parenthesis The braces are on separate lines
The indentations of the C++ Statements
Set of instructions
We will look at functions in detail later
Introduction C++ 4
Sample C++ Program: Empty Main Function
Function returns a value of type integer
Function Name
int main ( ) {
Introduction C++
A Sample C++ Program Hello World Program
/* Lets greet the world. */ #include <iostream> using namespace std; int main ( ) { // preprocessor instruction
//Output Hello World! cout << Hello World!\n; //Send 0 back to OS indicating success return 0; }
Introduction C++
Explanation of Code The Hello World Program
/* Lets greet the world. */ // preprocessor instruction When Comment spans multiple lines
Single Line Comments
Comments are not executed
Introduction C++
Explanation of Code The Hello World Program #include <iostream>
#include Directives tell compiler where to find info about items used in the program [Note: NO space between # and include] #include <iostream> directs the processor to make cout (see-out), cin (see in) and other programs in iostream library available to your program
using namespace std;
Tells the compiler to use names in iostream in a standard way
Introduction C++
Explanation of Code The Hello World Program
main ( )
A Special function name It indicates where program execution starts The OS calls the function main ( ) to start the C++ program
int main( )
The function main( ) returns a value of type integer
Introduction C++
Explanation of Code The Hello World Program Statements
std: : cout << Hello World!\n; std:: look in the namespace std for the name that follows cout << outputs to the monitor the text string Hello World! followed by a newline Think of cout as a name (Identifier) for the monitor
; (semicolon) Program statements must
end with a semicolon
Introduction C++
10
Running a C++ Program
C++ source code is written with an editor
vi Emacs Ultraedit
The Compiler converts your source code to Object code The Linker combines all the object code into an executable program
Introduction C++
11
Compiling C++ Programs Command Line Compiler, g++
Compiler Commands
g++ file.cpp
Comments
Compiles the file file.cpp and creates an executable named a.out Displays the version of g++
g++ -- version
g++ file.cpp o file2
Compiles the file file.cpp, creates an executable, and names the executable file2 instead of a.out
12
Introduction C++
Compiling C++ Programs Error Messages
Error
Syntax Error
Description
The rules of the C++ language were not followed if i=< 100 should be If (i <= 100) Wrong algorithm
Logical Error
Runtime Error
Program terminates abruptly: Div by 0; illegal memory access
13
Introduction C++
Compiling and Running your C++ Program
C++ Program Source Code
Data for C++ Program
Compiler
editor
Keyboard, file
Computer
compile command
Object Code for your Program
Other Object Codes
linker
Computer (Complete Object Code)
Run
Output of C++ Program
Introduction C++ 14
Compiling and Running Your C++ Program
Compile the code Fix errors the Compiler indicates and recompile code Run the program again
Test and re-run
Introduction C++
15
Program Development Phase
Step #1: Problem-Solving Phase
Define Problem
Complex problem ?
Adopt Problem Decomposition principle
Design Algorithm [Iterative process]
Just express your initial thoughts [on paper], youll refine them later: Draw/Sketch charts Draw/Sketch Pictures (worth a thousand words!) Narratives Complex Algorithm? Adopt Algorithm Decomposition Principle Psuedocode Verify that algorithm solves the problem in its entirety
Introduction C++ 16
Program Development Process
Step #2: Implementation Phase
Translate Algorithm to C++
START writing your C++ Code
Unit Test
Test individual functions for accuracy Fix errors
System Test
Test the entire program Fix errors
Introduction C++
17
Program Development Process
Problem-solving Phase Start
Problem Definition Translate to C++
Implementation Phase
Algorithm Design
Verify Algorithm (Paper Testing)
Testing
Working Program
Introduction C++ 18
Another Sample C++ Code
#include <iostream> using namespace std;
int main( ) {
int number_of_pods, peas_per_pod, total_peas;
cout << Press return after entering a number. \n; cout << Enter the number of pods:\n; cin >> number_of_pods; cout << Enter the number of peas in a pod:\n; cin>> peas_per_pod; total_peas = number_of_pods * peas_per_pod; cout << If you have ; cout << number_of_pods; cout << pea pods\n; cout << and; cout << peas_per_pod; cout << peas in each pod, then\n; cout << you have ; cout << total_peas; cout << peas in all the pods.\n; return 0; }
Introduction C++ 19
Explanation of Sample Code
variables, cin
#include <iostream> using namespace std;
int main( ) {
Variables:
number_pods, peas_per_pod, total_peas
int number_of_pods, peas_per_pod, total_peas;
cout << Press return after entering a number. \n; cout << Enter the number of pods:\n;
cin >> number_of_pods;
cout << Enter the number of peas in a pod:\n;
cin>> peas_per_pod;
total_peas = number_of_pods * peas_per_pod; cout << If you have ; cout << number_of_pods; cout << pea pods\n; cout << and; cout << peas_per_pod; cout << peas in each pod, then\n; cout << you have ; cout << total_peas; cout << peas in all the pods.\n; return 0;
Must be Declared before they are used Identified by Names (Identifier) Special rules for Identifiers
Input Statement:
cin
Tells the computer how to handle info entered from keyboard
Introduction C++
20
Explanation of Sample Code Multiplication, Assignment
#include <iostream> using namespace std;
int main( ) {
int number_of_pods, peas_per_pod, total_peas; cout << Press return after entering a number. \n; cout << Enter the number of pods:\n; cin >> number_of_pods; cout << Enter the number of peas in a pod:\n; cin>> peas_per_pod;
Multiplication Operator:
Assignment Statement:
total_peas = number_of_pods * peas_per_pod;
cout << If you have ;
cout << number_of_pods; cout << pea pods\n; cout << and; cout << peas_per_pod; cout << peas in each pod, then\n; cout << you have ; cout << total_peas; cout << peas in all the pods.\n; return 0;
=
Assign value on the rhs of
=
to Variable on the lhs of
We will talk more about variables & Operators shortly
Introduction C++ 21
Sample C++ Code #2 Sample Dialogue
#include <iostream> using namespace std;
int main( ) {
int number_of_pods, peas_per_pod, total_peas;
cout << Press return after entering a number. \n; cout << Enter the number of pods:\n; cin >> number_of_pods; cout << Enter the number of peas in a pod:\n; cin>> peas_per_pod; total_peas = number_of_pods * peas_per_pod; cout << If you have ; cout << number_of_pods; cout << pea pods\n; cout << and; cout << peas_per_pod; cout << peas in each pod, then\n; cout << you have ; cout << total_peas; cout << peas in all the pods.\n; return 0; }
Introduction C++ 22
Sample Dialogue
Press return after entering a number Enter the number of pods 10 Enter the number of peas in a pod 9 If you have 10 pea pods and 9 peas in each pod, then you have 90 peas in all the pods
Layout of a Simple C++ Program
#include <iostream> using namespace std; int main ( ) { Variable Declarations Statement #1 Statement #2
Last Statement return 0; }
Introduction C++ 23