Professional Documents
Culture Documents
Course Materials:
1. Lecture notes
2. Textbooks:
a. Robert Lafore: Object-Oriented Programming in C++, 4th edition, Sams Publishing, 2002.
b. D.S. Malik: C++ Programming: From Problem Analysis to Program Design, Cengage Learning, 2013
3. Other recommended references:
a. Richard L. Halterman: Fundamentals of C++ Programming, 2015.
b. Deitel, P., and Deitel H.: C++ How To Program, 9th edition, Pearson Education, 2013.
Topics:
1. C++ Programming Basics
2. Decisions and Loops
3. Functions
4. Structs
5. Arrays
6. Pointers
7. Objects, Classes, and inheritance
Student Assessment:
1. Midterm exam (22/3/2018, 20 points).
2. Practical assignments (5 points).
3. Non-credit homework for your practice and we can discuss it in the next lecture.
4. The final exam (75 points).
I recommend that you type all of the examples in every lecture, even
when they are very simple. Keying stuff makes it less likely that you
will forget things later. Don’t be afraid to experiment with the code.
Making mistakes is very educational in programming. The more
mistakes you make early, the more you are likely to learn.
١
Lecture 1:
THE BASIC ELEMENTS OF C++
This lecture explains the basic elements of C++. After completing this
lecture, students become familiar with the basics of C++ and are
ready to write programs that are complicated enough to do some
computations. These programs teach problem-solving skills.
Getting Started
Your Development Environment: this will be explained in the laps.
Compilers take source code and transform it into executable files, which
your computer can run as it does with other programs.
٢
Getting Started (Cont.)
Backslash \
Slash /
Double slash //
Braces {}
Parenthesis ()
Double quotation marks "
Single quotation mark '
Angular brackets <>
Hash #
Colon :
٣
Basic Program Construction
1 // first.cpp
٤
Comments
- They help understand the code by providing explanation about it, and
Comments (Cont.)
Comment Syntax
1. Start with a double slash symbol // and terminate at the end of the line.
(This is one of the exceptions to the rule that the compiler ignores whitespace).
Can start at the beginning of the line or on the same line following a program statement.
Type // without space between them.
EX:
// demonstrates comments // at the beginning of the line
#include <iostream> // following a program statement // preprocessor directive
using namespace std; // “using” directive
int main() // function name “main”
{ // start the function body
cout << “Hello world”; // statement
return 0; // statement
} // end of the function body
٥
Comments (Cont.)
2. Begins with the /* and ends with */ (not with the end of the line).
EX:
/*
This is an old-style comment.
Directives
1. Preprocessor Directives:
is an instruction to the compiler. It starts with #
EX: #include <iostream>
The preprocessor directive #include tells the compiler to insert another file into
your source file. It will be replaced by ﺗﺴﺘﺒﺪل ﺑـthe contents of the indicated file.
٦
Directives (Cont.)
NOTE: A namespace is a part of the program in which certain names are recognized and
are unknown outside of this namespace.
The directive using namespace std; says that all the program statements
that follow are within the std namespace.
EX: cout is declared within this namespace.
If we don’t use the using directive, we will need to add the std many times.
EX: std::cout << "Welcome to C++!";
To avoid adding std:: dozens of times in programs, we use the using directive.
Always start with main( ) // the space inside ( ) does not matter
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. In most C++ programs, main( ) calls member
functions and other standalone functions.
Function name
The parentheses ( ) following the word main are the distinguishing feature
of a function. They’re used to hold function arguments- values passed
from the calling program to the function.
٧
Program Statements
A semicolon ; signals the end of the statement. This is a crucial part of the
syntax but easy to forget.
If you leave the semicolon, the compiler will often (although not always)
signal an error. See the next slide.
The operator << is called the insertion operator. It directs the contents of the
variable on its right to the object on its left (the display).
EX1: cout << “Hello world ” ; // write << without space between them.
In this example, the operator << directs the string constant “Hello world ” to cout
which sends it to the display.
Cascading <<
The insertion operator << can be used repeatedly in the same cout
EX2: cout << “Hello ” << “world” ;
٨
White space
EX:
The previous example can be written this way:
#include <iostream>
using
namespace std;
int main () { cout
<<
“Hello world !”
; return
0;}
There are several exceptions to the rule that white space is invisible to the
compiler:
2. Also, string constants, such as “Every age has a language of its own”,
cannot be broken into separate lines. (If you need a long string
constant, you can insert a backslash (\) at the line break or divide the
string into two separate strings, each surrounded by quotes.)
٩
Common Escape Sequences
The name reflects the fact that the backslash causes an “escape” from the normal way characters are
interpreted. EX: t is interpreted not as the character t but as the tab character.
Escape sequences can be used as separate characters or embedded in string constants.
Escape Name Description
Sequence
\n New line Causes the cursor to go to the next line for subsequent
printing
\t Horizontal tab Causes the cursor to skip to the next tab stop
1 // first.cpp
3 #include <iostream>
1. Load <iostream>
4 using namespace std;
7 cout << "Welcome "; //note the space after Welcome 2.1 Print "Welcome"
8 cout << "to C++!\n";
2.2 Print "to C++!"
9
١٠
1 // first.cpp
3 #include <iostream>
1. Load <iostream>
4
2. main
5 int main()
2.1 Print "Welcome"
6 {
EX:
cout<<“welcome to C++” <<endl; //write endl without “” and it is read end line;
١١
More Useful Programs
Input
The main objective of a C++ program is to perform calculations and manipulate data.
And the data must be loaded into the main memory before it can be manipulated.
Tell it what names to use for each memory location and what type of data to store in this
location. Knowing the location of data is essential because the stored data in this
location might be needed at several places in the program. Knowing what data type is
crucial for performing accurate calculations. It is also critical to know whether your
data needs to remain fixed throughout the program execution or whether it should
change.
١٢
1. instruct the computer to allocate memory (using: a. variables, and b. constants)
a) Variables
– In some programs, data needs to be modified during program execution.
–
– EX: after each test, the average score changes. After each increase, the salary changes.
– A variable is a memory location whose content may change during program execution.
– Must be declared with a name and a data type before they can be used.
These statements which are called declarations must terminate with asemicolon;
You can place variable declarations anywhere in a program. It’s not necessary to
declare variables before the first executable statement. However, it’s more
readable if commonly-used variables are located at the beginning of the
program.
١٣
A variable with a name total and of type int in memory.
Variable names
– Every variable has a name, a type, a size and a value.
– The names given to variables are called identifiers.
Note: if we don’t declare a variable before using it, we’ll receive this error:
error C2065: 'num2' : undeclared identifier
١٤
b) Constants
Some data must stay the same throughout ﺧﻼلa program.
EX: the pay rate for all part-time employees, the passing degree
The const qualifier specifies that the value of a variable will not change
throughout the program.
Any attempt to alter ﺗﻐﻴﺮthe value of a variable defined with this qualifier will
elicit an error message from the compiler.
EX:
const int MIN=60;
const int NO_OF_STUDENTS = 20;
١٥