OOP Lab - 2.1 - C++ Operators
OOP Lab - 2.1 - C++ Operators
C++ Programming
Computer Instructor: Muhammad Abdullah Orakzai
1) Operators in C++
2) Unary operators
3) Binary operators
4) Ternary operators
5) Lab Task
6) Home Tasks
2
Operators
Example
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
4
Types of Operators
1. Unary operators
2. Binary operators
3. Ternary operators
5
1) Unary Operator
1. Increment (++)
2. Decrement (--)
3. Negation (!)
6
2) Binary Operator
1. Arithmetic (+, -, *, /, %)
2. Relational (>, <, >=, <=, !=, ==)
3. Logical (&&, ||)
4. Assignment (=)
5. Arithmetic Assignment operator (+=, -=, *=, /=, %=)
7
3) Ternary Operator
Example
(condition) ? statement 1 : statement 2;
int result= (n1>n2) ? n1 : n2;
8
3) Ternary Operator…
#include<iostream>
int main()
{
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}
9
Arithmetic Operators in C++
Arithmetic operators are used to perform common mathematical operations.
Operator Name Description Example
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
int main()
{
int n1, n2, sum;
cout<<"Enter first number:\t";
cin>>n1;
cout<<"Enter 2nd number:\t";
cin>>n2;
sum=n1+n2;
cout<<"The sum is:\t"<<sum<<endl;
11
}
Assignment Operator
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
Example
int x = 10;
12
Arithmetic Assignment Operator
Example
int x = 10;
x += 5;
13
Arithmetic Assignment Operator…
❖ Note: The return value of a comparison is either true (1) or false (0).
❖ In the following example, we use the greater than operator (>) to find
out if 5 is greater than 3:
Example
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3 15
Relational/Comparison Operators
Logical operators are used to determine the logic between variables or values:
&& Logical and Returns true if both statements x < 5 && x < 10
are true
! Logical not Reverse the result, returns false !(x < 5 && x < 10)
if the result is true
17
Increment and Decrement Operators
1) Increment Operator:
The operators that is used to add 1 to the value of a variable is called
increment operator.
2) Decrement Operator :
The operator that is used to subtract 1 from the value of a variable is called
decrement operator.
18
1) The Increment Operator (++)
❖Prefix and postfix operators have different effects when they are used
in expressions. 20
i) Prefix Increment Operator
21
i) Prefix Increment Operator…
#include<iostream>
int main()
{
int a=2;
int b=3;
int c=2;
int result=a+b+(++c);
cout<<"Result is: "<<result;
cout<<"\nValue of c is: "<<c;
} 22
i ) Prefix Increment Operator…
23
ii) Postfix Increment Operator
24
ii) Postfix Increment Operator…
In this case, 1 will be added to the value of c after its existing value has been
used in the expression. Thus after execution, the result will be equal to 7 and
the value of c will be 3.
25
ii) Postfix Increment Operator…
#include<iostream>
int main()
{
int a=2;
int b=3;
int c=2;
int result=a+b+(c++);
cout<<"Result is: "<<result;
cout<<"\nValue of c is: "<<c;
} 26
2) The Decrement Operator (--)
27
i ) Prefix Decrement Operator
28
i ) Prefix Decrement Operator…
#include<iostream>
int main()
{
int a=2;
int b=3;
int c=2;
int result=a+b+(--c);
cout<<"Result is: "<<result;
cout<<"\nValue of c is: "<<c;
} 29
i )Prefix Decrement Operator…
30
ii) Postfix Decrement Operator
31
ii) Postfix Decrement Operator…
In this case, 1 will be subtracted from the value of c after its existing value has been used
in the expression. Thus after execution, the result will be equal to 7 and the value of c
will be 1.
32
ii) Postfix Decrement Operator…
#include<iostream>
int main()
{
int a=2;
int b=3;
int c=2;
int result=a+b+(c--);
cout<<"Result is: "<<result;
cout<<"\nValue of c is: "<<c;
} 33
Class Task-1
Ask user to enter a three digit number and then display the
number in reverse order.
34
Task-1 Solution
#include<iostream>
int main()
{
int number;
cout<<"Enter 3 digit number:";
cin>>number;
cout<<number%10;
number=number/10;
cout<<number%10;
number=number/10;
cout<<number;
} 35
Assignment # 01
1) Write a C++ program that will convert dollar to rupees (Dollar to Rupees Conversion
Calculator).
2) Write a C++ program that will convert rupees to dollar (Rupees to Dollar Conversion
Calculator).
3) Write a C++ program that will convert centigrade to Fahrenheit.
4) Take student name and marks of your 2nd semester from user and then generate DMC
which will contain obtained marks out of total and percentage.
36
References
• https://beginnersbook.com/2017/08/cpp-data-types/
• https://www.geeksforgeeks.org/c-data-types/
• http://www.cplusplus.com/doc/tutorial/basic_io/
• https://www.geeksforgeeks.org/basic-input-output-c/
• https://www.w3schools.com/cpp/default.asp
• https://www.javatpoint.com/cpp-tutorial
37
THANK YOU
Thanks
38