Oop Abeer
Oop Abeer
(OOP)
2022/2021
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
Review of C++
PROGRAMMING
COMPILER
• Compilers are utility programs that take your code and transform it into
executable machine code files.
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
For example, let’s look at the implementation of the Hello World program:
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
• The file <iostream>, which is a standard file that should come with the
C++ compiler, is short for input-output streams. This command
contains code for displaying and getting an input from the user.
• This code is saying: Use the cout and endl tools from the std toolbox.
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
• The name cout is short for character output and displays whatever is
between the << brackets.
• Symbols such as << can also behave like functions and are used with the
keyword cout.
NAMESPACE
Consider a situation, when we have two persons with the same name,
(Ahmed), in the same class. Whenever we need to differentiate them
definitely we would have to use some additional information along with
their name, like either the area, if they live in different area or their
mother’s or father’s name, etc.
Same situation can arise in your C++ applications. For example, you might
be writing some code that has a function called xyz() and there is another
library available which is also having same function xyz(). Now the
compiler has no way of knowing which version of xyz() function you are
referring to within your code.
IDENTIFIERS
Class Names
class Calculation
class Demo Variable Names
int a;
Valid Functions Names int marks;
void display() int rank;
void getMarks() double res_marks;
VARIABLES
A variable provides us with named storage that our programs can manipulate.
Each variable in C++ has a specific type, which determines the size and layout
of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable. The
name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and
lowercase letters are distinct because C++ is case-sensitive: There are
following basic types of variable in C++
Type Description
bool Stores either value true or false.
char Typically a single octet one byte. This is an
integer type.
int The most natural size of integer for the
machine.
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
A variable definition tells the compiler where and how much storage to create
for the variable. A variable definition specifies a data type, and contains a list
of one or more variables of that type as follows −
Here, type must be a valid C++ data type including char, w_char, int, float,
double, bool or any user-defined object, etc., and variable_list may consist of
one or more identifier names separated by commas. Some valid declarations
are shown here
The line int i, j, k; both declares and defines the variables i, j and k; which
instructs the compiler to create variables named i, j and k of type int.
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
For definition without an initializer: variables with static storage duration are
implicitly initialized with NULL allbyteshavethevalue0; the initial value of all
other variables is undefined.
Example
Try the following example where a variable has been declared at the top, but it
has been defined inside the main function:
CONSTANTS
Constants refer to fixed values that the program may not alter and they are
called literals.
Constants can be of any of the basic data types and can be divided into
Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean
Values.
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
Again, constants are treated just like regular variables except that their values
cannot be modified after their definition.
Defining Constants
When the above code is compiled and executed, it produces the following
result −
50
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
You can use const prefix to declare constants with a specific type as follows −
When the above code is compiled and executed, it produces the following
result −
50
Enumeration (ENUM)
Enumeration is a user defined data type in C/C++ language. It is used to
assign names to the integral constants which makes a program easy to read
and maintain. The keyword “enum” is used to declare an enumeration.
Here,
The enum keyword is also used to define the variables of enum type. There are
two ways to define the variables of enum type as follows −
Example
#include <iostream>
using namespace std;
enum colors{red=5, black};
enum suit{heart, diamond=8, spade=3, club};
int main() {
cout <<"The value of enum color : "<<red<<","<<black;
cout <<"\nThe default value of enum suit :
"<<heart<<","<<diamond<<","<<spade<<","<<club;
return 0;
}
Output
In the above program, two enums are declared as color and suit outside the
main() function.
OPERATORS
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Arithmetic Operators
Relational Operators
For example :
#include <iostream>
using namespace std;
int main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
cout << "Line 1 - a is equal to b" << endl ;
} else {
cout << "Line 1 - a is not equal to b" << endl ;
}
if( a < b ) {
cout << "Line 2 - a is less than b" << endl ;
} else {
cout << "Line 2 - a is not less than b" << endl ;
}
if( a > b ) {
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
if( b >= a ) {
cout << "Line 5 - b is either greater than \ or equal to b" << endl ;
}
return 0;
}
When the above code is compiled and executed, it produces the following
result −
Logical Operators
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
For example
#include <iostream>
int main() {
bool is tall=true;
else {
return 0;
Example2:
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 20;
int c ;
if(a && b) {
cout << "Line 1 - Condition is true"<< endl ;
}
if(a || b) {
cout << "Line 2 - Condition is true"<< endl ;
}
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
if(a && b) {
cout << "Line 3 - Condition is true"<< endl ;
} else {
cout << "Line 4 - Condition is not true"<< endl ;
}
return 0;
}
When the above code is compiled and executed, it produces the following
result −
Bitwise Operators
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
TYPE CASTING
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
Syntax:
int num1;
float num2;
In the above syntax, the value of num1 has promoted from int to float directly
which is also known as standard conversion.
• Static Cast: It is used to cast a pointer of base class into derived class.
• Dynamic Cast: It is used in runtime casting.
• Constant Cast: It is used in explicitly overriding constant in a cast.
• Reinterpret Cast: It is used to change a pointer to any other type of
pointer.
Example
here is c++ program to demonstrate the working of implicit & explicit type
conversion:
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
#include <iostream>
int main()
int a = 15 ;
char b = 'c' ;
a = a + b ; // implicitly conversion of a.
return 0 ;
Output:
The value of b is c
Example 2
#include <iostream>
using namespace std;
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
x = x + y;
return 0;
}
Output
x = 107
y=a
z = 108
CONTROL STRCTURE
C++ has only three kinds of control structures, which from this point forward
we refer to as control statements: the sequence statement, selection
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
C++ if Statement
if (condition) {
// body of if statement
}
Example
#include <iostream>
int main() {
int number;
if (number > 0) {
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
cout << "You entered a positive integer: " << number << endl;
return 0;
C++ if...else
The if statement can have an optional else clause. Its syntax is:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
#include <iostream>
int main() {
int number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
else {
cout << "You entered a negative integer: " << number << endl;
return 0;
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
// code block 3
}
Here,
Note: There can be more than one else if statement but only
one if and else statements.
Example 3
#include <iostream>
int main() {
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
int number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
cout << "You entered a negative integer: " << number << endl;
else {
return 0;
// outer if statement
if (condition1) {
// statements
// inner if statement
if (condition2) {
// statements
}
}
Notes:
• We can add else and else if statements to the inner if statement as required.
• The inner if statement can also be inserted inside the outer else or else
if statements (if they exist).
• We can nest multiple layers of if statements.
Example
#include <iostream>
int main() {
int num;
// outer if condition
if (num != 0) {
// inner if condition
if ((num % 2) == 0) {
else {
else {
cout << "The number is 0 and it is neither even nor odd." << endl;
• We take an integer as an input from the user and store it in the variable num.
• We then use an if...else statement to check whether num is not equal to 0.
o If true, then the inner if...else statement is executed.
o If false, the code inside the outer else condition is executed, which prints
"The number is 0 and neither even nor odd."
• The inner if...else statement checks whether the input number is divisible
by 2.
o If true, then we print a statement saying that the number is even.
o If false, we print that the number is odd.
Notice that 0 is also divisible by 2, but it is actually not an even number. This
is why we first make sure that the input number is not 0 in the
outer if condition.
Note: As you can see, nested if...else makes your logic complicated. If
possible, you should always try to avoid nested if...else.
Example
Read three number and check which one is larger using if-else?
#include <iostream>
int main() {
int a,b,c;
cin>>a;
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
cin>>b;
cin>>c;
else
}
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
return 0;
SWITCH STATEMENT
Syntax:
switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared to and a colon.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
Example
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
char grade = 'A';
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
Another solution
#include <iostream>
int main () {
int grade;
cin>>grade;
switch(grade) {
case 100 :
break;
case 80 :
break;
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
case 60 :
break;
case 49 :
case 30 :
break;
default :
return 0;
# include <iostream>
int main() {
char op;
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2nd Stage Al- Esraa University Collage
break;
default:
break;
return 0;