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-step
descriptions 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 of instructions called statements. These statements
are understandable by the computer machine.
Definition 3: The computer uses its memory called RAM
(Random Access Memory), to store the program.
Definition 4: The computer uses its brain called
processor (CPU) to perform the C++ statements.
Definition 5: The C++ program has a well defined
structure as follows:
1 #include <iostream.h>
2 void main()
                                 The C++ Program
3 {                              Structure
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 or ProgHello.cpp
Prof. Jihad Mohamd ALJA'AM   1
                                               Example
           #inculde <iostream.h>
                                                         This is a C++ program.
           void main()
           {
                                                         It has one statement:
             cout << “Hello, World”;
                                                         cout << “Hello, World”;
           }
                                                                     Storage in RAM
         Output                                                      Primary Storage
  Computer SCREEN                                               RAM contains Program
                                                                Random Access Memory
Hello, World                    RUN                             #inculde <iostream.h>
                                                                void main()
                                                                {
                                                                  cout << “Hello, World”;
                                                                }
                                                 Store       Storage in Hard Disk
                                                              Secondary Storage
     The program is stored                   Hard Disk: C
     into the hard disk C.
                                       #inculde <iostream.h>
     You should give it a              void main()
     name like:                        {
                                         cout << “Hello, World”;
                                       }
      ProgHello.cpp
                                       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).
         Prof. Jihad Mohamd ALJA'AM                2
                     Computer Components
                                  Keyboard: Input Program
Temporary                                  Hold Program
 Storage                                    but needs
                                              Power
                                      RAM: Primary Storage
                                      Processor: Run Program
                                      Screen: Output Results
                                        Hold Program
Permanent                               With or Without
 Storage                                    Power
                                Hard Disks: Secondary Storage
    The program is stored into the hard disk C through a text
            file having a name like: ProgHello.cpp
   Prof. Jihad Mohamd ALJA'AM     3
The Text File stored into theHard Disk C: ProgHello.cpp
           #include <iostream.h>
           void main()
           {
             cout << “Hello, World”;
           }
This is a text file that will be stored into the hard disk of
your computer.
                 ProgHello.cpp                 Hard Disk: C
You can open the program, update it (Add and
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.
Prof. Jihad Mohamd ALJA'AM       4
              Some Simple C++ Program
        #include <iostream.h>
        void main()
        {
         cout << “************\n”;
         cout << “Hello, World\n”;
         cout << “************”;
        }
                    ************
                    Hello, World
                    ************
        #include <iostream.h>
        void main()
        {
         cout << “    *\n”;
         cout << “   ***\n”;
         cout << “ *****”;
        }
                               *
                                     Screen
                              ***    Output
                             *****
Prof. Jihad Mohamd ALJA'AM     5
      #include <iostream.h>
      void main()
      {
        cout << “********\n”;
        cout << “********\n”;
        cout << “********”;
      }
                       *******
                                    Screen
                       *******      Output
                       *******
        #include <iostream.h>
        void main()
        {
          cout << “5 + 8 = ”;
          cout << 5+8;
          }
                       5 + 8 = 13
Prof. Jihad Mohamd ALJA'AM   6
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)-Language
    Program1.cpp  Program1.obj  Program1.exe
The compiler issues the list of errors encountered in your
program. They are called syntax error.
  #include <iostream.h>
  void main()                       COMPILE
  {                                            Syntax Error
    cot << “    *\n”;                           “cot” not
    cout << “  ***\n”;                           declared
    cout << “ *****”;
  }
The compiler will automatically detect the syntax error
                                                  .obj
                                                Program
No Syntax error  Compiler issues
                                                  .exe
                                                Program
SCREEN       Output Results            Run    .exe
Prof. Jihad Mohamd ALJA'AM    7
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
                             Processor:
                               CPU
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).
Prof. Jihad Mohamd ALJA'AM        8
   RAM (Random Access Memory) STRUCTURE
            Addresses            Location
              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 one
    data in binary (0,1).
Prof. Jihad Mohamd ALJA'AM   9
            C++ Programming Language
This powerful programming language is formed of a
subset of the English language. A set of English words
called keywords or reserved 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 recognize
the mathematical functions like:
            sin, cos, tan, sqrt, etc
Prof. Jihad Mohamd ALJA'AM   10
   This is a list of reserved keywords in C++
Keyword                                 Description
and            alternative to && operator
and_eq         alternative to &= operator
asm            insert an assembly instruction
               (C++0x) automatically detect variable type from initializer
