Cplus
Cplus
Chapter 8
Objectives
Examine a Sample C++ Program Compiling a C++ Program Program Development Process
Introduction C++
Introduction C++
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
Function Name
int main ( ) {
Introduction C++
/* 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++
Introduction C++
Introduction C++
main ( )
A Special function name It indicates where program execution starts The OS calls the function main ( ) to start the C++ program
int main( )
Introduction C++
Introduction C++
10
The Compiler converts your source code to Object code The Linker combines all the object code into an executable program
Introduction C++
11
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
Compiles the file file.cpp, creates an executable, and names the executable file2 instead of a.out
12
Introduction C++
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
Introduction C++
Compiler
editor
Keyboard, file
Computer
compile command
Object Code for your Program
linker
Computer (Complete Object Code)
Run
Output of C++ Program
Introduction C++ 14
Compile the code Fix errors the Compiler indicates and recompile code Run the program again
Test and re-run
Introduction C++
15
Define Problem
Complex problem ?
Adopt Problem Decomposition principle
Unit Test
Test individual functions for accuracy Fix errors
System Test
Test the entire program Fix errors
Introduction C++
17
Implementation Phase
Algorithm Design
Testing
Working Program
Introduction C++ 18
variables, cin
#include <iostream> using namespace std;
int main( ) {
Variables:
number_pods, peas_per_pod, total_peas
cout << Press return after entering a number. \n; cout << Enter the number of pods:\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
Multiplication Operator:
Assignment Statement:
=
Assign value on the rhs of
=
to Variable on the lhs of
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