Skip to content

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

#include <iostream>
using namespace std;
int main(){
int cups;
double pricePerCup, totalPrice, discountedPrice;
cout << "Enter the number of tea cups: ";
cin >> cups;
cout << "Enter the price per cups: ";
cin >> pricePerCup;
totalPrice = cups * pricePerCup;
// apply 5% discount if total price is above 100
if (totalPrice > 100) {
discountedPrice = totalPrice - (totalPrice * 0.05);
cout << "Discounted price is: " << discountedPrice << endl;
} else {
cout << "Total price is " << totalPrice << endl;
}
return 0;
}

Going through the code:

  • The int cups; line declares a variable named cups of type int and does not assign a value to it.
  • The double pricePerCup, totalPrice, discountedPrice; line declares three variables named pricePerCup, totalPrice, and discountedPrice of type double 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 the cups 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 the pricePerCup variable.
  • The totalPrice = cups * pricePerCup; line calculates the total price by multiplying the cups and pricePerCup variables.
  • The if (totalPrice > 100) { line starts an if statement that checks if the totalPrice variable is greater than 100.
  • The discountedPrice = totalPrice - (totalPrice * 0.05); line calculates the discounted price by subtracting the totalPrice multiplied by 0.05 from the totalPrice variable.
  • The cout << "Discounted price is: " << discountedPrice << endl; line prints the string “Discounted price is: ” followed by the value of the discountedPrice variable to the console.
  • The } else { line starts an else block that is executed if the if statement is false.
  • The cout << "Total price is " << totalPrice << endl; line prints the string “Total price is ” followed by the value of the totalPrice variable to the console.
  • The } line ends the if 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

#include <iostream>
using namespace std;
int main(){
int teaBags;
cout << "Enter the number of tea bags you have: ";
cin >> teaBags;
if (teaBags < 10) {
// teaBags = teaBags + 5
teaBags += 5;
}
cout << "Your total bags are: " << teaBags;
return 0;
}

Going through the code:

  • The int teaBags; line declares a variable named teaBags of type int 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 the teaBags variable.
  • The if (teaBags < 10) { line starts an if statement that checks if the teaBags variable is less than 10.
  • The // teaBags = teaBags + 5 line adds 5 to the teaBags variable.
  • The teaBags += 5; line adds 5 to the teaBags variable.
  • The } line ends the if statement.
  • The cout << "Your total bags are: " << teaBags; line prints the string “Your total bags are: ” followed by the value of the teaBags 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

#include <iostream>
using namespace std;
int main(){
int cups;
cout << "Enter the number of cups you have";
cin >> cups;
if (cups > 20) {
cout << "You will get a GOLD badge" << endl;
} else if (cups >= 10 && cups <= 20) {
cout << "You will get a SILVER badge" << endl;
} else {
cout << "NO BADGE for you" << endl;
}
return 0;
}

Going through the code

  • The int cups; line declares a variable named cups of type int 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 the cups variable.
  • The if (cups > 20) { line starts an if statement that checks if the cups variable is greater than 20.
  • 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 an else if block that is executed if the if 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 an else block that is executed if the if 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 the if 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

#include <iostream>
using namespace std;
int main(){
bool isStudent;
int cups;
cout << "Are you a student (1 for yes and 0 for No) ?";
cin >> isStudent;
cout << "How many cups of tea have you purchased ?";
cin >> cups;
if (isStudent || cups > 15) {
cout << "You are elegible for a discount " << endl;
} else {
cout << "You are NOT elegible for a discount " << endl;
}
return 0;
}

Going through the code

  • The bool isStudent; line declares a variable named isStudent of type bool and does not assign a value to it.
  • The int cups; line declares a variable named cups of type int 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 the isStudent 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 the cups variable.
  • The if (isStudent || cups > 15) { line starts an if statement that checks if the isStudent variable is true or if the cups variable is greater than 15.
  • 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 an else block that is executed if the if 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 the if 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++.