auto
               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 throw
char           name of builtin character type (also 8-bit integer type)
class          declare a class
compl          alternative to ~ operator
               modifier for a variable, a pointer to, or a reference to, which states
const
               that the variable cannot be modified
const_cast     cast that only allows to strip 'const' or 'volatile' modifiers
continue       bypass iterations of a loop
               default handler in a case statement (in C++0x also force default
default
               implementation of a method)
               make dynamic memory available (in C++0x also remove a method
delete
               that would be provided by default otherwise)
do             begin of a 'do-while' looping construct
double         name of a builtin double precision floating-point type
               cast a pointer/reference from base (polymorphic) class to derived
dynamic_cast
               class, with runtime error reporting, if cast is not correct
else           alternate case for an if statement
enum           declare an enumeration type
               make a class's one-argument constructor not allowed to be used for
explicit
               implicit conversions
               states that the following template declaration will be defined in
export
               another compilation unit
               declares an external linkage for a variable (if not initialized, provides
               only forwarding declaration for a variable to be declared either later
extern
               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
Prof. Jihad Mohamd ALJA'AM               11
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
                 declare that a function is to be expanded in place when called (or to
inline
                 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
                 request that a variable be implemented by machine's register rather
register
                 than on function's stack
                 cast between any pointers or any integer with no change, which does
reinterpret_cast
                 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
                 cast that can be done implicitly in reverse, with changing the pointer
static_cast
                 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
                 declare that the identifier next to this word must be always
typename         interpreted as type (and not a variable or function), also declares type
                 parameter for template
Prof. Jihad Mohamd ALJA'AM                 12
            a structure that assigns multiple variables to the same memory
union
            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 function
volatile    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
Prof. Jihad Mohamd ALJA'AM          13
        Arithmetic Operations in C++
          Operator                          Name
             +                             Addition
             –                            Subtraction
             /                             Division
             *                           Multiplication
                             Example
  #include <iostream.h>
  void main()
  {
    cout << “6*3 = ” << 6*3                 <<   endl;
    cout << “6-3 = ” << 6-3                 <<   endl;
    cout << “6/3 = ” << 6/3                 <<   endl;
    cout << “6+3 = ” << 6+3                 <<   endl;
  }
                       6*3     =    18
                       6-3     =    3
                       6/3     =    2
                       6+3     =    9
Prof. Jihad Mohamd ALJA'AM     14
                   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 at Variable (a location in RAM).
                             Example
                        cin >> X ;
Read data from the Keyboard and store it at X (into
RAM, the computer memory);
X can be: integer, float, char, …
Exercise-1: Read an integer from the
keyboard and store it at X.
Integers:          45,       -102,     342,   25,   -12
Prof. Jihad Mohamd ALJA'AM     15
Solution
  COMMENTS
          // Program done by Fatma
          // Date: Monday Sep 20, 2010
1 #include <iostream.h>
2 void main()
3 {
4     int X;    // X is a number
5     cout << "Enter The Value X = ";
6     cin >> X;
7     cout << "X = " << X;
8 }
1: input-output: To allow read and
write (use of cin and cout).
2: the main program: To allow                      the
proper start of the program.
3: {: To begin the program.
4: Declare a variable X of                        type
integer. X can receive integers.
5: Display a message. We prompt the
user to enter the value of X.
6: cin >> X: Read an integer from the
keyboard and store it at X. The user
should type in the integer number and
press the button "Enter".
7: Display           two     messages   X   and    its
value.
8: }: To close the program.
Prof. Jihad Mohamd ALJA'AM   16
#include <iostream.h>
void main()
{
   int X;    // X is a number
   cout << "Enter The Value of X = ";
   cin >> X;
   cout << "X = " << X;
}
    Enter The Value X = 34
    X = 34
Exercise-1:   Run all   the  previous
programs on your computer machine and
see the results. Bring a printed copy
next lecture.
Exercise-2: Write a C++ program that
asks the user to enter his current age
and display his age of next year.
Exercise-3: Find ERRORS and CORRECT.
#include <iostraam.h>
void main ()
{ ;
  cout << “8%3 = ” << “8%3” << endl;
  cont; << 16-3 = ” << 16-“3” <<\n;;
  cout << “9/3 = ” << 9/3 << end;
}
Prof. Jihad Mohamd ALJA'AM   17
Prof. Jihad Mohamd ALJA'AM   18