C++ Programming Language 11/20/2022
Introduction to C++
C++ Programming Language
Programming Language
Dr Sawsan M. Mahmoud
Computer Engineering
College of Engineering / Mustansiriyah University
2022-2023
1
Sunday, November 20, 2022
Contact
Sunday, November 20, 2022
Name: Sawsan Mous Mahmoud
Email:
[email protected] Room: Room in Computer Engineering
Department, 2nd Floor.
Lecture Time : Sunday 08 AM – 11:30 AM,
C++ Programming Language
Monday 08 AM – 11:30 AM, and Wednesday
02:30 PM – 05:30 PM.
Notes: Other office hours are available by an
appointment.
The contents of this syllabus is not to be
2
changed during the current semester.
1
C++ Programming Language 11/20/2022
What is all about Modules
This course introduces the fundamental of the C++
Sunday, November 20, 2022
language and the basic data structures used in computer
software so:
• Understand the data structures
• Analyse the algorithms that use them
• Know when to apply them
Also, practice design and analysis of data structures and
C++ Programming Language
practice using these data structures by writing programs.
On successful completion of this course, you will
understand:
• What the tools are for storing and processing common data
types
• Which tools are appropriate
So that you will be able to make good design choices as a 3
developer, project manager, or system customer
What is this Module all about?
Introduction C++ programming language
Sunday, November 20, 2022
Declaration, Comments, Numbers, Variables, I/O data (cout,
cin).
Expressions and Assignments, Increment and Decrement
Operator, Combined Operator.
Selection and Switch, If statement Nested If, If else, Switch
Repetition statement, While, Do While For Nested For
C++ Programming Language
statement.
One dimensional array I.
One dimensional array II.
Two dimensional array I.
Two dimensional array II.
Struct I
4
Struct II
2
C++ Programming Language 11/20/2022
What is this Module all about?
Sunday, November 20, 2022
Functions, Passing Parameters to Functions
Stack (Insertion, Deletion)
Evaluation and Translation of Expression. I
Evaluation and Translation of Expression. II
Queue (Insertion, Deletion). I
C++ Programming Language
Queue (Insertion, Deletion). II
Circular Queue
Linked List, Linear Structure (Insertion). I
Linked List, Linear Structure (Insertion). II
Introduction to Trees and its operations
Introduction to binary Search Tree and its operations
5
Module Resources
Book
Sunday, November 20, 2022
• Richard L. Halterman (2017), Fundamentals of
Programming C++.
• Ulla Kirch-Prinz, Peter Prinz, A Complete Guide to
Programming in C++, 2002.
Suggested references
• Clifford A. Shaffer, Virginia Tech (2011), Data
C++ Programming Language
Structures and Algorithm Analysis.
• James F. Korsh,Leonard J.Garrett (2008), Data
Structures, Algorithms, and Program style using(c),
• Textbook. Michael T. Goodrich, Roberto Tamassia.
David M. Mount (2007), Data Structures and
Algorithms in C++.
6
3
C++ Programming Language 11/20/2022
Assessment C++ Programming
Language
Sunday, November 20, 2022
Assessment of Learning: The student will be evaluated
based on homework, exams, and class participation.
Examination Information: The exams will cover material
that has been discussed in class or covered in the books.
Assignments: Homework must be submitted at the
C++ Programming Language
beginning of class on the date which it is due. Late
homework will be accepted only when special circumstances
are approved by the instructor.
Terms examination: 40%
• Term Exams
• Assignments
Final Exam: 60% 7
Lecture one
Introduction to C++ Programming
C++ Programming Language
Language
Sunday, November 20, 2022
4
C++ Programming Language 11/20/2022
Computer and Development Tools
A computer program is a sequence of
Sunday, November 20, 2022
instructions that dictate the flow of
electrical impulses within a computer
system.
These impulses affect the computer’s
C++ Programming Language
memory and interact with the display
screen, keyboard, mouse, and perhaps
even other computers across a network.
in such a way as to permit humans to
perform useful tasks, solve high-level
9
problems, and play games.
Computer and Development Tools
Sunday, November 20, 2022
Software must be stored in the computer’s
memory. These patterns of electronic symbols
are best represented as a sequence of zeroes
and ones, digits from the binary (base 2) number
system. An example of a binary program
sequence is:
C++ Programming Language
10001011011000010001000001001110
Software can be represented by printed words
and symbols that are easier for humans to
manage than binary sequences.
Tools exist that automatically convert a higher-
level description of what is to be done into the 10
required lower-level code.
5
C++ Programming Language 11/20/2022
Computer and Development Tools
Sunday, November 20, 2022
Higher-level programming languages like C++ allow
programmers to express solutions to programming
problems in terms that are much closer to a natural
language like English.
Some examples of the more popular of the hundreds of
higher-level programming languages that have been
C++ Programming Language
devised over the past 60 years include FORTRAN,
COBOL, Lisp, Haskell, C, Perl, Python, Java, and C#.
A C++ compiler is used to translate the C++ code into
machine code. The higher-level language code is called
source code. The compiled machine language code is
called the target code. The compiler translates the
source code into the target machine language. 11
Computer and Development Tools
An editor allows the user to enter the program source
Sunday, November 20, 2022
code and save it to files. Most programming editors
increase programmer productivity by using colors to
highlight language features.
The syntax of a language refers to the way pieces of
the language are arranged to make well-formed
C++ Programming Language
sentences.
Programmers must follow strict syntax rules to create
well-formed computer programs.
Only well-formed programs are acceptable and can be
compiled and executed.
Some syntax-aware editors can use colors or other
special annotations to alert programmers of syntax 12
errors before the program is compiled.
6
C++ Programming Language 11/20/2022
Computer and Development Tools
Sunday, November 20, 2022
C++ Programming Language
13
The Task of Programming
Sunday, November 20, 2022
Machine language: language that computers can
understand; it consists of 1s and 0s
Interpreter: program that translates programming
language instructions one line at a time
C++ Programming Language
Compiler: translates entire program at one time
Run a program by issuing a command to execute the
program statements
Test a program by using sample data to determine
whether the program results are correct
14
7
C++ Programming Language 11/20/2022
What is C++?
The C++ programming language
Sunday, November 20, 2022
was created by Bjarne Stroustrup
and his team at Bell Laboratories
(AT&T, USA)
The earliest versions, which were
originally referred to as “C with
classes,” date back to 1980. As the
C++ Programming Language
name C++ implies, C++ was derived
from the C programming language:
++ is the increment operator in C.
As early as 1989 an ANSI
Committee (American National Bjarne Stroustrup is
Standards Institute) was founded to a computer scientist, most
notable for the invention and
standardize the C++ programming development off 15
language. the C++ programming
language.
DEVELOPING A C++ PROGRAM
The following three steps are required to create and
translate a C++ program:
1. First, a text editor is used to save the C++ program in a
text file. In other words, the source code is saved to a
source file. In larger projects the programmer will normally
use modular programming. This means that the source
code will be stored in several source files that are edited
Data Structure
and translated separately.
2. The source file is put through a compiler for translation. If
everything works as planned, an object file made up of
machine code is created. The object file is also referred to
as a module.
3. Finally, the linker combines the object file with other
modules to form an executable file. These further
modules contain functions from standard libraries or 16
parts of the program that have been compiled previously.
Sunday, November 20, 2022
8
C++ Programming Language 11/20/2022
DEVELOPING A C++ PROGRAM
Translating a C++ program
17
Sunday, November 20, 2022
C++ Programming Language
A BEGINNER’S C++ PROGRAM
■ Sample program
#include <iostream>
using namespace std;
int main()
{
C++ Programming Language
cout << "Enjoy yourself with C++!" << endl;
return 0;
}
Screen output
Enjoy yourself with C++!
18
Sunday, November 20, 2022
9
C++ Programming Language 11/20/2022
Structure of function main()
C++ Programming Language
19
Sunday, November 20, 2022
C++ Program
The first line begins with the number
symbol, #, which indicates that the line is
intended for the pre-processor. The pre-
processor is just one step in the first
translation phase and no object code is created
at this time.
C++ Programming Language
You can type #include <filename> to have the
pre-processor copy the quoted file to this
position in the source code.
This allows the program access to all the
information contained in the header file. The
header file iostream comprises conventions 20
for input and output streams.
Sunday, November 20, 2022
10
C++ Programming Language 11/20/2022
C++ Program
The word stream indicates that the information
involved will be treated as a flow of data.
Predefined names in C++ are to be found in the
std (standard) namespace.
The using directive allows direct access to the names
C++ Programming Language
of the std namespace.
Program execution begins with the first instruction in
function main(), and this is why each C++ program
must have a main function.
This function’s structure is not different from that of any
other C++ function.
21
Sunday, November 20, 2022
C++ Program
In our example the function main() contains two statements. The
first statement cout << "Enjoy yourself with C++!" << endl;
outputs the text string Enjoy yourself with C++! on the screen.
The name cout (console output) designates an object
responsible for output.
The two less-than symbols, <<, indicate that characters are
C++ Programming Language
being “pushed” to the output stream.
Finally endl (end of line) causes a line feed.
The statement return 0; terminates the function main() and also
the program, returning a value of 0 as an exit code to the calling
program. It is standard practice to use the exit code 0 to indicate
that a program has terminated correctly.
Note that statements are followed by a semicolon. By the way,
the shortest statement comprises only a semicolon and does 22
nothing.
Sunday, November 20, 2022
11
C++ Programming Language 11/20/2022
C++ Program
C++ is case sensitive. All commands in C
must be lowercase.
C++ has a free-form line structure.
End of each statement must be marked with
Data Structure
a semicolon. Multiple statements can be on
the same line.
White space is ignored. Statements can
continue over many lines.
23
Sunday, November 20, 2022
Comments
This example also introduces comments. Strings
enclosed in /* . . . */ or starting with // are interpreted as
comments.
EXAMPLES:
/* I can cover
several lines */
// I can cover just one line
Data Structure
In single-line comments the compiler ignores any
characters following the // signs up to the end of the
line.
Comments that cover several lines are useful when
troubleshooting, as you can use them to mask
complete sections of your program.
Both comment types can be used to comment out the 24
other type.
Sunday, November 20, 2022
12
C++ Programming Language 11/20/2022
Exercise (1)
Write a C++ program that outputs the following
text on screen:
Oh what
a happy day!
Oh yes,
what a happy day!
Data Structure
25
Sunday, November 20, 2022
Exercise (2)
The following program contains several errors:
*/ Now you should not forget your glasses //
#include <stream>
int main
{
cout << "If this text",
Data Structure
cout >> " appears on your display, ";
cout << " endl;"
cout << 'you can pat yourself on '
<< " the back!" << endl.
return 0;
)
Resolve the errors and run the program to test your
changes. 26
Sunday, November 20, 2022
13
C++ Programming Language 11/20/2022
Exercise (3)
What does the C++ program output on screen?
#include <iostream>
using namespace std;
int main(){
cout << endl << "Dear reader, "
<< endl << "have a ";
Data Structure
cout << “break!" << endl;
return 0;}
27
Sunday, November 20, 2022
Solution (1)
Exercise 1
#include <iostream>
using namespace std;
int main()
{
cout << " Oh what " << endl;
Data Structure
cout << " a happy day! " << endl;
cout << " Oh yes, " << endl;
cout << " what a happy day! " << endl;
return 0;
}
28
Sunday, November 20, 2022
14
C++ Programming Language 11/20/2022
Solution (2)
Exercise 2
The corrected places are underlined.
Data Structure
29
Sunday, November 20, 2022
Solution (3)
Exercise 3
The screen output begins on a new line:
Dear reader,
have a BREAK!
Data Structure
30
Sunday, November 20, 2022
15
C++ Programming Language 11/20/2022
Thank you
Data Structure
31
Sunday, November 20, 2022
16