Chapter 2 (Part 1)
Chapter 2 (Part 1)
Chapter 2 (Part 1)
1
Contents
Objectives
Learning Outcomes
Introduction of C++
Basic Structure of C++ Program
Whitespace
Escape Sequence
The endl manipulator
Comments
Exercise
Summary
References
2
Objectives
3
Learning Outcomes
4
Introduction of C++
C++ is a compiler
Compiler take source code and transform it into executable
files.
Source files are text files(extension .CPP) file.
5
Introduction of C++
6
Basic structure of C++ program
//Welcome.cpp C++ Program
CPU
Output
7
About Basic Program
Line 1, Start with a number sign ‘#’ is called preprocessor
directive. The <iostream.h> is called a header file.
Line 2 is Using Directive.
Line 3 is a function called main( ). The int is the return value
of function main().
Line 5 and 6 are the program statements.
Line 7 is the return statement.
Line 4 is the begin of the function main() and line 8 is the end
of the function main().
8
Preprocessor Directives
9
Header Files
Many functions and symbols needed to run a C++ program are
provided as collection of libraries.
Every library has a name and is referred to by a header file.
Each header file contains file access, mathematical computations,
and data conversion, among other its information for a particular
group of library functions.
The preprocessor directives #include tells the compiler to add the
source file iostream.h to the source file before compiling.
iostream.h is an example of a header file, and also called include
file.
It’s concerned with basic input/output operations.
10
Using Directive
Using Directive allows all the names in a namespace to be used.
Namespace is a declarative region that provides a scope to the
identifiers (the names of types, functions, variables, etc) inside it.
Namespace a part of the program in which certain names are
recognized; outside of namespace they’re unknown.
Example: using namespace std;
If you do not use the using directive,
std :: cout<< “Hi! Students” ;
11
Functions
12
Always Start with main()
When you run a C++ program, the first statement executed will
be at the beginning of a function called main().
The program may consist of many functions, classes, and other
program elements, but on startup, control always goes to main().
If there is no function called main() in your program, an error
will be reported when you run the program.
13
Getting Started
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
14
Output statement
15
Whitespace
Consider the program: #include <iostream>
using namespace std;
int main ()
{ cout
<<
"Hello! Welcome to UCSY"
; return
0;}
16
Example Program
17
Escape Sequences
The backslash causes an “escape” “\’” from the normal way
characters are interpreted.
18
Escape Sequences (Cont’d)
19
Escape Sequences (Cont’d)
#include <iostream>
using namespace std;
int main()
{
cout<<"My name is \"Cherry Phue\".\nMy Registeration
number is \'7777\'.";
return 0;
}
20
The endl manipulator
21
The endl manipulator
#include <iostream>
using namespace std;
int main()
{
cout<<"My name is \"Cherry Phue\"."<<endl<<"My
Registeration number is \'7777\'.";
return 0;
}
22
Cascading
23
Comments
Comments are an important part of any program.
Comments help the person writing a program and anyone else
who must read the source file, understand what’s going on.
Compiler ignores comments, so they do not add the file size or
execution time of the executable program.
24
Comments syntax
C++ has two comments style:
1. Comments start with a double slash symbol(//) and terminate
at the end of the line.
// single line comment (preferred)
25
When to use Comments
Use comments to
Explain to the person looking at the listing what you’re
trying to do.
Example,
Identify program
who wrote it
Record when program was written
Add descriptions of modifications
26
Comments (Example)
// comments.cpp
// This program demonstrates comments
#include <iostream> //preprocessor directive
using namespace std; // using directive
int main() //function name “main”
{ //start function body
cout << “Every age has a language of its own\n”; //statement
27
Exercise
1. Write a C++ program that will display the following welcome
message on the screen.
28
Exercises
3. Type the following codes and compile and run it. What output will
be display?
cout<<“Listen to the beep now.\a”;
cout<<“\n Where is the \’t\’ in cat\b?\n\n”;
cout<<“I earned $50 \r where is the money?\n”;
cout<<“The rabbit jumps \t\t two tabs.\n\n”;
29
Summary
How to write new program with basic structure
How to save the program
How to open the specific program
Whitespace
Escape Sequence
The endl manipulator
Comments
30
References
Farrell
5. C Programming for Engineering & Computer Science by H.H. Tan and T.B.
D’Orazio
31
Thank You!
32