lecture 1 ok
TRANSCRIPT
-
8/8/2019 Lecture 1 OK
1/18
Prof. Jihad Mohamd ALJA'AM 1
Computer Programming Eng. 106
Wednesday September 22, 2010
Objective:Introduction to C++Arithmetic Operators
Input and Output, Variables and Comments
Integer Division and Modulus
Internal Coding of Integer Numbers
Definition 1: A computer program is a step-by-stepdescriptions of the solution to a problem. It is written in
C++ code (i.e., C++ statements).
Definition 2: C++ is a programming language containing
a set ofinstructions called statements. These statements
are understandable by the computer machine.
Definition 3: The computer uses its memory calledRAM
(Random Access Memory), to store the program.
Definition 4: The computer uses its brain calledprocessor(CPU) to perform the C++ statements.
Definition 5: The C++ program has a well definedstructure as follows:1 #include 2 void main()3 {
4 Statements;5 }
This C++ program is written by you using the keyboard.It will be stored into a text file and has a given name like
program1.cpp orProgHello.cpp
The C++ Program
Structure
-
8/8/2019 Lecture 1 OK
2/18
Prof. Jihad Mohamd ALJA'AM 2
Rules of Thumbs
1.Keyboard (Write Program, or Input Program).2.Store Program in Computer Memory (RAM).3.Run Program (PROCESSOR).4.Output Results (SCREEN).5.Storage of Program (HARD DISK C).
#inculde
void main(){
cout
-
8/8/2019 Lecture 1 OK
3/18
Prof. Jihad Mohamd ALJA'AM 3
Computer Components
Keyboard: Input Program
RAM: Primary Storage
Processor: Run Program
Screen: Output Results
Hard Disks: Secondary Storage
The program is stored into the hard diskC through a text
file having a name like:ProgHello.cpp
Permanent
Storage
Temporary
Storage
Hold Program
but needs
Power
Hold Program
With or Without
Power
-
8/8/2019 Lecture 1 OK
4/18
Prof. Jihad Mohamd ALJA'AM 4
The Text File stored into theHard Disk C:ProgHello.cpp
This is a text file that will be stored into the hard disk of
your computer.
You can open the program, update it (Addand
Remove things) and re-save it with the same
name.
You should compile the program in order to
run it. Compile means error-checking.
A compiled program is error-free and can then
be run and produce the results.
#include void main()
{cout
-
8/8/2019 Lecture 1 OK
5/18
Prof. Jihad Mohamd ALJA'AM 5
Some Simple C++ Program
#include void main()
{cout
-
8/8/2019 Lecture 1 OK
6/18
Prof. Jihad Mohamd ALJA'AM 6
#include void main(){cout
-
8/8/2019 Lecture 1 OK
7/18
Prof. Jihad Mohamd ALJA'AM 7
Definition 6: The C++ uses a specific software called
compiler to check the correctness of your program and
convert it to assembly and (0,1)-Language.
Your Program Checking (Compiler) (0,1)-LanguageProgram1.cpp Program1.obj Program1.exe
The compiler issues the list of errors encountered in your
program. They are called syntax error.
The compiler will automatically detect the syntax error
No Syntax error Compiler issues
#include
void main(){cot
-
8/8/2019 Lecture 1 OK
8/18
Prof. Jihad Mohamd ALJA'AM 8
Definition 7: The C++ package contains an IDE
(Integrated Environment) which allows you to write your
program, give it a name, save it into your hard disk,
compile it and run it to see the result.
Definition 8: The computer uses its RAM (internal
memory) to store the program (called the C++ code), the
data and the result.
Random Access Memory
PROGRAM
DATA
RESULT
RAM
Definition 8: The RAM needs power to work and store
the Program. It is called Primary Storage Area.
Definition 9: The computer sends the program to the
hard disk to save it permanently. The hard disk is called
Secondary Storage Area (Permanent Storage).
Processor:
CPU
-
8/8/2019 Lecture 1 OK
9/18
Prof. Jihad Mohamd ALJA'AM 9
RAM (Random Access Memory) STRUCTURE
1 data
2 data
3 data
4 data
5 data
6 data
.
.
.
Addresses are expressed in Hexadecimal(i.e., numbers in base 16).
Every memory location can contain onedata in binary (0,1).
Addresses Location
-
8/8/2019 Lecture 1 OK
10/18
Prof. Jihad Mohamd ALJA'AM 10
C++ Programming Language
This powerful programming language is formed of a
subset of the English language. A set of English words
called keywords orreserved words.
Here are some of these keywords:
cin, cout, if, for, while, main, int,float, double, unsigned, char, switch,
case, void, return, etc.
Every key word has a specific meaning. Whenever we
mistakenly write a keyword, a syntax error is generated.
Example: if we write cot instead of cout, the C++
complier generated a syntax error for the word cot.
The C++ language recognizes the numbers like integer
and real numbers positive and negative.
123, 34.5, -324, -45.6, 342.521, etc
It also recognizes the arithmetic operators:
+, -, /, *, %, etc
In addition, there is a library allowing C++ to recognizethe mathematical functions like:
sin, cos, tan, sqrt, etc
-
8/8/2019 Lecture 1 OK
11/18
Prof. Jihad Mohamd ALJA'AM 11
This is a list of reserved keywords in C++
Keyword Description
and alternative to && operatorand_eq alternative to &= operator
asm insert an assembly instruction
auto(C++0x) automatically detect variable type from initializer
expression (meaningless declare a local variable in pre-C++0x)
bitand alternative to bitwise & operator
bitor alternative to | operator
bool name of builtin boolean type
break break out of a loop
case in switch statement, defines a value label
catch handles exceptions from throwchar name of builtin character type (also 8-bit integer type)
class declare a class
compl alternative to ~ operator
constmodifier for a variable, a pointer to, or a reference to, which states
that the variable cannot be modified
const_cast cast that only allows to strip 'const' or 'volatile' modifiers
continue bypass iterations of a loop
defaultdefault handler in a case statement (in C++0x also force default
implementation of a method)
delete make dynamic memory available (in C++0x also remove a methodthat would be provided by default otherwise)
do begin of a 'do-while' looping construct
double name of a builtin double precision floating-point type
dynamic_castcast a pointer/reference from base (polymorphic) class to derived
class, with runtime error reporting, if cast is not correct
else alternate case for an if statement
enum declare an enumeration type
explicitmake a class's one-argument constructor not allowed to be used for
implicit conversions
export states that the following template declaration will be defined inanother compilation unit
extern
declares an external linkage for a variable (if not initialized, provides
only forwarding declaration for a variable to be declared either later
or in another compilation unit) or (extern C) declares C linkage for
a function
false a constant representing the boolean false value
float name of a single precision floating-point type
-
8/8/2019 Lecture 1 OK
12/18
Prof. Jihad Mohamd ALJA'AM 12
for looping construct
friend grant non-member function access to private data
goto jump to a label (within the same function)
if execute code based on the result of a test
inlinedeclare that a function is to be expanded in place when called (or to
undergo vague linkage, if expanding is not possible)int name of a builtin default integer type
long name of a builtin long integer variable
mutable wipe constness from a class's field, when whole object is const
namespace partition the global namespace by defining a scope
new create an object, allocating memory from dynamic pool
not alternative to ! operator
not_eq alternative to != operator
operator create overloaded operator functions
or alternative to || operator
or_eq alternative to |= operator
private declare private members of a class
protected declare protected members of a class
public declare public members of a class
registerrequest that a variable be implemented by machine's register rather
than on function's stack
reinterpret_castcast between any pointers or any integer with no change, which does
not strip const or volatile modifiers
return return from a function
short declare a short integer variable
signed modify variable type declarations
sizeof return the size of a variable or type
static create permanent storage for a variable
static_castcast that can be done implicitly in reverse, with changing the pointer
for derived class, if required
struct define a new structure
switch execute code based on different possible values for a variable
template create generic functions or classes
this a pointer to the current object inside a class's method
throw throws an exception
true a constant representing the boolean true value
try execute code that can throw an exception
typedef create a new type name from an existing type
typeid describes an object
typename
declare that the identifier next to this word must be always
interpreted as type (and not a variable or function), also declares type
parameter for template
-
8/8/2019 Lecture 1 OK
13/18
Prof. Jihad Mohamd ALJA'AM 13
uniona structure that assigns multiple variables to the same memory
location
unsigned modifier for integer types that makes them only positive range
using import complete or partial namespaces into the current scope
virtual create a function that can be overridden by a derived class
void
name of a builtin void type or declare no return value in functionvolatile warn the compiler about variables that can be modified unexpectedly
wchar_t name of a builtin wide-character type
while begin of 'while' or end of 'do-while' looping constructs
xor alternative to ^ operator
xor_eq alternative to ^= operator
-
8/8/2019 Lecture 1 OK
14/18
Prof. Jihad Mohamd ALJA'AM 14
Arithmetic Operations in C++
Operator Name
+ Addition
Subtract
/ Division
* Multiplication
Example
#include
void main(){cout
-
8/8/2019 Lecture 1 OK
15/18
Prof. Jihad Mohamd ALJA'AM 15
Reading of Data
Statement cin
Read data from the standard input device (Keyboard).
Syntax
cin >> Variable ;
Read data from the standard input device (Keyboard) and
store it atVariable (a location in RAM).
Example
cin >> X ;
Read data from the Keyboardand store it at X (intoRAM, the computer memory);
X can be: integer, float, char,
Exercise-1: Read an integer from thekeyboard and store it at X.
Integers: 45, -102, 342, 25, -12
-
8/8/2019 Lecture 1 OK
16/18
Prof. Jihad Mohamd ALJA'AM 16
Solution// Program done by Fatma// Date: Monday Sep 20, 2010
1 #include
2 void main()3 {4 int X; // X is a number5 cout > X;7 cout X: Read an integer from thekeyboard and store it at X. The usershould type in the integer number and
press the button "Enter".
7: Display two messages X and itsvalue.
8: }: To close the program.
COMMENTS
-
8/8/2019 Lecture 1 OK
17/18
Prof. Jihad Mohamd ALJA'AM 17
#include void main(){
int X; // X is a numbercout
-
8/8/2019 Lecture 1 OK
18/18
Prof Jihad Mohamd ALJA'AM 18