C++ - Operators
Chapter 5: Operators
In this chapter, we will learn about operators in C++. We will start by learning about the different types of operators in C++ and how to use them. Operators are used to perform operations on variables and values.
Types of Operators
C++ provides several types of operators that are used to perform operations on variables and values. Some of the types of operators in C++ include:
- Arithmetic Operators: These are operators that are used to perform arithmetic operations on variables and values.
- Assignment Operators: These are operators that are used to assign values to variables.
- Comparison Operators: These are operators that are used to compare values and determine if they are equal or not.
- Logical Operators: These are operators that are used to perform logical operations on variables and values.
- Increment and Decrement Operators: These are operators that are used to increment or decrement the value of a variable.
- Bitwise Operators: These are operators that are used to perform bitwise operations on variables and values.
To learn these operators, we will use tasks based approach. Each task will help you to understand the requirements and then you will be able to implement the code.
1. Arithmetic Operators
Task
Challenge: Create a program that calculates the total price of tea cups. The user inputs the number of cups they want and the price per cup. The program should calculate the total price, apply a 5% discount if the total is above a certain amount, and show the final price.
Solution
Going through the code:
- The
int cups;
line declares a variable namedcups
of typeint
and does not assign a value to it. - The
double pricePerCup, totalPrice, discountedPrice;
line declares three variables namedpricePerCup
,totalPrice
, anddiscountedPrice
of typedouble
and does not assign a value to them. - The
cout << "Enter the number of tea cups: ";
line prints the string “Enter the number of tea cups: ” to the console. - The
cin >> cups;
line reads an integer from the console and assigns it to thecups
variable. - The
cout << "Enter the price per cups: ";
line prints the string “Enter the price per cups: ” to the console. - The
cin >> pricePerCup;
line reads a double from the console and assigns it to thepricePerCup
variable. - The
totalPrice = cups * pricePerCup;
line calculates the total price by multiplying thecups
andpricePerCup
variables. - The
if (totalPrice > 100) {
line starts anif
statement that checks if thetotalPrice
variable is greater than100
. - The
discountedPrice = totalPrice - (totalPrice * 0.05);
line calculates the discounted price by subtracting thetotalPrice
multiplied by0.05
from thetotalPrice
variable. - The
cout << "Discounted price is: " << discountedPrice << endl;
line prints the string “Discounted price is: ” followed by the value of thediscountedPrice
variable to the console. - The
} else {
line starts anelse
block that is executed if theif
statement is false. - The
cout << "Total price is " << totalPrice << endl;
line prints the string “Total price is ” followed by the value of thetotalPrice
variable to the console. - The
}
line ends theif
statement.
2. Assignment Operators
Task
Write a program that allows a user to input the number of tea bags they have. Assign additional bags to them based on certain conditions (e.g., if they have fewer than 10 bags, give them 5 extra). Update the original number using assignment operators.
Solution
Going through the code:
- The
int teaBags;
line declares a variable namedteaBags
of typeint
and does not assign a value to it. - The
cout << "Enter the number of tea bags you have: ";
line prints the string “Enter the number of tea bags you have: ” to the console. - The
cin >> teaBags;
line reads an integer from the console and assigns it to theteaBags
variable. - The
if (teaBags < 10) {
line starts anif
statement that checks if theteaBags
variable is less than10
. - The
// teaBags = teaBags + 5
line adds5
to theteaBags
variable. - The
teaBags += 5;
line adds5
to theteaBags
variable. - The
}
line ends theif
statement. - The
cout << "Your total bags are: " << teaBags;
line prints the string “Your total bags are: ” followed by the value of theteaBags
variable to the console.
3. Relational Operators
Task
A tea shop offers a loyalty program. Customers who buy more than 20 cups of tea get a special “Gold” badge, and those who buy 10 to 20 cups get a “Silver” badge. Write a program to display the badge they will receive based on the number of cups they buy.
Solution
Going through the code
- The
int cups;
line declares a variable namedcups
of typeint
and does not assign a value to it. - The
cout << "Enter the number of cups you have";
line prints the string “Enter the number of cups you have” to the console. - The
cin >> cups;
line reads an integer from the console and assigns it to thecups
variable. - The
if (cups > 20) {
line starts anif
statement that checks if thecups
variable is greater than20
. - The
cout << "You will get a GOLD badge" << endl;
line prints the string “You will get a GOLD badge” followed by a newline character to the console. - The
} else if (cups >= 10 && cups <= 20) {
line starts anelse if
block that is executed if theif
statement is false. - The
cout << "You will get a SILVER badge" << endl;
line prints the string “You will get a SILVER badge” followed by a newline character to the console. - The
} else {
line starts anelse
block that is executed if theif
statement is false. - The
cout << "NO BADGE for you" << endl;
line prints the string “NO BADGE for you” followed by a newline character to the console. - The
}
line ends theif
statement.
4. Logical Operators
Task
Create a program that checks if a user is eligible for a tea subscription discount. The discount applies if the user is either a student or has purchased more than 15 cups. Ask the user to input their status (student or not) and their cup count.
Solution
Going through the code
- The
bool isStudent;
line declares a variable namedisStudent
of typebool
and does not assign a value to it. - The
int cups;
line declares a variable namedcups
of typeint
and does not assign a value to it. - The
cout << "Are you a student (1 for yes and 0 for No) ?";
line prints the string “Are you a student (1 for yes and 0 for No) ?” to the console. - The
cin >> isStudent;
line reads a boolean value from the console and assigns it to theisStudent
variable. - The
cout << "How many cups of tea have you purchased ?";
line prints the string “How many cups of tea have you purchased ?” to the console. - The
cin >> cups;
line reads an integer from the console and assigns it to thecups
variable. - The
if (isStudent || cups > 15) {
line starts anif
statement that checks if theisStudent
variable is true or if thecups
variable is greater than15
. - The
cout << "You are elegible for a discount " << endl;
line prints the string “You are elegible for a discount ” followed by a newline character to the console. - The
} else {
line starts anelse
block that is executed if theif
statement is false. - The
cout << "You are NOT elegible for a discount " << endl;
line prints the string “You are NOT elegible for a discount ” followed by a newline character to the console. - The
}
line ends theif
statement.
5. Bitwise Operators
Bitwise operators are used to perform bitwise operations on variables and values. Bitwise operations are used to manipulate individual bits of a binary number. Decimal numbers are represented using binary numbers, which are made up of 1s and 0s. Bitwise operators are used to perform operations on these binary numbers.
You can learn more about bitwise operators by visiting Bitwisecmd.com. We talked about it in the video, watch it for more information.
Summary
In this chapter, we have learned about operators in C++. We have also learned about the different types of operators and how to use them. By the end of this chapter, you should have a good understanding of how to use operators in C++.