Chapter 2
An Overview of C++
Ragia A. Ibrahim, Ph.D. Student
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
‫بحوث‬ ‫مركز‬ ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬
2
Is prime?
Start
“Enter Positive Integer Number”
Store Value In
N
i<n
i=2
prime= true
i++
prime= false
n mod i = 0
print prime
Stop
Introduction
• The programming language C++ was
developed as extension of C.
• C++ provides capabilities for object
oriented programming.
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
C++ is hybrid language which has
structured style, an object-oriented style
or both
Introduction
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
Programming example: converting
units of measurement
• Problem statement: you work in a store
that imports textile most of the textile you
receive is measured in square meters.
However, the store’s customers want to
know the equivalent amount in square
yards. You need to write an algorithm that
performs this conversion
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Problem analysis:
• Problem input:
• The textile size in square meters
• Problem output:
• The textile size in square yards
• Relevant formula:
• 1 square yards= 1.196 square meter
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Program
// this program converts square meters to square yards
# include <iostream.h>
Int main()
{
float m,y;
cout<<“enter the textile size in square meters:n”;
cin>>m;
y = 1.196*m;
cout<<“the textile size in square yards is”<<y;
return 0;
} ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Output window
Enter the textile size in square meters:
2.0
The textile size in square yards is 2.392
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Data Types and Variables
Declaration
ExampleDetailsData
type
Ch=‘K’;
ch=‘k’;
char ch;8 bitschar
n=9;Int n;Short 16, 32 bits
long 64 bits (10 digits)
Int
s=4.7float s;(machine Dependent)
float s;
(6 digits).
float
f=0;0
0 false
1 true
bool f;bool f;bool
Sign -1 Magnitude-31
Sign -1 Exponent 23 Fraction-8
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Arithmetic operators
operation C++ operator expression
Addition + X+y
Subtraction - X-y
Multiplication * X*y
Division / x/y
Modulus % X%y
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
Assignment operators
• Sum = i = 0;
• Sum = (i=0);
Precedence order
last2nd1st
+ ,-*,/,%()
Associates RIGHT to LEFT
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Relational Operators
Operation C++
operator
Expression
Equality == X==y
inequality != X!=y
Greater than > X>y
Less than < X<y
Greater than or equal to >= X>=y
Less than or equal to <= X<=y
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
Logical operators
Operation C++ operator Expression
Logical negation ! !x
Logical and && X && y
Logical or || X || y
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
Shorthand Assignment Operators
• lhs op = rhs
• +=,-=,*=,/=,%=,>>=,<<=,&=,|=,and ^=
• P *=x;
• lhs = lhs op rhs
e.g. p *=x; p=p*x;
p+=x; p=p+x;
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Postfix and Prefix Assignment
i++ or ++i is equivalent to i + = 1
i-- or --i is equivalent to i - = 1
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
Postfix and Prefix Assignment
//pre-incrementing and post-incrementing
#include <iostream.h>
Int main()
{ int c=5;
cout<<c;
cout <<c++;
cout <<c<<endle;
c=5;
cout <<c;
cout <<++c;
cout <<c;
return 0;
}
5 5 6
5 6 6
Output
window
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
The Evaluation Order of Operators
Operator type Operation Associativity
Prefix ++, --
Unary ! Left to right
Arithmetic * , / , % , + , - Left to right
Relational < , > , <= , >=,==,!= Left to right
Logical && || Left to right
Assignment
postfix
=, +=, -=, *=, /=, %=
Right to left
++ , --
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
Expression Value
X>y || z<=y 0||1 1
--x || y&&z 0||1 1
++x + y > !z /w 2 + 3> 0/4 1
E.g.
x=1 y=2 z=3 w=4
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
Function?.
Function body consists of declaration and
statements enclosed between { }
Variable type f_name(tyep1,type2…)
{
}
e.g.
Int main()
{
}
‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
HW…please try to solve the
following from the text book:
1
4
5
7

Chapter2

  • 1.
    Chapter 2 An Overviewof C++ Ragia A. Ibrahim, Ph.D. Student ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 2.
    ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ 2 Is prime? Start “Enter Positive Integer Number” Store Value In N i<n i=2 prime= true i++ prime= false n mod i = 0 print prime Stop
  • 3.
    Introduction • The programminglanguage C++ was developed as extension of C. • C++ provides capabilities for object oriented programming. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 4.
    C++ is hybridlanguage which has structured style, an object-oriented style or both Introduction ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
  • 5.
    Programming example: converting unitsof measurement • Problem statement: you work in a store that imports textile most of the textile you receive is measured in square meters. However, the store’s customers want to know the equivalent amount in square yards. You need to write an algorithm that performs this conversion ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 6.
    Problem analysis: • Probleminput: • The textile size in square meters • Problem output: • The textile size in square yards • Relevant formula: • 1 square yards= 1.196 square meter ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 7.
    Program // this programconverts square meters to square yards # include <iostream.h> Int main() { float m,y; cout<<“enter the textile size in square meters:n”; cin>>m; y = 1.196*m; cout<<“the textile size in square yards is”<<y; return 0; } ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 8.
    Output window Enter thetextile size in square meters: 2.0 The textile size in square yards is 2.392 ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 9.
    Data Types andVariables Declaration ExampleDetailsData type Ch=‘K’; ch=‘k’; char ch;8 bitschar n=9;Int n;Short 16, 32 bits long 64 bits (10 digits) Int s=4.7float s;(machine Dependent) float s; (6 digits). float f=0;0 0 false 1 true bool f;bool f;bool Sign -1 Magnitude-31 Sign -1 Exponent 23 Fraction-8 ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 10.
    Arithmetic operators operation C++operator expression Addition + X+y Subtraction - X-y Multiplication * X*y Division / x/y Modulus % X%y ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
  • 11.
    Assignment operators • Sum= i = 0; • Sum = (i=0); Precedence order last2nd1st + ,-*,/,%() Associates RIGHT to LEFT ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 12.
    Relational Operators Operation C++ operator Expression Equality== X==y inequality != X!=y Greater than > X>y Less than < X<y Greater than or equal to >= X>=y Less than or equal to <= X<=y ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
  • 13.
    Logical operators Operation C++operator Expression Logical negation ! !x Logical and && X && y Logical or || X || y ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
  • 14.
    Shorthand Assignment Operators •lhs op = rhs • +=,-=,*=,/=,%=,>>=,<<=,&=,|=,and ^= • P *=x; • lhs = lhs op rhs e.g. p *=x; p=p*x; p+=x; p=p+x; ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 15.
    Postfix and PrefixAssignment i++ or ++i is equivalent to i + = 1 i-- or --i is equivalent to i - = 1 ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 16.
    Postfix and PrefixAssignment //pre-incrementing and post-incrementing #include <iostream.h> Int main() { int c=5; cout<<c; cout <<c++; cout <<c<<endle; c=5; cout <<c; cout <<++c; cout <<c; return 0; } 5 5 6 5 6 6 Output window ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
  • 17.
    The Evaluation Orderof Operators Operator type Operation Associativity Prefix ++, -- Unary ! Left to right Arithmetic * , / , % , + , - Left to right Relational < , > , <= , >=,==,!= Left to right Logical && || Left to right Assignment postfix =, +=, -=, *=, /=, %= Right to left ++ , -- ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
  • 18.
    Expression Value X>y ||z<=y 0||1 1 --x || y&&z 0||1 1 ++x + y > !z /w 2 + 3> 0/4 1 E.g. x=1 y=2 z=3 w=4 ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬
  • 19.
    ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬ Function?. Function body consists of declaration and statements enclosed between { } Variable type f_name(tyep1,type2…) { } e.g. Int main() { }
  • 20.
    ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬‫القاهرة‬ ‫جامعة‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ ‫االحصائيه‬ HW…please try to solve the following from the text book: 1 4 5 7