C++ Programming: Code Examples, Tutorials, and Problem Solving

These sources offer a comprehensive introduction to C++ programming concepts for beginners. They cover fundamental topics like setting up the development environment, understanding variables, data types, operators, and control flow statements like ‘if-else,’ loops, and switch cases. The sources also discuss functions, including function overloading and recursion, illustrating how to build practical applications such as a calculator, a BMI calculator, and games. Additionally, there’s exploration of drawing shapes and the use of loops for generating multiplication tables, providing hands-on examples to reinforce learning. Finally, generics and templates are introduced to highlight programming best practices and how to improve code reusability.

C++ Fundamentals Study Guide

Quiz

1. What is the purpose of the ASCII table? The ASCII table provides a numeric representation for characters, allowing computers to distinguish between letters, numbers, and symbols by assigning each a unique numerical code. This enables computers to process and store text.

2. How many characters can ASCII represent, and why is this sometimes a limitation? Standard ASCII can represent 128 characters because it uses 7 bits per character. This is a limitation because it is sufficient for English characters but doesn’t include the characters needed to represent many other languages.

3. Explain the difference between char and int data types in the context of ASCII representation. A char data type stores a single character, while an int data type stores an integer number. In the context of ASCII, a char variable can hold a character, and an int variable can hold the corresponding ASCII code of that character, allowing conversions between them using casting operators.

4. What is a casting operator, and how is it used to convert between characters and their ASCII values? A casting operator is a way to explicitly convert one data type into another. In C++, you can use (int)character to get the ASCII value of a character or (char)number to get the character represented by an ASCII number.

5. Describe the purpose of the modulo operator (%) and give an example of its use. The modulo operator (%) returns the remainder of a division. For example, 5 % 2 equals 1, because 5 divided by 2 is 2 with a remainder of 1.

6. Explain how the modulo operator can be used to determine if a number is even or odd. If a number modulo 2 equals 0, the number is even; if it equals 1, the number is odd. This is because even numbers are perfectly divisible by 2, leaving no remainder, while odd numbers leave a remainder of 1.

7. What is the difference between the = operator and the == operator in C++? The = operator is the assignment operator, used to assign a value to a variable (e.g., x = 5). The == operator is the equality operator, used to compare two values for equality (e.g., x == 5).

8. What is the difference between pre-increment (++x) and post-increment (x++) operators? The pre-increment operator (++x) increments the variable’s value before it is used in the expression, while the post-increment operator (x++) increments the variable’s value after it has been used in the expression. For example, if x is 5, y = ++x will result in x being 6 and y being 6, but y = x++ will result in x being 6 and y being 5.

9. Name three relational operators in C++ and provide an example of how they are used. Three relational operators in C++ are:

  • < (less than): if (a < b)
  • > (greater than): if (x > y)
  • == (equal to): if (num == 10)

10. Explain the purpose of the break statement in a switch statement. The break statement terminates the execution of the current case in a switch statement and transfers control to the statement immediately following the switch block. Without a break statement, execution would “fall through” to the next case, even if its condition is not met.

Essay Questions

  1. Discuss the importance of understanding the underlying numeric representation of characters (like ASCII) when developing software applications. Give examples of situations where this knowledge could be crucial for solving a programming problem.
  2. Explain the concept of operator precedence in C++. Describe how different types of operators (arithmetic, relational, logical, assignment) interact within a complex expression, and how parentheses can be used to control the order of evaluation.
  3. Compare and contrast the while loop, the for loop, and the do-while loop in C++. Provide scenarios where one type of loop might be more appropriate than the others.
  4. Discuss the advantages of using functions in C++ programming. Explain how functions can improve code readability, reusability, and maintainability, and illustrate your points with examples.
  5. Explain the concept of object-oriented programming. Discuss the differences between classes and objects.

Glossary

  • ASCII: (American Standard Code for Information Interchange) A character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Each character is assigned a unique number.
  • Casting Operator: An operator that converts a value from one data type to another. For example, converting an int to a char.
  • Modulo Operator (%): An arithmetic operator that returns the remainder of a division.
  • Variable: A named storage location in a computer’s memory that can hold a value.
  • Data Type: A classification of data that tells the compiler how the programmer intends to use the data. Examples include int, char, float, and bool.
  • Operator Precedence: The order in which different operators are evaluated in a complex expression.
  • Loop: A programming construct that repeats a block of code multiple times.
  • While Loop: A control flow statement that executes a block of code repeatedly as long as a specified condition is true.
  • For Loop: A control flow statement for specifying iteration, which allows code to be executed repeatedly.
  • Do-While Loop: A control flow statement similar to a while loop, except that its condition is tested at the end of the loop body, ensuring that the loop body is always executed at least once.
  • Function: A block of organized, reusable code that is used to perform a single, related action.
  • Return Type: Specifies the data type of the value that a function returns after its execution.
  • Parameter: A value that is passed into a function when it is called.
  • Arguments: The actual values passed to a function when it is invoked.
  • Object-Oriented Programming: A programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.
  • Classes: A blueprint for creating objects. It defines the attributes and methods that objects of that class will have.
  • Objects: An instance of a class. It is a concrete entity that has attributes and can perform methods.
  • Pointers: A variable that stores the memory address of another variable.
  • Void Pointers: Pointers that can point to any data type. They are useful for generic programming.
  • Recursion: A method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

C++ Programming Fundamentals: A Comprehensive Guide

Okay, here’s a briefing document summarizing the key themes and ideas from the provided source “01.pdf”:

Briefing Document: C++ Programming Concepts

Overview:

The source material appears to be a C++ programming tutorial covering fundamental concepts, including ASCII representation of characters, casting, basic input/output, arithmetic operators, logical operators, assignment operators, conditional statements (if/else, switch), loops (while, for, do-while), functions (including recursion), pointers, and object-oriented programming principles. The material uses practical examples (e.g., building a cipher, determining even/odd numbers, calculating triangle types, drawing shapes) to illustrate these concepts.

Main Themes & Ideas:

  • ASCII and Character Representation:
  • Characters in C++ are represented by numerical codes using the ASCII standard. The source highlights the difference between uppercase and lowercase letters having distinct ASCII values.
  • Quote: “each character has its numeric pair inside ask key table… lowercase letter a is 97… capital letter A… 65… computer can distinguish between lowercase and uppercase letters.”
  • Casting operators can be used to convert between characters and their corresponding ASCII integer values.
  • Quote: “you can get an idea on how you can build something more useful than than just basic hello world application so you can for example build a program that ciphers words and sentences in a s key.”
  • Basic Input and Output:
  • std::cout is used for outputting text and variable values to the console.
  • std::cin is used for reading input from the user.
  • The tutorial demonstrates how to chain cout and cin operators to input or output multiple variables in a single line.
  • Arithmetic, Relational, Logical, and Assignment Operators:
  • The source covers various operators including modulo (%), increment/decrement (++, –), relational operators (<, >, ==, !=, <=, >=), logical operators (&&, ||, !), and assignment operators (=, +=, -=, *=, /=, %=).
  • It explains the difference between pre-increment/decrement and post-increment/decrement.
  • Quote: “…this operator here gives you the remainder of dividing these two numbers…” (referring to modulo).
  • Operator precedence is briefly touched upon: arithmetic > relational > logical.
  • Conditional Statements (if/else, switch):
  • if and else statements are used to execute different code blocks based on a condition.
  • The tutorial shows how to nest if/else statements.
  • switch statements are used for multi-way branching based on the value of a variable. The break statement is essential to prevent fall-through to subsequent case labels.
  • Quote: “this break keyword is really used so that our switch case statement knows where our first case ends.”
  • Loops (while, for, do-while):
  • while loops execute a block of code repeatedly as long as a condition is true. The source emphasizes the importance of ensuring the loop condition eventually becomes false to avoid infinite loops.
  • for loops provide a concise way to iterate a specific number of times. Nested for loops are used to create patterns and shapes.
  • do-while loops are similar to while loops, but the code block is executed at least once before the condition is checked.
  • Functions:
  • Functions are reusable blocks of code that perform a specific task. The source covers defining functions, passing arguments, and returning values.
  • Quote: “…this main function here is much more clean than it was before…” (referring to using functions to improve code organization).
  • Recursion: The tutorial explains recursion where a function calls itself. A base case is crucial to prevent infinite recursion and stack overflow errors.
  • Pointers:
  • Pointers are variables that store memory addresses.
  • The & operator is used to get the address of a variable.
  • The * operator is used to declare a pointer and to dereference a pointer (access the value at the stored address).
  • Pointers must be of the same type as the variable they point to (with the exception of void pointers).
  • Quote: “your pointer has to be of the same type like the variable that it is pointing to”.
  • Void Pointers: These can point to variables of any data type but must be cast to a specific type before dereferencing.
  • Quote: “a void pointer is a special type of pointer that can hold the address of a variable of any other data type”.
  • Object-Oriented Programming (OOP):
  • The source briefly introduces OOP, stating that it allows representing real-life objects with their attributes and behaviors.
  • Classes and objects are mentioned as fundamental concepts.

Illustrative Examples:

  • Cipher: Creating a program to convert words into ASCII codes and vice-versa.
  • Even/Odd Checker: Determining if a number is even or odd using the modulo operator.
  • Triangle Classifier: Determining if a triangle is equilateral, isosceles, or scalene based on side lengths.
  • Shape Drawing: Using nested loops to draw rectangle shapes.
  • Digit Counter: Counting the number of digits in a number using a while loop.
  • Pin Verification: A do-while loop is used to allow a user a limited number of attempts to enter a correct PIN.
  • Prime Number Checker: Creating a function to determine if a given number is a prime.
  • Summing Numbers with Recursion: Demonstrates a recursive function to sum a range of numbers.

Key Quotes:

  • “each character has its numeric pair inside ask key table… lowercase letter a is 97… capital letter A… 65… computer can distinguish between lowercase and uppercase letters.”
  • “…this operator here gives you the remainder of dividing these two numbers…” (referring to modulo).
  • “this break keyword is really used so that our switch case statement knows where our first case ends.”
  • “…this main function here is much more clean than it was before…” (referring to using functions to improve code organization).
  • “a void pointer is a special type of pointer that can hold the address of a variable of any other data type”.
  • “your pointer has to be of the same type like the variable that it is pointing to”.

Potential Use:

This document can serve as a quick reference or study guide for individuals learning C++ programming, covering many of the essential building blocks of the language.

Programming Concepts: Answering Common Questions

FAQ on Programming Concepts

  • What is the ASCII key table, and how is it used to represent characters in a computer?
  • The ASCII (American Standard Code for Information Interchange) key table is a standard that assigns a unique numeric value to each character, including letters (uppercase and lowercase), numbers, and some special symbols. This allows computers to store and process text by representing characters as numbers. For example, ‘a’ is represented as 97 and ‘A’ is represented as 65.
  • How can you convert between characters and their corresponding ASCII values in C++?
  • In C++, you can convert a character to its ASCII value by using a casting operator, like int(character). Conversely, you can convert an ASCII value back to its character representation by using char(numericValue).
  • What is the modulo operator (%) used for, and how can it be applied to determine if a number is even or odd?
  • The modulo operator (%) returns the remainder of a division. For example, 5 % 2 equals 1 because 5 divided by 2 is 2 with a remainder of 1. To check if a number is even or odd, you can use the modulo operator with 2. If number % 2 equals 0, the number is even; otherwise, it’s odd.
  • What are increment and decrement operators in C++, and what is the difference between pre-increment/decrement and post-increment/decrement?
  • Increment (++) and decrement (–) operators are used to increase or decrease the value of a variable by 1, respectively. Pre-increment/decrement modifies the value of the variable before it is used in the expression, while post-increment/decrement modifies the value after it is used. For example, if counter = 7, ++counter would result in counter being 8, and the expression would evaluate to 8. Whereas counter++ would also increment counter to 8, but the expression would evaluate to 7.
  • What are relational and logical operators in C++, and how are they used?
  • Relational operators (e.g., <, >, <=, >=, ==, !=) compare the values of two operands and return a Boolean value (true or false) based on the relationship. Logical operators (AND &&, OR ||, NOT !) combine two or more conditions into a single condition. Relational operators are used to compare numerical and string values, while logical operators are used for flow control.
  • What is the purpose of a switch statement, and how does the break keyword work within it?
  • A switch statement is a control structure that allows you to execute different blocks of code based on the value of a variable. The break keyword is crucial because it terminates the execution of a case within the switch statement. Without break, the code will “fall through” to the next case, potentially executing unintended code.
  • What are loops used for in programming, and how do while, do-while, and for loops differ in C++?
  • Loops are used to execute a block of code multiple times. while loops execute as long as a specified condition is true, checking the condition before each execution. do-while loops are similar to while loops, but they execute the block of code at least once, checking the condition at the end of each execution. for loops are designed for executing a block of code a specific number of times, typically using a counter variable that is initialized, checked against a condition, and incremented/decremented in each iteration.
  • What are functions, and how do they contribute to code reusability and modularity?
  • Functions are self-contained blocks of code designed to perform a specific task. They accept input (parameters), process it, and may return a result. Functions promote code reusability by allowing you to use the same block of code multiple times without rewriting it. They also contribute to modularity by breaking down complex problems into smaller, manageable parts, making the code easier to understand, test, and maintain.

Visual Studio: A Beginner’s Guide

Here’s what the sources say about Visual Studio:

  • Visual Studio 2019 is a tool used for C++ programming.
  • It is an IDE (integrated development environment), meaning it has many options, including ones that are useful for beginners.
  • The Community version can be downloaded and used for free, and there are many tutorials available on YouTube that explain how to download and install it.
  • When Visual Studio is opened, recent projects are displayed on the left.
  • On the right, there is a menu with multiple options: checking out code from GitHub, opening an existing project, and creating a new project.
  • To create a new project, the project type must be defined. For beginners, a simple C++ console application is recommended.
  • A new window will appear where the project name, location, and solution name can be defined.
  • The solution is a group of projects. Large applications can be divided into smaller projects within the same solution to make them easier to maintain.
  • Visual Studio’s Solution Explorer displays the solution name and the projects inside it. If the Solution Explorer is not visible, it can be opened by going to View and selecting Solution Explorer.
  • When a project is created, default folders and files are created. Source code files are placed in the source files folder.
  • The part of Visual Studio where you write code is a text editor with different colors to indicate the purpose or role of the code.
  • To run code, you can click the green play button.
  • It automatically builds code when started from Visual Studio.

C++ Data Types: Variables, Storage, and Overflow

Here’s what the sources say about data types:

  • A variable is a container for a certain data type. You can put a whole number, character, decimal point number, or a Boolean value inside it.
  • In C++, you cannot put two different data types inside one variable. It is also not possible to change a data type of variables once it is created.
  • Here are some of the most basic data types in C++:
  • int: can hold negative whole numbers, zero, and positive whole numbers.
  • char: can store one character.
  • bool: can store a true or false value.
  • float: can store decimal point numbers.
  • double: can also store decimal numbers, but it can hold much more data than a float. Double is double the size of float. Float takes four bytes, while double takes eight.
  • unsigned int: If you want to use only positive integer numbers, there is a data type for that called unsigned int. Its size is the same as of int (four bytes or 32 bits). However, since it is unsigned, it does not use one bit for sign because the sign is by default plus. It uses all 32 bits to represent value and those values go from zero to 4 billion and something.
  • Each data type takes a certain space in memory. To determine how much memory each takes, you can use the sizeof operator.
  • bool uses one byte.
  • char uses one byte.
  • float uses four bytes.
  • double uses eight bytes.
  • Each data type also has a maximum amount of data that they can hold. If you put too much data inside a variable, it is going to overflow. This is called data type overflow.
  • The int data type has reserved words for the minimum and maximum amounts.
  • int_min is the minimal amount for int in C++.
  • int_max is the maximum amount for int in C++.
  • The unsigned int data type also has a reserved word for the maximum amount.
  • uint_max
  • If you divide two whole numbers, the result will also be represented as a whole number. If you want to show the real result, you have to make one of them a float or double.
  • The modulo operator gives you the remainder of dividing two whole numbers.

Conditional Logic: Mastering If/Else Statements

Here’s what the sources say about the “if/else” statement:

  • The if statement is used when a block of code should be executed conditionally. This means the code will execute only in a certain situation.
  • An else block can be added, which will be executed if the initial if block is not executed.

An example task is to check if a number entered by a user is even or odd and write that out to the user.

Here’s how the “if/else” statement would be structured in code for this task:

  1. Prompt the user to enter a whole number.
  2. Use cin command to store the user’s input as an integer in a variable called number.
  3. Use the modulo operator to determine whether the number the user entered is even or odd. The modulo operator gives the remainder of dividing two whole numbers.
  4. Use the if statement to check whether the number modulo 2 is equal to 0. The == operator is used to check equality.
  5. If the result is zero, the program should output to the user that the number is even.
  6. If the result is not zero, the else statement is used to output to the user that the number is odd.

#include <iostream>

using namespace std;

int main() {

int number;

cout << “Please enter whole number” << endl;

cin >> number;

if (number % 2 == 0) {

cout << “You have entered even number” << endl;

} else {

cout << “You have entered odd number” << endl;

}

cout << “Thanks bye” << endl;

}

Complex “if/else” Statements

  • “If/else” statements can be more complex.
  • If you are not familiar with “if/else” statements, it is recommended to first watch a video explaining the basics before moving on to the more complex uses.

Consider a task where the user enters the lengths of a triangle’s sides (a, b, and c), and the program determines whether the triangle is equilateral, isosceles, or scalene:

  • An equilateral triangle has all three sides of the same length.
  • An isosceles triangle has two sides of the same length.
  • A scalene triangle has all three sides of different lengths.

Here’s how the “if/else” statement could be structured in code for this task:

  1. Declare three float variables: a, b, and c.
  2. Prompt the user to enter the side lengths of the triangle.
  3. Use cin command to store the user’s input in the variables a, b, and c.
  4. Use an if statement to check if a is equal to b AND b is equal to c. The && operator is a logical end operator. The expression will be true only if both sides are true.
  5. If the result is true, output to the user that the triangle is equilateral.
  6. If the result is false, use an else statement and nest another if/else statement.
  7. Check if a is not equal to b AND a is not equal to c AND b is not equal to c. The != operator means “not equal to”.
  8. If the result is true, output to the user that the triangle is scalene.
  9. If the result is false, output to the user that the triangle is isosceles.

#include <iostream>

using namespace std;

int main() {

float a, b, c;

cout << “Please enter a, b, and c” << endl;

cin >> a >> b >> c;

if (a == b && b == c) {

cout << “equilateral triangle” << endl;

} else {

if (a != b && a != c && b != c) {

cout << “scalene triangle” << endl;

} else {

cout << “isosceles triangle” << endl;

}

}

}

Understanding Loop Control Structures: For, While, and Do While

Here’s what the sources say about loop control:

  • There are three types of loops: for loop, while loop, and do while loop.
  • for loop: Before the loop starts, you should know how many iterations that loop is going to make.
  • The syntax of the for loop is for (initial value of counter; condition; increment/decrement).
  • Inside the parentheses, you put three expressions:
  • Set the initial value of the counter variable.
  • Put the condition that your for loop is going to check each time that it wants to run a block of code. Before each iteration, it is going to check that condition.
  • A way for your counter value to come to that condition. You have to either increase or decrease your counter variable in order to reach that condition at a certain point, and then when that condition results as false, your for loop is not going to be executed anymore.
  • After the for loop parentheses, you put curly brackets in which you write out the code that is going to be executed in each iteration of your for loop.
  • while loop: With a while loop, you first check the condition, and then if that condition results as true, you can really proceed with executing your block of code that comes after your while.
  • do while loop: With a do while loop, you first execute a block of code and then check the condition. If the condition results as true, you go back and do one more iteration.
  • The syntax of the do while loop is do {code block} while (condition);.
  • The code inside the do block is executed at least once, regardless of whether the condition is true or false.

Here’s an example of using the do while loop:

int pin, usersPin, errorCounter = 0;

do {

cout << “Pin: ” << endl;

cin >> pin;

if (pin != usersPin) {

errorCounter++;

}

} while (errorCounter < 3 && pin != usersPin);

  • break statement: The break keyword is used so that your switch case statement knows where your first case ends.
  • If you don’t put the break statement, you are going to get your program to behave unexpectedly.
  • When the program reaches the break keyword, the loop will stop.

Nested Loops

  • Loops can be nested, meaning you can put a loop inside another loop.
  • An example of using nested loops is drawing a rectangle shape.
  • The outer loop controls the height of the rectangle.
  • The inner loop controls the width of the rectangle.

C++ Function Guide: Creation, Parameters, and Overloading

Here’s what the sources say about function creation:

  • A function is a block of code that is grouped together and performs a specific task. The code in the function is executed only when the function is called or invoked.
  • Every C++ program contains at least one function, the main function. The execution of the program begins on the first line of the main function and ends on the last line, or when a return statement is encountered.
  • Besides the main function, you can create your own functions.
  • Functions make your code reusable. You can write code to solve a problem once, put the code in a function, and then call the function many times.
  • Each function should perform a specific task. This makes the code easier to manage.

How to create a function:

  1. Specify the return type of the function. If the function does not return a value, use the void return type.
  2. Write the name of the function.
  3. In parentheses, list any arguments or parameters that the function receives. The parentheses can be left empty if the function does not receive any arguments.
  4. In curly brackets, write the body of the function. This is the code that will be executed when the function is called.

Here is an example:

void function() {

std::cout << “hello from function” << std::endl;

}

Function Declaration and Definition

  • The recommended way to create functions is to separate the declaration and the definition.
  • The declaration of a function tells the compiler the return type, name, and parameters of the function. The function declaration goes before the main function.
  • The definition of a function is the actual code that will be executed when the function is called. The definition goes after the main function.
  • To navigate to a function’s definition, right-click the name of the function and select “go to definition,” or press F12.

Here is an example:

#include <iostream>

//Function declaration

void myFunction();

int main() {

myFunction();

return 0;

}

//Function definition

void myFunction() {

std::cout << “Function was executed!” << std::endl;

}

Function Parameters

  • Parameters are values that are passed to a function. Sometimes a function needs to receive a certain value or variable in order to perform a task.
  • To create a function that receives a parameter, specify the type and name of the parameter in the function’s parentheses.

Here is an example:

void introduceMe(std::string name)

{

std::cout << “My name is ” << name << std::endl;

}

  • To pass multiple arguments to a function, use a comma to separate the arguments in the function’s parentheses.

Here is an example:

void introduceMe(std::string name, std::string city, int age)

{

std::cout << “My name is ” << name << std::endl;

std::cout << “I am from ” << city << std::endl;

std::cout << “I am ” << age << ” years old” <<std::endl;

}

  • A default parameter is a parameter that has a default value. If a value is not passed for that parameter when the function is called, the default value will be used. To specify a default parameter, assign a value to the parameter in the function definition.

Here is an example:

void introduceMe(std::string name, std::string city, int age=0)

{

std::cout << “My name is ” << name << std::endl;

std::cout << “I am from ” << city << std::endl;

if (age != 0){

std::cout << “I am ” << age << ” years old” <<std::endl;

}

}

  • Default arguments can only be specified at the end of the parameter list.

Function Return Statements

  • The return type of a function can be any data type, including int, float, bool, char, or double. It can also be void, which means that the function does not return anything.
  • A function can return only one value. Only one return statement inside a function will be executed.
  • Not all functions need to return a value. An example of a function that does not return a value is a function that writes a menu to the user.
  • Functions that return a value can perform a calculation on arguments passed to the function and then return the result of that calculation.

Function Overloading

  • Function overloading means that you can create multiple functions with the same name, but with different parameters.
  • When functions do the same thing but use different data types, you can use the same name as long as the parameters are different.
  • When a function is called, the compiler decides which function to call based on the data types of the arguments that are passed to it.
C++ FULL COURSE For Beginners (Learn C++ in 10 hours)

The Original Text

hi everyone welcome to my channel my name is Salina I am a software engineer and I make programming and it related videos here on YouTube so if that is something that is of interest to you consider subscribing to my channel and this video is going to be allinone C++ full course for beginners and the goal of this video is to take you from that place where you are struggling with programming where you don’t understand how to read code or write code to a place where where you feel really comfortable writing code and reading code and you really understand what the code is doing because that is the most important part so this course consists of lectures where I will teach you a specific concept and then uh there will be a task and by solving that task you will get a good understanding of that specific concept and then the complexity of those tasks will gradually increase so that at the end of this video If you finish it you will have have a very good understanding of the most important programming Concepts not only in C++ but you will be able to take that knowledge with you to other programming languages as well and then uh at the end after you finish this course in the description I will leave directions for you on what should be your next step because I have created a road map with steps and directions so that if you follow those you will be able to build your programming knowledge to a very very high level but before we start I want to ask you one question and I feel extremely proud and happy that you decided to invest your time to learn programming and I can personally give you many reasons why that is a great decision but what I’m more interested in is what are your reasons why would you like to learn programming and be good at it is it because you are very creative person or you want to get a job and earn a lot of money or you want to gain those problem solving skills or because you think that programming is cool um so any other reason that you might have as well please write that in the comments down below and then uh make a public commitment before you start because that is something that will motivate you so um write I will learn programming because and then put your reason and hold yourself accountable so I am very much looking forward to reading your answer in the comments down below so without further Ado I will just roll the intro and let’s start with the [Music] video hi guys very warm welcome to everyone watching this video and this video is going to be introduction to C++ programming language and the first video of this course I was planning on dividing this video into two separate parts so that in the first part I’m going to explain some basic concepts some general concepts that are related to C++ and that you need to understand in my opinion first before starting to write code and then in the second part we are going to build our first C++ program so I’m going to include in the description of this video a time stamp so that you can navigate yourself to the coding part if you want but if you’re an absolute beginner I would recommend you to watch the entire video Let’s explain a couple of Concepts before we actually start to write codes so that you are familiar with them you may wonder what kind of applications you can build with C++ well C++ is general purpose language that means that it can be used to build all type of applications so uh it is very close to the hardware and that means that it can manipulate resources easily so you can build operating systems if you want with it you can build compilers browsers uh demanding games so all general purpose software can be built with C++ uh it is not specific for only one domain but it can be used to solve and build different types of applications uh after that I would like to mention that C++ is compiled language that means that the code that you write first has to be compiled or uh converted translated into another language that is understandable to your machine to your computer so that phase is called compiling code or building code there are certain errors that can occur in this phase of course and those are compile time errors uh this type of Errors is usually the most common among beginners among people who are just starting to learn C++ and we are going to demonstrate some of those compile time errors when we start uh coding after that C++ is case sensitive language that means that uh C++ distinguishes between lowercase and uppercase letters so if you have for example a variable all up uppercase and then a variable all lowercase so the same meaning the same word those two are going to be treated and and understood differently as different variables uh by C++ so it is case sensitive language why C++ was created another question because before it there were other programming languages uh well yes they were uh and it was created in 1980s uh and it was created with two programming languages in mind one of those two was simula uh and the other one was C so simula at the time was great language for human way of understanding and representing things so it was very understandable and easy to use to humans but at the same time it was very very slow so uh they decided to combine it with another programming language a fast one and at that time the best choice for that was C programming language so they combined did the two and they got C++ uh C++ is very very uh understandable language very easy to use by humans so you can build and maintain uh huge projects with C++ uh but at the same time because of this C part uh it is uh very close to the hardware so uh demanding tasks can be executed fast uh and also well since since it was created uh from C programming language you can say that any valid uh C program is actually valid C++ program so if you’ve ever built a c program you have actually built a C++ program already as I have already said the code that you write is not directly understandable to your machine so first it has to be converted into another language that is understandable to your computer but the one the language that is understandable to you is called source code I am going to put here an example of source code uh and I’m also one wondering at this moment what do you think that this code does so before we started any programming any coding at all what do you think that the result of this program is going to be so just to check how understandable C++ really is even for someone who has not have a lot of experience in coding so write in the comments of the video what you think that this code is going to do and I’m going to check those so at the same time you’re going to be doing uh something called writing pseudo code which means describing what a program does but just in plain English so no C++ so this is uh the source code after that that source code is transferred or uh translated into another form another language which is understandable to your computer and that is object code that object code is usually in the form of binary numbers so ones and zeros and that is the only language that your computer actually understand and this is an example of your uh machine language I’m also not going to ask anyone to describe what this here does because this is highly unreadable and cannot be understood by humans but uh as I said it is the only language that your machine understands so algorithm what is algorithm algorithm is nothing more than a recipe so steps instructions that you write to your computer list of ingredients and and steps that your computer needs to do in order to perform a certain task that’s algorithm now that we have understood these Concepts let’s switch to more practical part so the tool that we are going to use is called Visual Studio 2019 and there is a lot of tutorials on YouTube on how you can download and install the visual studio and you can also use Community version for free so uh when we open Visual Studio you can see that here in this left part we have our recent projects uh so the project that that I have recently created and this may be empty if you haven’t used your visual studio so on the other side there is a menu with multiple options you have uh the option to check out your code from GitHub here you can open existing project from your computer uh and continue working on that but the option that we are going to use is called create new project so we are going to click that so this new window now here appears and we are going to Define what kind of project we want as beginners we are going to build a very simple app so we are going to build a C++ console application so you can type in a C++ and make sure to select C++ console application so not c not Visual Basic but C++ and when you clicked it you can now uh click uh when you selected it you can now click next so this new window uh now we get a new window uh where we have to Define our project name location where our project is going to be saved and and our solution name as default our solution is named the same as our project what solution is is just actually a group of project so if you had a huge application and you wanted to divide it in smaller parts so that it is easier to maintain you could divide it in multiple projects but all of those projects would be part of the same solution so as we are since we are building a simple app we have only one project and we will name it my first app so my my first app and location for our project we can either enter manually or we can do it by clicking this button here and I want my f files to be saved on desktop so I’m going to select uh desktop and now we are done with uh with creating our project so uh with configuring our project so we can click uh create so the tool that we are going to use uh Visual Studio is an IDE or integrated development environment that means that it has a lot more options than just those that we as beginners are going to need and use so for now I’m going to explain only the most basic ones the ones that we are going to need as beginners but as we progress along the way I’m going to explain the new features that we’ll use so so the first thing that you can see here is our solution Explorer so uh if you cannot see your solution Explorer you can click here to this View and select solution Explorer and that should bring your solution Explorer back to view so as we said our solution Explorer is named my first app and it has one project inside it so this this this project is this one here and as you can see when we created our project it was created with some default folders and files for now we are going to use only one file so you can delete these folders header files and resource files if you want just click right click and click delete so now this source files folder is where we are going to put our source code files if you remember from before I have said that uh source code is the code that we write and that is understandable to us so this is our source code file so this with CPP extension so when we created our project this file was created by default and if you double click it it opens uh here so this part here is nothing more than a than a text editor really as any text editor it just has these uh different colors depending on the purpose or the role of the code so that you can manage the code easier if it were all the same color it would be harder to uh to read so for example this green part here is a comment comment is just a note for yourself so compiler will not try to translate commented lines so if if you type for example here this slash slash or that means actually begin comment and then you can explain for example what this preceding code does so you can write this will output to the console hello world so this will output to the console hello world so that when you for example come tomorrow to this code and you have forgotten what this code here does this will remind you this comment here and these are some default comments which I will delete now we are not going to uh need this so just select everything and click delete as you would in any regular text editor this part here also now let’s explain what we got here so this include iio stream is actually a header file so it is also called a library and this um I stream is input output Stream So it allows our uh us to input and output data to and from our program for example thanks to this we can use commands such as this here so see out or console output so without having this we would actually have to program ourselves how we will input and output data in uh our in our console so we will not delete this line here now check this code here it says main well that means that this is our main function so a main procedure that our program is going to execute so whenever whatever you put actually in uh these braces here your program will execute and the execution of your program begins here and it ends here so since this here is the the scope of your main function so you can collapse it by clicking here and you see that everything is collapsed now so for now it’s enough for you to know that this is our main method our main procedure and later in this course we are going to uh learn and understand what methods are used for what is their purpose and we are also going to create a lot of Our Own methods so now we have this uh C out or console output which is the command to Output to the console and then we have also combined with this um STD and these redirection signs so STD is the Nam space for this C out so that’s how your computer knows what this SE out really is I will show you later how you can get rid of this if you really want and we also have this hello world in uh quotation marks okay let’s see what we have learned so far we said that this is our solution it inside it has one project and within that project we have this file with uh with extension. CPP which is our source code so if we double click this file it brings us here so to this text editor and this is our program this is the code of our program now how we can run this code well see this green play button here well that’s our start button it’s really uh it’s really intuitive so if I click it we will get our hello world text here which I have outputed to the console uh and you can also see that we we have besides our hello world we have gotten some junk text here so what I’m going to do is show you how you can get rid of this in a moment so let’s close our program for a moment and what you need to do in order to get rid of this junk text is type this command here so system pause greater than zero in quotation mark and the end of command you notice that at each of uh at the end of each command uh you have to put this semic column and if you didn’t it would result in an compiler error and I will demonstrate that let’s run our program now and you will see that our junk text has disappeared so as I said after each command you have to put this semic column and if you if you didn’t do that it would create a syntatic error and that then your your compiler would not be able to understand and your program would not run actually your program would not start so if I delete this semicolon here you see that something has changed in this in this window here so you see this R error error red sign um well if I hover over it you see you see it here also here here uh it says expected a semicolon which is very clearly says that you are missing a semicolon and if you see this red underline here it says also expected a semic colum so uh before this you are missing a semic column here so if I add it here if I add it here our error will disappear so if you try to run your program before fixing this error it would prompt you with with a window like this so it says you have one failed project and zero succeeded projects so this is the code building that I have mentioned before so this one project which is actually the only project that we have has failed building so your compiler does not actually understand what you have written since you are missing a semi uh column so another thing how you could know that you have syntax error uh except from this uh this prompt window that we got is this uh syntax error so missing semic column before this closed brace and if you click it here it will take you right where you need to go to fix your error so it’s it’s very useful and if I press my play button again it now says hello world so I will stop program now uh you can do it either by clicking this red sign here which says stop debugging or you can click this X on the console window so if some other error has happened to you which didn’t happen to me here you can write that in the comments down below and I will gladly help you also someone else can help you from uh from the other people learning from this video and I would highly encourage that since in my opinion the best way to learn something is to try to reproduce what you’ve learned and to explain it to someone else so write in the comments if you got some other error now let’s see how we can upgrade our program so let’s write something else let’s add for example STD two column signs console out so uh under quotation marks I will put something that I want to write out so let’s say for example my name is Salina you can put your name of course and at the end of each command you put semicolon and let’s add one more so let’s say today is Friday and also add end of command here so it should write when we run our program it should write these sentences in uh this order here so you can run your program and if you can notice it has this new line in the first sentence so this is because of this this back slash n so that puts whatever text is coming after in a new line so you can put uh that text in a new line by either entering a back sln or with adding this STD end line so STD end line and I have previously stopped my program if I run it again you see that that we have gotten a new line here you see also that this um STD here is becoming a bit tedious to write again and again and I’ve told you that I’m going to show you how you can get rid of this uh so what you can do is you can uh put this STD on a global level so you can say using using namespace STD using namespace STD and what will that do is reference this STD namespace on a global level so you don’t have to uh reference it every time that you are using it and now we can delete all of these so wherever you have written STD that’s going to be referenced here now and you don’t need it need to write it separately and if we run our program uh here we are going to get uh our result so we have also uh this new line added by either sln or this end line here okay so in this video we have learned how we can output data in a console and in the next video we are going to see how we can input data into our uh console so we can ask user to input data and we are also going to talk about variables and data types another thing that I want to show you before I finish this video is you may be wondering okay do I have to start Visual Studio every time that I want to run my program is there any other way for me to to execute my program uh and uh the answer is no you don’t have to start Visual Studio every time there is another way to start your program for for w from Windows so let’s save all changes that we have made here so far so you can press control+ s or um if you have made any changes you see this star here so it is indicating indicating that you have unsaved changes so you can right click on that and click save and you see that that start disappears now so let’s find a folder where we have created our project for me that was desktop uh if you follow this tutorial from the beginning you created it also and if you follow me you created it also on desktop uh so you can open your folder you can uh go to debug and inside that you uh see this file here it says application so this is your program so uh if you right click on that and you check its properties you will see that it says exe that means that it is executable file so that is your program that is your um application and if you double click on that well you see that it runs same as in visual studio so you can start your program from Windows actually if you want to show it to your friends or someone else you don’t have to you don’t have to open Visual Studio each time that you want to uh to run your program but one more thing to keep in mind is that if I go to visual studio now and I add one more line let’s say let’s say we write out have a nice day so and and if I save it so contrl s and if I try to open my program from Windows now what you’ll see now is that this new line that I have just added is missing but why is that so you know well um if you remember when we were talking about building code we said that that’s the phase of uh converting source code that you have written into a code that your machine understands and when you start your code from Visual Studio it does that process automatically for you so it builds your code and then it executes your your program but when you are starting your uh program from uh Windows it is missing that phase of building code so your uh recent changes your last changes were not translated so your uh computer has only the only translated version that it has is the previous one so without about your uh recently added changes what you need to do is you either have to run uh your code from Visual Studio first or it’s enough for you just to build that code inside visual studio and what happens when you do that so just click here and you can click build and what happens is your Visual Studio translates this code that you have written and it saves it here and now when you run it and when your windows runs it it has uh the the latest version it has the latest changes so this is the small trick that I wanted to uh show you so uh this will be all for this video and uh if you came across to some errors that didn’t happen here to me or you have any questions you can write those in the comments and I will I will answer those and if you watched so far you can like this video you can subscribe and share it with someone else who would also like to learn programming and I will see you in the next video hi guys I hope you are all well and welcome to the second video of C++ for beginners in this video we are going to talk about variables data types and also I’m going to explain how you can enter data in your program and how that data is encoded into numbers using something called asky table so here I have opened visual studio and if you are not familiar with Visual Studio or there is some code that you see here but you don’t understand it I would recommend you to watch my first video of this course the link is going to be in the description of this video and then come back to this video so let’s talk about variables now what is variable well I like to explain it a real life example for example you see this glass here well what I can say about this glass is that it is a container for a certain type of liquid so it can hold water milk tea juice whatever you want really and the same way a variable is a container but for a certain data type so you can put whole number inside it you can put character or a decimal point number or a Boolean value meaning true or false value so they are the same in this aspect another thing that I can say about this glass is that it defines a maximum amount of liquid that it can hold so it has a max limit and if you put too much it will overflow the same way with variables variables also Define a maximum amount of data that they can hold for example if you have a whole number variable well there is a Max number limit for that variable that it can hold and also if you put too much it is going to overflow it is literally called data type overflow so it is a container the same as variable and also it defines a maximum amount of data that it can hold but how much data you really put inside that variable so the same way with with the water inside this glass if you decide to put 1/3 or 1/2 or 2/3 of this glass the same way is with variables you can put however much you want as long as you don’t put too much because then it’s going to overflow one thing that is different though between variables and this glass is that you can put one day inside this glass you can put water the next day you can put juice the next day you can put milk then tea or even you can mix two liquids but with variables you cannot do that at least not with Once in C Plus+ there are certain languages that you can do that with and there is also advantages and disadvantages when it comes to that but in C++ you cannot put two different data types inside one variables and you can also not change a data type of variables once you create it so let me show you what I’m talking about on a practical example let’s say that you want to create a variable that is going to hold your annual salary well that variable is definitely going to be a decimal point number or also floating Point number that’s how it’s called in C++ and the type that is used for that is called float so I’m going to say float and then I’m going to give a name to that variable since we said that is going to hold a annual salary we are going to call it annual salary okay and then we are going to assign it a value and that value is going to be let’s say 50,000 99 so now what I have done here is I have created a variable called annual salary I have said that that variable is going to be of type floating Point number and then I have put inside that variable 5099 so now let’s see how we can base on this variable based on our annual salary calculate how much our user is earning every month so what we are going to do is we are going to create another variable we are going to call it monthly salary and we are going to assign that variable a result of dividing this annual salary with 12 months so we are creating a variable of type float also since the result of division usually can be also floating Point number or decimal number and because of that we are using also float type for our monthly salary variable so I’m creating monthly salary variable and I’m going to say that that variable is going to hold result of this expression annual salary divided by 12 months and now we have result of this division inside this variable and what we are left to do is to write that result out to our user so I’m going to say see out since that’s the command that we use to Output data to console and I’m going to say your monthly salary is and I’m going to say monthly salary so one thing to notice here is that when you are writing out strings or when you’re writing out data like this you use quotation marks but when you want to write out the value that you are holding inside a variable well then you just put the name of your variable you don’t use any quotation marks then and now we can run our program and let’s see what’s the result that we got you see here your monthly salary is 4,166 75 so if you want to check that out you can uh you can calculate it in your calculator one thing that uh that bothers me in this program is that we have hardcoded this value here so we have hardcoded coded this 50,000 99 not every everyone’s salary is 50,9 and what if we wanted our user to enter what his salary is during the execution of our of our program so when he runs our program our program should ask him what is your annual salary and then he enters that salary and then we calculate his monthly salary based on the number that he has entered so I would like to upgrade this program now to work like I just explained and how we are going to do that well first I’m going to delete these 50,000 99 okay and then we are left with this situation here so it says float annual salary that means that I have just created a variable called annual salary and I have said I’m going to put a floating Point number inside that variable but I have not assigned any value to it yet so here we have a container for our annual salary and that container doesn’t hold any value and if we try to run our program now what do you think that we are going to get write that in the comments before I run this program and now I’m going to uh so pause the video and think about it and then write write your answer in the comments and now I’m going to run the the program and you can see that we have some build errors as this here uh this window here says so let’s see if we open this part here it says we have one error and that error is uninitialized local variable annual salary used what that means is that this exess expression here if you see this green underline It also says using uninitialized memory annual salary well that means that this expression here our computer doesn’t know how to do this expression since we haven’t assigned any value to our annual salary variable so it doesn’t know how to divide that that with 12 since there is no value inside that container inside that variable so this causes a compiler error and whoever has written that compiler error is going to occure he was right so how we have to what we have to do to correct this is we have to ask our user to enter his uh annual salary so what we are going to do now is let’s see First Command that we use to Output data from our program into our console is C out so console output and the command that we use to input data is console input or C in so I’m going to say C in and then we use these other redirection signs and then we say what is the variable that we want to store what our user has entered and I’m going to say annual salary so what I have said here is our user is going to console input the data and then whatever he has inputed you have to redirect that inside our annual salary variable so now if I run my program now you see that our program runs now so there is no compile time error and also our cursor is blinking well it is blinking because it expects here our user to enter annual salary but this is not really clear to someone who has not made this program so what we have to do if I enter some value now well it is going to calculate it correctly but this is not really user friendly so what we have to do is we have to write a message to our user before he enters anything so that he knows what he should enter so before this C in command I’m going to say C out and I’m going to say please enter your annual salary okay and now if I run my program you are going to see please enter your annual salary and the cursor is blinking waiting for you to enter the value and if I I say for example 40,000 you see that your monthly salary is 33333 so the program has calculated your monthly salary and the number is based on whatever our user decides to input so we have upgraded our program and there is also one more thing that I would like to show you when we are doing this example and that is let’s say that we want to calculate how much money our user is going to earn in 10 years so let’s say see out in 10 years you will earn and now we have to put a value of how much money he is going to earn and if you look at this situation here we first created our monthly salary variable and we then assigned it a value of annual salary divided by 12 but there is another way that we can write out results of Expressions without creating any variables if that is what we need and that is just here so in this line in 10 years you will earn you add these redirection signs and then you say expression that you want your computer to calculate and what we want to calculate is how how much our user is going to earn in 10 years so that is annual salary times 10 and if you do it this way so if you don’t decide to create a variable then you will not be able to use result of this expression later in your program so you don’t have a container that holds this 10 year salary but you have a container that holds annual salary and monthly salary so each time that you want to to write out or do something with your 10year salary you will have to write this expression while if you want to do something with your monthly salary you have a container that is holding that value directly for you and you can access it so if we run our program now you see it expects us to enter our annual salary let’s say 50,000 and you see here your monthly salary is 4,166 67 and then in 10 years you will earn what is this half a million so okay one thing that we are missing here then and that you can add is end line or back slash end so you can add it here and your program is going to format your text nicely so this line should go here like this if I if I add here one more end line and if I run my program again you see enter 50,000 and now we have our text formatted nicely because of this end line that I have just added let’s now create another variable and this time we are going to create a variable of type character so Char that is the type that is used for character variables in C++ it’s called Char and let’s name our variable character and when you want to assign a value to a character variable in C++ what you use is these single quotes like this if you see this example here when we are writing out text so string of text we use these double quotation marks but when we want to assign a character value of a character variable we use these single quotations like here and there are also some more rules when it comes to naming your variables and the most important one in my opinion and the most important one to make it easier to you is to name your variables according to their purpose so give meaningful names to your variables that is really really important for example you see this uh monthly salary variable here we could have as well named this variable blah blah blah and copy it here also because we are having compiler error right now because our compiler does not know what this variable really is you see it says identifier is undefined because you have not created this variable here because it supposes that it is a variable but it has not been created before so it does not recognize what this variable is so we have to copy the new name here also and now our compile time error disappears and this here this name does not represent anything to your computer so your computer is now going to run your program and your program is going to work just the same as it did before but this is really making it hard for you to understand what your code is doing and especially if you come to this for example in 10 days or someone else tries to understand your PR program this is really hard to read and he will just try to uncover really what your program is doing without understanding it because you have used these blah blah blah names for your variables the same situation would be if you for example have three boxes inside your kitchen and you you use one for storing sugar and flour and salt and instead of naming those sugar flour salt respectively you decide to name them Red Box green box and blue box and then in someone else comes to your house someone else comes to your program he does not really know where to find sugar because red does not mean that that box is storing sugar so you should name that box sugar box because imagine have three is easily easy to remember but imagine having 10 boxes or 10 variables or a hundred of them or even a thousand which is very common inside large programs so what you should do and I am going to do is return that previous name so monthly salary and here also so you should give meaningful names to your variables that is very important and that is the first rule the second rule is that well the second and third and the fourth are the rules that you will have to obey in most cases because if you don’t then you are just going to get a compile time error in 99% of the cases let’s say and I’m not going to say what that 1% is because I want you to obey these rules so when it comes naming your variables another thing is that your variable names cannot contain any special characters except underscore so like this this here is valid name to your variable but if you try for example to name your your variable like this so you say plus character you see that here in this error window it says expected an identifier so you cannot use this to name your variable or this you cannot say character with this minus inside it so character minus um so these signs these special signs are used for something else they are operators inside C++ and and you are not supposed to use those inside your variable names if you really want to use some special signs what you should and can use is underscore like this so this is the only sign that is allowed and that I recommend for you to use for naming your variables that’s the second rule the third rule is that your variable names cannot begin with numbers like this so you cannot say eight character for example It also says expected an identifier but what you can do is you can say for example character 8 that is a valid name so a number can be on any other position except on the first position so this is a valid name for a variable so you can put also number here so you can say for example C12 eror 8 but again what does that really mean me that is not a meaningful name for a variable so again we come to the first rule so you should name your variables according to their purpose so I’m going to return this to character name again and then we have our third rule I guess which is you cannot use space inside your variable names so for example this this is not valid and your compiler says it here so it gives you an error it does not know what this really is so you cannot use Space Blank spaces inside your variable names because you are going to get compile time error and if I try to do that and I try to run my program I get the window so it says you cannot run this program you have syntatic errors what you should do is you should correct those errors and then you can really run your program like this this okay so let’s repeat these rules one more time the first rule is that you should give meaningful names to your variables so name them according to their purpose the second rule is that your variables cannot contain any special characters except underscore the third rule is that your variables cannot start with numbers you can put number on any other position but not on the first one and third rule is that and the fourth rule actually is that your variables cannot contain blank spaces so empty spaces no no in a variable name what you should do is you should name your variables according to their purpose so meaningfully and you should also use something called camel casing what is camel casing you see how I have named this annual salary here you see that the first letter of my variable name is small letter and then the next word starts with capital letter and that is called camel casing so for example you would name your variable like this first second third so like this you see that this first second third these are words inside your variable name and this F is lowercase letter that’s the start letter of your variable and then each next word begins with capital letter and all the rest is lowercase letters that is called camel casing and that is something that is usually used with C++ let’s talk about data types now I am going to delete this code so if you need this code for a bit longer you can pause the video here so what kind of data can we store in our variables Well for now we are going to cover only the most basic ones the ones that we are going to use frequently and you should know that there are a a lot more data types besides from these that we are going to mention here a more complex ones that we are going to cover later in these videos in this tutorial so the first one that I would like to mention is a whole number data type so integer a data type that can hold negative whole numbers zero and positive whole numbers so the type that is used for that and C++ is called int and good example example for INT variable is let’s say year of birth so that is an example of a whole number variable and let’s say that that variable is going to hold 1995 okay so that would be an integer variable after that we have already seen this so it is a character variable so let’s say Char and an example of a Char variable could be gender for example let’s call it gender and you can store f for female or M for male for example so the Char variable can store one character then the next one is Boolean value meaning true or false value this is the simplest one and the type that is used to store bullion is called bull and a good example for a bull value could be for example if you want to hold in your program a variable that tells you whether your user is older or younger than 18 years 18 years old so let’s call our variable is older than 18 and we can assign to that variable either value of true or false so I’m going to put true true here another very able another data type is decimal point number also called floating Point number which we have mentioned before and let’s create a variable of type float and let’s say that that variable is going to to hold our average grade so average grade and uh this average grade can also be a whole number but since it has potential to be also floating Point number or a decimal point number we are going to to use a float data type for this average grade and another data type that I would like to mention for also storing decimal numbers is called double and the difference between double and Float is that double is double size of float so if you remember when we talked previously about uh Tak memory space that that varibles take so float is taking four bytes and double is taking double of that so eight bytes so you can put much more data inside your double data type than inside float and let’s say for example that if you had a credit card and you had a lot of money a lot a lot a lot of money and you want to store that value inside a variable you would use a double data type so let’s say double balance if equals to this large number here which I’m not going to even try to read okay so these are the most common data types that we are going to use in this tutorial I have said earlier that each data type takes a certain space in memory but let’s see exactly how much each of these take for that we are going to use something called size of operator and let’s see how we use it so C out size of int is and then we write this size off so size of and in parenthesis we pass to it data type that we want to check how much memory it is using so int and if I run my program now you see that it says that int takes four bytes of memory and if you’ve ever actually studied binary algebra you would know that four bytes of memory is actually 32 bits since each bite has eight bits and as we said our int can store both positive and negative numbers and zero also remember that so the first of these 30 two bits is used to determine whether the number has plus sign or minus sign so whether it’s a positive or A negative number and then remaining 31 bits are used for this value itself so let’s check what is min value for our int let’s write that out let’s write C out int Min value is and we have reserved word for minimal amount for INT in C++ and that is intore min and if I run my program now you see that it says this minus 2 billion extremely hard to read this number but you see that this is the minimal amount of int so this number is actually 2 to ^ of 31 let’s just quickly write these so -1 -2 -3 and then minus this number here so this number as I said is 2 to the power of 31 why not 32 because this one bite is used to represent this minus prefix now we should check a positive number range so we also can write write int max value is and then there is another keyword in C++ for INT Max which is intore Max and are you expecting to see this same interval but just with plus sign so write in the comments what are you expecting to see and if I run my program you see that that did not happen and if some of you know why it did not happen I would also like to to hear what you think why we have this number here and then this number here for our negative and for our positive range so different ending number and I’m going to explain why because this positive range here we have to include zero also so in order to include one number here just let’s just copy this and paste it so our positive range is going to from one up to let’s say that it is this number but since it is including zero also in order to include one number here so zero you have to take one number here so this here is not eight anymore but is seven so that is why we got let’s say different number for our positive and negative range because this here includes zero also one thing that I have to mention though is that if you want to use only positive integer numbers there is a data type for that as well and data type that is used for that is called unsigned int and its size is the same as of int so four bytes or 32 bits but since it is unsigned that means that it does not use one bit for sign because the sign is by default plus but it uses all 32 bits to represent value and those values go from zero to let’s see exactly how big of a number 4 billion and something so see out size of unsigned int is and let’s see size of unsigned int this is the data type and let’s also write out what is the max value of unsigned int so let’s say C out u in max value is and there is there is also reserved War for that and that is U inore Max so if I run my program now you see that we got 4 billion and something number so double the size of this and this is actually 2 to ^ of 32 but minus one because we are also including zero in this range so let’s check these remaining data types I’m going to write those out quickly and I will be back so I have written here the rest of these data types and as you can see since our bull is only true or false value one bite is enough for bull our Char is using also one bite float is using four bytes double is using eight and I’m also going to include a table with some more data types which we haven’t mentioned here so if you need to check those out please pause the video one thing that I promis to show you is data type overflow let’s see what that is so let’s create a variable of type int and call it int Max and let’s say that I want to store inside that variable maximum amount of int So intore Max and I’m going to write that variable out now just see how big of value that really is so see out int Max and if I run my program now you see that I have gotten here this 2 billion very large number actually so that is the max amount of int now my question for you is what is going to happen in this situation so see out int Max + one what is going to be the result of our program now if I run it so some of you may know this already some of you may not but I am very interested in reading your answers so make sure to write your answers before watching my explanation on this write those in the comments down below and if I run my program now well our number has not increased by one but it has went to the minimal amount of int and why has that happened well that is the data type overflow that I was talking about and how this works actually is it works the same as your clock works so it goes 9 10 11 12 and then from 12 since that is the max value on your clock it does not go to 13 but it goes back to Min so 1 12 and then 1 and that is applicable here it went from the max value to Min value just by adding one and that is called data type overflow I already can see that this video is pretty pretty long and I’m currently having problems uploading very long videos to YouTube so I’m going to leave for the next video explanation of asky table and we are also going to do some more examples and practice C++ some more so make sure to subscribe to my channel and also click the Bell icon so that you are first to be notified when I upload my next video and also like it and share it with anyone who do you think could benefit from this video and I will see you in the next one hi guys welcome to my channel as I promised in my previous video in this video I’m going to explain the concept of asky table and then I’m going to give you an idea an example of a program that is very beginner friendly very simple but also very fun and that you can build only with knowledge from my two previous courses and this one so if you haven’t watched those make sure to watch them link is going to be somewhere here and also in the description and also make sure to like this video and to subscribe to my channel for more educational content and let’s begin now so what is aski well aski stands for American Standard code for information interchange and it was first created in order to standardize the way of representing characters in your computer so in asky table those characters are represented as numbers numeric codes and what this means is following let me give you an example when I first learned to read and write so when I was a little I decided to together with my friend create uh our special symbol language where we would exchange each character with a special symbol that only two of us could understand and then we would exchange letters us using that symbol language and no one else could understand those letters and also guys write in the comments down below if you did that I know you did so don’t make me feel like a weird one well that is exactly what your computer does it exchanges each character with a number so that it can understand it and it can communicate with other computers so this means that your computer or ask key table assigns to each character a unique number and let’s prove that now so how we can do that is in my visual studio I’m going to say see out and then in these parentheses I’m going to say int and then these single quotes and I’m going to say letter a and let’s add end line so this is one way of doing that and then there is another way of doing just the same thing and that is let’s say see out and then int and then inside these parentheses we are going to say our single quotes and our letter a and also add end line so this here is called casting operator and it is the operator that forces one data type to convert into another and since we said that each character has its numeric pair inside ask key table well we can ask our computer to give us that pair so here I’m seeing please tell me what is my numeric pair of my letter A so what number is my letter a and if I run my program now you see that I got 97 so our lowercase letter a is 9 97 in ask key table let’s check now a capital letter A so I’m going to say C out in off and let’s put our capital letter a and one more end line so if I run my program now you see that I’m going to get a different number so 65 and this is so that your computer can distinguish between lowercase and uppercase letters so ask key uses seven seven bits to represent each character and that means that it can represent 2 to the power of seven that’s how much characters it can represent so 128 characters and that is enough for all English characters so all English lowercase and up uppercase letters and numbers and also some special symbols but there is also a lot more languages and also languages that have a lot more characters so there is a lot more standards for representing those characters inside your computer so this that we have just mentioned is called um ask key and there is also extended ask key that is another standard that can represent more characters then there is also utf8 utf16 and a lot more different standards so what we should remember for now that is related to ask key is that as key represents each character with a number so it holds its numeric nuic match it’s num numeric pair in its table and then that we can also get get that numeric match using our casting operator so this here is a casting operator so now you may ask if we can get a numeric value of our letter so if if we can get our int number of our our letter can we do vice versa so can we get a character from our numeric value and the answer is yes we can convert a number into a letter also and I’m going to demonstrate how we can do that in a moment so you can say see out Char and let’s say Char of 65 and add end line and if I run my program you see that I’m going to get a letter A so capital letter a and now with this when you learn this you can get an idea on how you can build something more useful than than just basic hello world application so you can for example build a program that ciphers words and sentences in a s key and then also a program that deciphers those messages back into English readable characters so now we are going together to build a program that ciphers words into ask key codes and then I’m going to leave to you to build the program that deciphers those words back into English readable characters so let’s say that we want to Cipher a five letter word so what I’m going to do is what I need to do is to create five Char variables so I’m going to create five Char variables then I’m going to let my user input his letters inside those five chart variables and then I’m going to write out encoded words so I’m going to convert all of these letters in ask key codes and then I’m going to write that out to my user so I’m going to on a in a certain way Cipher his his word so I’m going to say here Char C1 Char C2 and then Char C3 and I won stop here for a moment I want to explain one thing when we first were talking about creating variables we said that this is the way of creating variables so you say the type of of your variable so Char in this case and then you say what name you are going to give to your V variable so in this case this is our C1 C2 C3 so this way of creating variables here it is correct but it is a bit tedious for this current situation there is another way of creating more than one variable and I’m going to demonstrate that now so what you can say also what you can do is Char C1 C2 C3 C4 and C5 and all separated with this comma sign so you can create more than one variable in one line like this but only if all of those variables are of the same type and in this situation they are so all of these variables are of type CH so I’ve now created five variables of type chart and another thing that I want to mention is that in upcoming videos we are going to learn how we can let our user decide how many characters how many uh how how many values he wants to enter during the execution of our program so now we have hardcoded these five values but we are going to learn more in the following courses and also another thing is that also in C++ there are a lot of solutions that you can use already made so that you can enter whole words or sentences so you don’t have to enter your word letter by letter so you don’t have to create variable for each letter that you want to enter but what I’m trying to explain here and what we want to understand here is how it really works in the background so how it works inside you have to understand that and I believe that that will give you a great advantage over those programmers that just know how to use something rather than how to make that something by themselves and then use it so I have here created five variables and each one hold a character that we will enter and let’s enter them now so what I’m going to say is C out enter five letters and then I’m going to say C in and I’m going going to put all my variables here so C1 C2 C3 C4 and C5 so if you can see here I have entered all of my five variables in a single line so you can do this you can buy B these operators like like this so that you can enter more than one variable in a single line the same way that you can bind your see out operator so that you can write out more than just one string so now what we are left to do is we have to write out our ask key message so what I’m going to say is see out ask key message and then I’m going to put int C1 let me write that out really quickly so I have written this message here and what I have said is console output asky message and then I have combined these C out operators so that I can write more than one variable in a single line so in a single command actually so I have said give me my int code of my C1 so give me my ask key code of my C1 variable and then put a blank space and then give me my asky code of my C2 and also C3 4 and five and I have also used these blank spaces because I don’t want to write just a single number in a single line because I want to know where my first character ends and where my second character begins that’s why I’m going to separate those codes with a empty space and also there’s another thing that I want to mention and it is that when you want to convert your variable so the value that you are holding in your variable and when you use your casting operator on that you don’t put your variable in a single quotation mark so you just put the name of your variable like this example here so if I run my program now you see that I get my message so enter five letters and I’m going to enter hello and you see now that I have this output asking message is 104 101 so 108 and 108 so that is our letter L you see that it has the same code and now I have gotten this asky message which is which is our ciphered word and also there is one thing that you that I want you to keep in mind and that is that this program here will not take into consideration your enter or your space key as a letter so if you if you put your enter or your space key however much times you want it will not treat it as a character there are some other functions that you can use in your C++ if you want to capture your enter for example or your space key such as cin.get if you really want you can Google that out but at this point we are keeping this very beginner friendly so this will not capture your enter and your space so let’s just prove that I’m I’m running my program once more and I’m going put enters and spaces and you see nothing is happening and then if I enter hello once more you see that I’m going to get the same numbers so the same message as before so we have built here a program that ciphers our words into s key and we have built this program after just studying a little bit of coding so imagine what you can do if you learned more so stay tuned make sure to subscribe to my channel and also like this video if you want to support me in filming these videos and also I’m going to leave to you to write a program that is going to decipher these messages from ask keyb into readable English words so you also can put your uh suggestions of a code in a comments down below and also what you can do you can upgrade this program here that we have written so that you can put in more words more sentences if you want and then you can put those coded encoded messages in the comments and everyone who wants to read those messages is going to have to build a program that is going to decipher those messages into readable English so subscribe to my channel like this video and I’ll see you in the next one hi guys welcome to my channel in today’s video I wanted to show you something that you are going to be using very often if you start on a programming path and that is if else statement so what is IFL statement well if statement is something that is used when you want to execute part of your code so block of your code conditionally which means not always but only in a certain situation and then if you add else block to that that else block is going to be executed otherwise so in the situation where your if block is not executed your else is going to be executed so let me explain that to you in a simple example here I have opened my visual studio and let’s quickly paste an exercise that we are going to do in this tutorial and that that is this task here so let’s say that our user has to enter a number a whole number integer number and then after that our program has to check whether that number is even or odd and has to write out that to our user so how we are going to solve this problem here well I’m going to explain explain this to you using diagrams because I believe that that is the best way for you to understand this if you haven’t used this IFL statement before and then what we are going to do after we understand those diagrams well we are literally going to just translate that diagram into our C++ code so let me open my tool here I have opened a tool that I’m going to be using for drawing diagrams it’s called blank diagram you can use whatever tool you want you can use even a paint or pen and paper if you want that will work as well so here I’m going to draw the flowchart of our program so how our program is going to be executed and let me add first here this shape and we are going to say that this is going to be the start of our program so I’m going to write out start and this indicates our main function let’s say the first line of our main function so here our program starts and then as we said in our task in our exercise our user has to enter an integer number and the shape that we are going to be using for that is this shape here so let me put this shape here and I’m going to write a text inside and I’m going to say in number so this shape here indicates that our user is going to enter some data in our program and why I’m using this shape because it looks like this it looks like a funnel so you can clearly visualize that something has to enter inside that shape so something has to be inputed in your program so I’m going to use this shape for that and then I’m going to say that our user has to enter an integer number which I’m going to call number so that variable I’m going to call number and let’s quickly add this Arrow so that we know that this is the flow of our program so after it has started our user enters a number and then after that what we have to do is our program has to decide whether that number that user has entered is even number or odd number so how we are going to do that well the shape that I’m going to be using for that decision let me find it Well it is this shape here so this diamond shape and as you can see it says decision on it so that’s the shape that we are going to be using and inside this this shape here I’m going to write a condition so as you can see our program flows this way and after it comes here as you can see it has one entry point and then it it can have multiple multiple flows that it can go to depending on this condition that we are going to put inside here but how we are going to decide whether that number that our user has entered is even or odd let me very quickly open my visual studio once more it is here and let me show you something I want to introduce you to a operator that is called modulo and that is used to show what is the remainder of dividing two whole numbers so I’m going to write out C out and then what I’m going to write out is let’s say 2 modulo 2 so this operator here gives you the remainder of dividing these two numbers and in this situation we are expecting to see a zero because 2 ided by 2 is 1 and then what remains after that operation is zero and that what remains should be should be shown when we use this operator here so if I run my program you can see that we have gotten the expected result so we have gotten zero here but let me show you what happens if I enter for example five well what we are going to get is one because 5 ided by two is going to be two and then that one that remains we can get using this operator and you see that it gives us the expected result so 5 modul 2 is equal to 1 so this operator here is what we are going to be using to determine whether our user has entered even number or odd number because when we get the result of modulating two numbers when our result is one that means that our user has has entered odd number and if we get zero as a result of this operation that means that our user has entered even number so I’m going to switch again to my diagram and then inside this decision here we are going to be writing we are going to write this condition here so I’m going to say number modulo 2 is equal to Zer so what this means it means that after our program has started and our user has entered the number that he wants an integer number we are coming to a decision point where we have to decide whether this number that he has entered after you modulate it with zero whether it’s a uh zero remainder or some other remainder so in this situation when this is zero we are going to execute a certain part of code and then let me add very quickly oh I’m impressed it already has this yes inside it and then we also have this no on this other line so in this situation where this results as operation that is true we are going to execute this part code of here so I’m going to add another shape which I cannot see but let me use this shape once more and I’m only going to rotate this shape like this and I’m going to delete this text and what we are going to be using this shape here for is to indicate that we are going to output something from our program so it looks like this which is clearly a visual of something is going to be outputed from it so it is just reverse of this shape here this funnel shape and then inside this shape let me write a text which is going to be um let’s say that in the case that this here results as a yes as a true operation so our number when it’s modulated by two it gives a zero remainder we are going to say that that number is even number so we are going to write that out to our user and then in the situation where this here results as a no so as operation that is false let me write out that here and I’m going to say to our user so I’m going to write out to my user that he has entered odd number and let’s just join this here like this okay so our user enters a number then we decide if that number modulated by two gives us zero and if yes we are going to write out to our user that that is a even number if no we are going to write out to our user that that number is odd number and this is here really our program what I’m going to add is let’s say that after this our program is going to stop so I’m going to to say here that this is the end of my program like this and let’s add one more stop of this flow here so in both of these situations after we have and after we have outputed to our user the result of this program our program is going to stop and this here really like these two stop points look a bit ugly to me so what I’m going to do instead of this I’m going to delete this you could have as well left it like this like like it was before but what I’m going to add I’m going to add one more of these diamond shapes only this time it’s going to be smaller and I’m going to say that in this situation it is going to be used only to say that this decision here this decision flow is going to be joined in this point here so like this I’m going to say whatever has happened whether this block of code or this block of code it has come to an end here and then after that from here I’m going to say now my program is going to stop like this and this and let me remove this yes and this is going to be the flowchart of our program so our program starts then our user enters integer number then we decide whether that number modulated by two is going to give us zero if yes we execute this else we execute this so if this is no and then we come to this which joins our two pads and then we can say that our program has ended and now let’s translate this to our code here I have opened both my visual studio and also I have exported this diagram so that we can translate it literally we can look at this diagram and then translate that into code so now I’m going to delete all of this that we have written inside our visual studio and I’m going to look at this picture and just translate it into C++ commands so the first this first shape here says that user is going to enter integer number inside our program and we are going to call that variable a number so I’m going to say int number and then this here this funnel shape really translates into C in command so user enter something inside our program and that is C in command and I’m going to say that user is going to enter a number before this I would like to add one more information to our user so that he knows what he should enter you could have as well put that to our diagram but I didn’t want it to make that diagram crowded so I didn’t but what we are going to add here is see out and then say please enter whole number like this so we write out a message to our user and after that our user as you can see here enters integer number we had also to declare this int number variable because you can enter the value only for variables that you have previously declared and then after that our program comes to this decision point and how we can represent this decision inside our code is using if statement so we are going to write here if and then I’m going to put this condition here inside these parentheses so I’m going to say if number modulo 2 equals zero this here is operator of equals so whether this side is equal to this side since only one of these so like this this operator here is used to assign value to a variable so that was already taken so this here two of these signs are used to check equality of these two sides so whether this is equal to this and then what I’m going to say as you can see from my diagram here we check this condition here and if this condition is true if this condition is yes we are going to execute a certain block of code and that is this code here and how you indicate a block of code in your C++ is using these braces here like this so this is your block of code that is going to to be executed in this yes situation and then in case that this no here happens so in case that this results as false you are going to execute another block of code and we represent that block of code using else statement so we are going to say if this here is true execute one block of code else if this here is false execute another block of code so here what our if makes us do well it says that we only have to write out to our user that he has entered even number because this shape here as you can see it looks like this and it represents that something should be outputed from your program so we are going to use for that our C out command so see out you have [Music] entered even number so like this and let’s add end line so in case that this here results as true we are going to Output to our user that he has entered even number else if this here results as false we are going to Output to our user let me copy this like this and we are going to change that only to odd so in case that this here results as false we are going to Output you have entered odd number so either this here is going to be executed or this here depending on this condition and you can see that from diag diagram clearly so when your program comes to this decision point it checks this condition and if the answer is true or yes it is going to execute this else if the answer is no it is going to execute this and then after that it really just joins these two flows and then that is the that is the stop point of your program so we can write here see out thanks by okay let’s say that that is the last line of our program you don’t I haven’t put this on on my diagram I okay so I’m going to run my program now and as you can see it says please enter a whole number so this line here and if I enter for example8 it is going to come to this line here and it is going to modulate our eight with two and if the result is zero which it is it is going to execute this line here and write out you have entered even number oh thanks we are missing an S here and then it writes out thanks by and let me add an S here okay and if I run my program again as you can see it ask me asks me again to enter a number and this time I’m going to enter five which we know that is an odd number it says you have entered an odd number because it has come to this decision Point here it has tried to modulate 5 with two and the result of that was not zero so it has not executed this but it has executed this else statement and it has written you have entered odd number as you can see here and then this thanks by okay so I hope that you have understood this and if you like me to use these diagrams to explain some more code that we are going to be using you can write that in the comments down below and I’m going to try to use more of these diagrams because I know that some people understand it better when there is a visual representation of your code I was one of those people when I first started learning programming so if you find it easier to understand when I use these diagrams I’m going to be using that so thanks for watching this video make sure to like it if you find it any helpful and also to subscribe to my channel and I’m going to see you in my next video bye hi guys welcome to my channel in this video I wanted to talk about if else statements but a bit more complex if El statements so that you can get an idea on how they are used in real life so if you’re not familiar with them at all like you have never heard of IFL statement then you should probably First Watch My First video of this subject and I’m going to link it somewhere here and then you can come back to this video but if you’re already familiar with the very basics of IFL statements then this video is right for you so that you can upgrade your knowledge so keep watching so the problem that we are going to solve in this video is the one that students usually get on their exams a lot and with the knowledge that you get from this video you are going to be able to recognize and solve other problems of this type and also if you don’t want to work with C++ this logic that I’m going to teach you today is going to be applicable in other programming languages as well so here’s the text of my problem and it says that our user should enter the lengths of a triangle so a b and c and after that our program should write out whether that triangle is equilateral isoceles or scaling triangle and as you already may know equilateral triangle is the one that has all three sides of the same length isoceles triangle has two sides of the same length and then scalene triangle is the one that has all three sides of different length so the way that we are going to solve this problem is we are going to really visualize the solution for this problem so that you can really understand it and that is going to be using diagrams the tool that I’m going to be using for drawing diagrams is called blank diagram you can use whatever tool you prefer even pen and paper if you want and the first shape that I’m going to put to this diagram is going to be this circle shape here let’s say that it is going to indicate the start position of our program so here is where our program starts and after our program has started what we want is we want our user to enter side lengths of our triangle and the shape that we are going to be using for DOT is this funnel shape here so that you can clearly visualize because of this funnel shape that something should enter inside it something should be inputed in your program and what should be inputed is side length so a b and c and the type that I’m going to be using for these three variables is float why float well because the lengths of sides of a triangle can be decimal point number or floating Point number so float and then let’s put this Arrow here so that you can oh so that you can uh visualize the flow of our program it goes like this and then after our user has inputed the side lengths what we should do is we should determine whether he has entered a equilateral triangle isoceles triangle or squalene triangle and what I’m going to do next is I’m going to use this diamond shape here which indicates decision in your program and what I’m going to put inside this shape here is condition the first thing that we want to check is whether whether our user has entered equilateral triangle so whether these three sides are all of the same length whether these three variables hold the same value and let me make this a bit bigger and inside here I’m going to say please check if my a is equal to my B and also if my B is equal to my C like this so let me join these two and I want to explain two things when it comes to this first one is this operator here this end operator these two ents this is logical end operator and it is used as I’m going to just explain so this expression here is going to result as true expression only in a situation where this here is true and also at the same time this here is true so in that case this whole expression expression is going to result as a true operation that is the logic behind this end operator and the second thing that I want to explain is following so I have said here please check if my a side length is equal to my B length and also if my b side length is equal to my C side length what you have Al what you can also put here is you can also say a because we have determined here that these two are of the same length so it really doesn’t matter if you put here A or B whichever you prefer so in this situation where this results as a true operation we are going this way and what I’m going to do when this results as a true operation as a yes operation I am going to use this shape here let me just rotate it I really don’t see a shape that that is this one so that’s why I’m using this reverse funnel shape and I’m rotating it so that you can clearly visualize something something that is being outputed from your program let me delete this text here and make it a bit wider and I’m going to put text inside it and what I want to write out in this situation here where this results as a true operation where this condition is fulfilled is that our user has entered equilateral triangle because all three sides are of the same length so let me write that out so in case that this condition condition here is true we are going to to write out our user that he has entered equilateral triangle and then let’s check the situation where this here results as a no operation so in that case we have to to determine whether our user has entered scaling triangle or a isoceles triangle and for that I’m going to be using another of these diamond shapes so another decision that we have to make and let’s put it here and oh no and the next thing that we want to check is following so I want check when I once I have determined that my user has not entered all three sides of the same length I want to determine whether he has entered all three sides of a different length so I’m going to put inside this shape here another condition and I’m going to say following so please check if my a is oops if my a is not equal to my B and also at the same time if my a is not equal to my C and also at the same time if my b is not equal to my C like this so this is the next condition that we want to check and this operator here is called not equal to operator and it is really reversed from this equal operator it has reversed logic so in this condition here what we want to check is whether my a side length is not equal to my bside length and also at the same time whether my aside length is not equal to my C side length and also whether my B is not equal to my C and if this here results as a true operation that means that all of these three side lengths are not equal they all have the different lengths so in that situation we are going to go this way and we are going to write out let me copy this shape and here I’m going to say please write out my user that he has entered a triangle that has all three different side lengths and that is going to be our scaling triangle like this okay so in the situation where this here results as a true operation it means that all of these three are of a different length and we write out to our user that he has entered scaling triangle and then the situation where this here results as a no because we have previously determined that these tree are definitely not of the same length and then we have in this condition here determined that these Tre are also not of a different length what is left is that the two of these two of these three sides are going to be of the same length and that is going to be the situation where we write out to our user well I can copy this and we write to our user that he has entered isoceles triangle so let me write that out okay so these three are three potential end points of our program so three potential solutions to our problem and after we write out to our user this or this or this I’m going to really put this one diamond shape just to indicate that our decision has now come to an end we have solved this problem and we are going to join these pads in this shape here so I’m going to put this and this like this so now after we have decided whether whether it’s an equilateral triangle or scaling or isoceles I can now say say you can resume the execution of my program normally so whatever I put after this shape here is going to be executed in either this case or this case or this case and what is going to be happening after the shape here is just stop so our program is going to stop but you can put a lot of code here if you need to so I’m going to say stop and let me delete this really I don’t need it okay let’s explain one more time this diagram so after our program start Parts our user should enter a b and c which represent side lengths of a triangle and I have used this float shape since since those side lines can be uh decimal point numbers and after our user has entered that we come to this decision Point here and what we want to decide here is first thing we want to check whether all of these three variables are of the same value so whether he has entered three same lengths for a triangle sides and in case that this here results as a true operation we are going this way and we write out to our user that he has entered equilateral triangle and after that we go this way and our program stops in the situation where this here results as a no operation we want to check something else so we are going this way and we come to another decision point and here we want to decide whether our user has entered a triangle that has all three three different side lengths so after we have checked that they are not equal we want to check whether all of three variables are of a different value and in case that this condition here results as a yes operation that means that he has entered scaling triangle but in case that this here results as a no operation we can now clearly say that he has entered isoceles triangle because we have checked here that these three sides are not the same and we have checked here that these three sides are not different so all of them are not difference and that means that two of them have to be equal and in that situation we go this way and we write out to our user that he has entered isoceles triangle and now we can go this way and you can see that in this situation as well our program stops here I have opened my flowchart and my visual studio so that we can just translate this diagram into our C++ code so the first thing here is our user enters our a b and c side length so let’s declare three variables of type float and let’s call them a b and c and now I’m just going to write out a message to my user so that he knows what he should enter so let’s say C out and please enter a and then B and then C like this and then let’s let’s use our C in command so that he enters his values okay let’s say first thing that our user uh enters is going to be stored inside my a variable after that what he enters please store that inside my B variable and then the third value that he enters is going to be stored inside my C variable so now that we have entered our side lengths of a triangle we come here so to a decision point and we said that we represent this with if else statement so I’m going to write here if and then inside these parentheses I’m going to say this condition here so please check like this please check if my a is equal to my B and then also check is my b equal to my C like this and if this this here this expression here results as true expression I’m going to execute one block of code and that block of codee is going to be this one here but if this expression here results as a false expression okay I am going to execute another block of code and that block of code we represent with our else statement like this okay and then let’s write this yes situation so let’s write here see out equilateral Al triangle so like this oops you should put that not in a quotation marks okay so in this situation where these three side lengths are equal we write out to our user that he has entered equilateral triangle but in the situation where this here results as false we have this else block to execute and let’s find out what is inside our else block so it is this code here and that is another if else statement so this if else statement statement is really nested inside this one here so it is nested inside this else block so here in these brackets here we are going to put another if El statement so let me write out if and then inside these parenthesis we put our condition from this decision here so from this diamond shape here and I’m going to write out please check if my a is not equal to my B and then is my a not equal to my C also and then is my b not equal to my C so in this condition here we check whether all of these three variables have different values here and if they do if this condition results as a true expression I’m going to write out let me just copy this and then we are going to adjust it I’m going to write out that my user has entered a scaling triangle as you can see here so when this goes in a yes way we write out that message so if this results as a true we write out that our user has entered scaling triangle but if this here results is a false then we are going to write out that our user has entered isosceles triangle and let me copy this like this okay so we have nested this if else statement inside this else block so this if else statement is going to execute only in a situation where this here results as a false F false expression so this code here if you find it a bit unreadable I want to show you one trick so one rule that you can really follow and that is if you have only one command under this if or else statement you don’t need to use these brackets so you don’t need to say that you are going to execute a block of code since you are not you are going to execute only one command so you can really delete th this here like this and then also here since we have only one command both here and here we we can delete these brackets here like this okay so now this code is a bit easier to read okay now as you can see from this diagram here it is literally translation of this here and vice versa so this code here is a translation of this diagram here and what it means is when our user enters three values for our sides of a triangle first we are going to check whether all of these three sides are equal if they are I’m going our program is actually going to say that our user has entered equilateral triangle but if this here results as false then we are going to our else block and then inside that else block we are going to Nest another if else so we are going to check another condition we are going to uh execute this if else here and this if else here is going to check for following so it is going to say please check if all of these three variables are different values so if all three sides of a triangle have different lengths and if they do we can say that that is a scaling triangle but if they don’t then since we have already checked that they are all that all of them are not equal and also we have checked here that all of them are not different we can say now that definitely two of these variables are equal and that means that our user has entered isoceles triangle and that is the program that we had here okay so I hope you like this video and if you did this is a strong call to subscribe to my channel and to share it with anyone else who would like to learn programming and I’m going to see you in my next video bye hi everyone welcome to another video of C++ for beginners in this video I wanted to talk about a operators in C++ and we are going to cover the ones that are used most of the time but I’m also going to make a separate video for certain operators that have specific use in C++ so in this video we will talk about the ones that are used most of the time and that you really need to know and understand and also make sure to watch the entire video because I do have some important tips to give you when it comes to this that you really need to understand and that you will find Handy for your future progress in learning of programming the first thing that I wanted to explain is what are operators you can understand operators as special type of functions that are used to perform certain calculations on arguments or operant and then based on those arguments and on that calculation they give you a new value so the first type of operators that I want to talk about are arithmetic operators and you are probably familiar with most of these so there is operator of adding and and subtracting multiplying dividing and also one more operator that you may not be familiar with which is the modulo operator so all of these are arithmetic operators and let me give you an example of how these are used so I’m going to say C out and then let’s write out the result of 5 + 2 and let’s add one end line okay so this here is the operator of adding and then these two are operant or arguments and this here is going to perform certain calculation and then write that out to my console so if I run my program we get the expected result which is seven so these really behave as you would expect them to behave in maths but there are certain rules that are specific to programming and that I want to mention so this operator here operator of dividing let me give you a simple example if I say C out and then five divided by two and I add end line what do you expect to see in this line here write that in the comments down below now I’m going to run my program and you see that the result that I’ve gotten from this line here is two but why has that happened we know that five divided by two is 2.5 well there is a certain rule in programming that says that the result of dividing two whole numbers like this so five is whole number and then two is also whole number is also going to be represented as whole number so even though the result of dividing D2 is 2.5 that 0.5 cannot really be put inside uh integer variable so it cannot be put inside integer container so just two can can be put here and that point two is really lost if you want to show the real result of dividing these two what you have to do is you have to make one of these two or both of them off a different type and that type is going to be decimal point number so float or double so if you put here 2.0 or you put here 5.0 then in this case you are going to get the result represented as decimal point number so you can use it like this or you can make just one of these of a floating Point type so you can say also like this and then if I run my program now you see that now I have got the expected result so 2.5 that is one rule that you really need to know when it comes to this division operator and another tip that I want to show you is this operator of modulo so how do we use this modulo operator this example here here that I’ve just shown you shown you let me return this to this situation where I’ve been dividing um two whole numbers and we’ve just seen that 5 ided by two is going to result as two and that one that remains we can get that one really with this operator here so if you say C out and then five modulo 2 add end line this operator here is going to give you the remainder of dividing two whole numbers which are these two so the result of this operation is going to be that one so let me run my program and you see that we have gotten that expected result so 5 divided by two when it comes to whole numbers if you’re dividing whole numbers the result is also going to be whole number and in this situation it’s going to be two and then that remainder after dividing two whole numbers you can get with this modulo operator and the remainder after after dividing five with two is one and you get it with this operator here so these five are really arithmetic operators and these are these are also called binary arithmetic operators why binary well because they are all used with two up runs they perform their calculation on two operant there is also another type of arithmetic operators in C++ that are used or that perform calculations on only one operant and those are unary operators the examples of unary operators in C++ are this operator here which is operator of increment and then this operator here which is the operator of decrement so how these are used let me give you example so let’s create a variable of type inth and let’s call it counter and I’m going to assign to that variable a value of seven for example so how this operator of increment works is it really increases the value of your variable by one so if I say counter counter Plus+ what this operator is going to do it’s really going to add plus one to my counter variable so 7 + one it should in this line here increase value of my counter variable to eight and let’s prove that oops see out counter and then let’s add one end line okay if I run my program you see that we’ve got expected result so eight we’ve declared a variable of type in called counter we’ve given it a value of seven and then in the next line we applied this increment operator to it which had increased its value + one so eight and then after we write out that value of that variable we got our result so that is how this increment operator is used this decrement operator so counter minus minus this decrement operator here Works uh reverse so it really subtracts one from this variable here and if I copy this line here and then paste it after this line here our counter should be decreased by one so the value of our counter should really again be seven and if I run my program oops like this you see that we’ve gotten our seven so we’ve gotten the expected result however there are specific rules when it comes to these increment and decrement operators and let me explain what I mean on another example so I’m going to create another variable of Type L int call it counter two and let’s assign it also a value of seven and what I want to do with this variable here is I want to write out its value but I want to write out and increment its value in a single line so I want to do this so see out counter to Plus+ and I’m missing this red Direction sign here okay so what are you expecting to see in this line here we’ve seen that this line here has resulted with eight and this line here should also result in eight let’s see okay run my program and it didn’t in this line here we have written out seven but why has that happened well this operator here is increment operator but it is really post increment operator and what that means is that whatever operation needs to be performed on this variable is going to happen first and then this increment operation is going to happen so that is called po increment another type of increment is pre-increment and it is used like this okay and this pre-increment means that whatever operation needs to happen on this variable here first I’m going to increment it and then I’m going to perform that operation so if I run my program now you see that we now get our eight same as here and that is because we have used this pre-increment which first has increased our variable and then our variable is written out okay and the same is applied for decrement operator so for example if I say C out counter to minus minus and then oh my God again okay and then if I run my program you see that we have gotten the same value for this line and this line so eight and why that has happened is because we have used this post decrement here so first our variable has been written out and after that the value of that variable has been decreased so just to prove that I’m going to say one more time C out and then counter two without any increment or decrement just to prove that uh the value of this variable has been decremented after it has been written out so if I run my program again you see that this line here has resulted in eight and then this line here has resulted in seven okay so you see that our console now is full of some random numbers and it’s really making it hard to read so what I want to do is I want to clear out my console but without deleting this code here so how I’m going to do that um I’m going to use a command that is called system and then inside these parentheses I’m going to say CLS which really is going to clear my screen so if I run my program you see that all of our code is still here but console has been cleared so that’s because of this command here that’s a quick tip if you need to clear your console another type of operators that I want to talk about are relational operators and these are really used to compare the value of two operant so two arguments and relational operators in C++ are following so we have less than and then we have greater than and then less than or equal to and then greater than or equal to then we have just is equal to operator and then we have not equal to so these six are relational operators in C++ and how these are used let me show you in an example so let’s say that I’m going to create a variable of type in call it a and let’s assign it the value of five and also create another variable that is called B and assign it value of five so now if I say C out and then inside this parentheses I’m going to say please write out if my if my V variable a is greater than my variable B so if if my five is greater than this another five okay and I’m using these parentheses because I want my C out command to know that this here is one expression that first should be calculated and then whatever this expression here results in I’m going to write that out in my cout command so if I run my program now you see that we have gotten zero and if you know about how computers work you know that they work on zeros and ones meaning that zero represents false and then one represents true so in this situation we have gotten the result of false so when I ask my computer is my a greater than my be so is five greater than five it says false no it is not okay and the same is going to happen if I put here this other operator of less than like this but if I for example put less than or equal to so this operator here and I run my program again you see that now we have gotten the result of one so that is true that is what our computers computer considers as true so our five is less than or equal to five which is correct and even better would be if we say just is equal to and in this situation also we are going to get true as a result like this because five is equal to five and then how we use this operator here is like this so you can say is my a not equal to my b and your computer really hears is my five not equal to my five and the response to that is going to be no it is false because these two are equal it’s the same number even though it’s held by two different variables okay I hope you understood these relational operators and there’s also one tip that I want to give you here and that is you see this comma sign here this is also operator in C++ and it is not really ational operator but it is binary operator and how this operator is really used because we have seen it before without knowing that it was an operator it is used as follows so in this line here we have said please create a variable of type int call it a and assign it value of five and then also so that is the purpose of my comma so also create another variable of type int call it B and assign it value of five so my comma in this situation is operator and separator as well so it is pretty intuitive so before we continue let’s add one more of these clear screen commands here and the next type of operators that I want to talk about are logical operators and there are three of those so we have logical and and then logical or and then also not operator so these three are logical iCal operators and they are usually used to combine two or more conditions into one single condition and I’m going to show you that on an example and let’s use for that example these variables here let’s just change the value of our B to eight for example so what I want to do is I want to write out following so is my a is my a equal to five and then also is my b equal to five I want to write out the result of this expression here and you see that we have used our end operator to combine these two expressions so if I run my program you see that the result of this is false so that is the nature of our end operator it really works like this it says please check is my a equal to five yes it is and then also check is my b equal to 5 no it is not it is 8 so then this operator says okay whatever then this whole expression is going to be false because this operator here needs both of its oper so this here and then this here also both of these need to be true in order for this whole expression to result as true expression that’s the nature of this end operator however there is one more forgiving operator in C++ and that is this operator here so this or operator and if if I place that here instead of our end operator and I run my program again you see that this time we got different result now we have true and that is the nature of our or operator so it says please check if this is true or this here is true whichever of these results as true then I’m going to be true expression only in the situation where this here is false and also this here is false I’m going to result as false in any other situation this here is going to be true so that is the nature of or operator and then we have also this not operator which really negates whatever comes after it so if I put it here for example you see that you’ve seen that previously this expression here has resulted as true expression now that I negate it we are going to get false so that is our not operator here okay and there is also one more thing that I want to explain when it comes to the procedence of these operators which means priority of these operators so which operator types have the biggest priority uh the biggest priority have arithmetic operators after that come these relational operators and then the smallest priority already have these logical operators so let me show you that on an example let’s just comment this very quickly and let’s add another line but the previous one that we had so this here like this that is the operation that has resulted as false so is my a equal to five and then is my B also equal to five and this has resulted as false but if we for example say it like this what do you think that this expression here is going to write out so if I run it you see that we have gotten one why is that so well as I’ve said the biggest priority have arithmetic operators so this expression here is going to be executed first so 5 + 3 after that priority have these relational operators so this operator here and this operator here so after that comes logical operation so so this expression here is going to be really is my a equal to 5 yes it is and then also is my b equal to eight and the answer to that is also yes so now both of these operant are true and our end operator is satisfied now and it’s going to result as true operation this whole expression so you need to remember that the biggest priority have arithmetic operators so these here so these here binary and unary arithmetic operators after that we have our relational operators and then the least priority have these logical operators here let’s add one more of these clear screen commands before we talk about the last type of operators that I want to cover in this video and those are operators of assignment and there are six of those and there are a follow so operator that just assigns value and then operator that adds and assigns and then operator that subtracts and assigns multiplies and assigns divides and assigns and modulates and assigns so these six are operators of assignment and let me show you how they work so first I’m going to create a variable of type end and let’s call it X and let’s assign it a value of for example five okay so this here is example of this assignment operator here and that is how it works it assigns whatever is on the right to your variable on the left so that is the direction of Association it really says whatever is on my right side Please assign that to my left side and let me show you know how these here work because they’re a bit more complex so if I say for example x + = 7 like this what this expression here is going to do well let me write out one more expression and that is X = x + 7 so this expression here and this expression here are just the same this is just shorter way to write this here and what this here does well this is expression that is not valid in mathematics but in programming it is and it does following so it says please whatever is on my right side assign that to my left side and why it does that well because this arithmetic operator here has priority over this assignment operator so if you remember when we were talking about priority these assignment operators have the lowest priority of all so whatever is on my right side that is x + 7 is going to be assigned to my X variable so 5 + 7 is going to be equal to 12 and then that is going to be assigned to your X variable in this line here and this expression here is really short way to write this here okay so I’m going to comment this out and if I write the value of my X now we are expecting to see 12 and it is the result that we have gotten so this x +al 7 is just going to do this so this expression here so it’s going to add to my X this number here and the same applies to these operators here so this one that subtracts and then assigns or multiplies and assigns divides and modulates so if you say for example x minus equal to 7 then that is really going to result as X is equal to x minus 7 and then that right side is going to be assigned to your left variable because arithmetic operators have priority over these operators of assignment and if I run my program again it really sees 5 – 7 and that is minus 2 so I hope that you have understood these examples that I’ve just given you and I’m also going to include some more examples at the end of this video but tricky want so that you can be sure that you have really understood these operators in C++ because they are used very often and you have to understand them in order to continue with your progress on a programming path so make sure to do those examples on your own and if you come across any problems or you have any questions that are related to this video write those in the comments down below and I’m going to answer them I’m going to help you and if you likeed this video make sure to subscribe to my channel and also click the Bell icon so that you are are first to be notified when I publish my next video and I’m going to see you in my next video bye hi guys welcome to my channel in this video I wanted to answer a question that I recently got and that is a question that is often asked on job interviews and also students get it on their exams and that is a question on how you can swap the value of two variables I’m going to give you two ways to solve this problem but let me just explain what I mean by this so I’m going to copy the text of this problem here like this and let me say that I have one variable which is going to be called a and that variable is going to hold value of 20 let’s say and then I have another variable which is going to be called B and that variable holds value of 10 and now I need to switch the value so to swap the value of these two variables I want my a to hold whatever my B is holding and then I want my B to hold whatever my a is holding so how we are going to do that for the first explanation I will need something so let me get it so here I have two glasses and inside this left glass I have my green juice and then inside this glass I have just plain water and the question that we have in our our Visual Studio is equivalent to a question how we can swap the content of these two glasses and to solve that problem I’m going to use another glass so a temporary glass and that is this one here and I’m sorry guys I know this is not a glass it’s a Shaker but it will do for this explanation so what I’m going to do is I’m going to take this this glass this variable and I’m going to put its content inside this third glass this temporary glass okay and then when I have saved the value of this glass inside this temporary glass what I can do is I can put my green juice so the content of this glass inside here okay this is a bit more complex in real life than in programming so once you do that you have saved the value of this glass here so the content of this glass here inside this glass and now what we are left to do is just to put whatever our temp variable our Temp Glass was holding inside this glass here and now we have successfully accomplished to solve this problem so we have switched the content of these two glasses and let’s just translate this into our code now that I have explained the algorithm to solve this problem we are just going to translate that algorithm into C++ code so the first thing that I’m going to do is I’m going to create a variable a temporary variable which I’m going to call Temp and inside that temp variable I’m going to put whatever my a is holding so I’m going to say that my temp holds now whatever is inside my a variable and since we have our a variable free now to hold something else I’m going to put my B value inside my a so now my a is going to hold whatever my B is holding and now that we have saved the value of our B inside our a variable we can put inside our B whatever our temp variable is holding so that is the value that previously was inside our a variable and this here is really the algorithm to solve this problem using a third VAR variable and let’s just quickly add a c out so that we can see what we have done so our a is equal to a and then our B is equal to RB and let’s add end line like this and I’m going to add just a comma sign here okay and if I run the program you see now that here we have a is equal to 20 and B is equal to 10 and now we have successfully swapped the value of these two variables so now my a is holding 10 and then my B is holding 20 which previously was inside my a variable and this is a solution for our problem using a third variable but this question is often asked in another way and that is how you can swap the value of two variables without using a third variable and now I’m going to explain that so let me very quickly delete this until here okay and in order to solve this problem without using third variable you will have to think in a more mathematical way and the solution for this is going to be the following so the first thing that I’m going to do is I’m going to say okay let’s say that my a variable is going to hold whatever my a is holding holding and I’m going to add whatever my B is holding also and this expression here would be really incorrect in any mathematics but in programming this is this is a valid expression and it means this here whatever this expression results in I’m going to assign that to my a variable and this here is whatever my a is holding plus whatever my B is holding assign that to a so that is going to be a so 20 + B 10 that is equal to 30 so now in this line here my a is going to hold the value of 30 and then in the next line let’s say now my B is going to hold whatever my a is holding and then subtract whatever my B is holding from that so in this line here I’m going to say store inside my B the result of this expression here and this expression will result in following a so 30 minus b – 10 that is going to be 30 – 10 20 so in this line here my B is going to hold the value of 20 and then what we are left to do is just to put the correct value inside our a and that is going to be a = to a minus B this means whatever my a is holding at this very moment and that is 30 minus whatever my B is holding at this very moment and that is 20 and guys 30 minus 20 is going to result in 10 and our a is going to get the value of 10 in this line here so now if I write out C out a is equal to a and let’s add and line and then C out B is equal to B and if I run my program you see that I’m going to get the expected result so my a now is holding whatever my B was holding so 10 and then my B is holding this value that previously was inside our a variable here so we have solved this problem in two ways so with or with without a third variable so if you found this video helpful make sure to like it and to subscribe to my channel and also any questions that you have leave those in the comments down below and share this video with anyone who’d like to learn programming and I’m going to see you in my next video and also guys one question for you if you have any recipes any algorithms on how you can make green juice taste and smell good please make sure to write those in the comments down below bye hi guys welcome to my channel welcome to another video of C++ for beginners I just got an idea of a simple and fun program that I can show you how you can build and since I’m a bit of a fitness Enthusiast myself if you haven’t noticed uh I wanted to show you how you can build your own body mass index calculator so stay tuned make sure to subscribe and let’s build our own BMI calculator okay so here is the text of my task and what we are going to build is BM calculator so first I want to explain what is BMI calculator it is body mass index calculator that is going to tell you whether you are underweight or normal weight or you are overweight depending on your weight and your height and here I’m going to use weight in kilograms and then height in meters so if someone else wants to adjust this program so that it can work with pounds feets and inches you can write out that code in the comments down below and the first one that gets it right I’m going to pin that comment so that everyone else can use it and see it okay so we are going to use this formula here which is used to calculating your BMI in metric system so another thing that I want to mention is this these three categories here there are also some more categories that you can really calculate whether you are extremely underweight or you are extremely overweight but these three should be enough for us to do now and these three I’m going to cover in this code another claer that I want to make is that this BMI calculator probably should not be used by people that have huge amounts of muscle so professional bodybuilders because even though they have muscle that muscle is still weight so even though they are not overweight they will fall in a category of overweight because their BMI is going to be higher but for most of us that don’t have a lot a lot a lot of muscle or are leaving normal lives training couple of times a week maybe this is going to be completely okay so let’s build our prog program now so here is our tool that we are going to use to draw this diagram so Lucid chart and let’s really develop the algorithm for solving this problem and then we are going to translate that diagram that algorithm in C++ code so as you may already know first thing that I’m going to put here is this shape here that is going to indicate the start position of my program this circle shape here and after that we really need two things from our user so two things that he should enter and those are his height and his weight so let’s use this funnel shape here that is going to indicate something entering in your program so visual representation that’s why I’m using that funnel shape and inside here I’m going to put two things that my user needs to enter and those two things are these two tanks here so his weight and he should enter his weight in kilogram and also his height and let’s say that he should enter his height in meters so I’m going to use metric system for this problem okay and let’s indicate the flow of our program it should flow like this and after our user has entered his weight and his height there is a simple calculation that our program needs to perform and that calculation is going to be a process for our program so the shape that I’m going to to use for that is this shape here so this shape that says process on it and what our program should process what our program should calculate is it should calculate BMI so I’m going to put here BMI is equal to and then I’m going to put this formula here that is used to calculate BMI okay so it’s weight represented at kilog divided by height to the^ of two so I’m going to multiply my height with height okay and this is really the calculation of our BMI and now after our program has calculated this BMI what we are left to do is we should depending on this BMI really write out to our user whether he’s underweight or overweight or of a normal weight so we should make a decision depending on this BMI and for that decision we are using this decision shape so this diamond shape and inside this diamond shape I’m going to put I’m going to put a condition and that condition is going to be let’s first check whether my BMI is lower than 18.5 like this and in this situation where our user’s BMI is lower than 18.5 so when this condition here results as yes I am going to write out something to my user and let’s me find a shape okay let’s rotate this shape and delete is text okay and what I want to write out to my user oh I can make this smaller what I want to write out to my user in the situation where his BMI is lower than 18.5 is I want to write out that he is really underweight so let’s put text inside here and let’s write out that he is underweight so that is going to happen in this situation oh my God that is going to happen in this situation here let me just okay now it’s okay so in this situation where his BMI is lower than 18.5 we are going to write out that he is underweight but in the situation where his BMI is not lower than 18.5 we have to check something else and let’s check so let’s add another another of these diamond shapes where is it here okay and let’s now check whether his BMI is higher than 25 so I’m going to write out BMI greater than 25 okay and that is going to happen in the case that this here results as no and in the situation where user’s BMI is higher than 25 that really means that our user is overweight so we are going to write out of okay so we are really going to write out a message to our user so let’s copy this shape here which we are using to write out a message to our user so this reversed finnal shape that represents something leaving your program something being outputed from your program so in the case where this calculation here results as true we are going to write out to our user that he is overweight like this okay and let’s really adjust this now it’s now it looks a bit more presentable okay so first we have checked whether his BMI is lower than 18.5 is if yes we have written out underweight if no we have to check whether his BMI is greater than 25 and if it if it is it means that he’s overweight but in the situation where this here results as a no that means that our user BMI is not lower than 18.5 nor it is greater than 25 so that means that his BMI is really somewhere in between so between 18.5 and 25 and that is a normal BMI so that is our last possible situation where we are going to write out to our user that he has a normal weight so let’s write out normal normal weight okay so after we have covered these three cases cases let’s just join our flows into one so I’m looking for this shape here and I’m going to make it smaller delete this text from it and I’m really just going to join these flows inside this shape here let’s let’s just try to move this here I’m going to delete it and let’s say that I’m going to oh no [Music] okay guys I had huge problems trying to adjust some arrows so I had to rewrite this whole diagram and I believe that I made it a bit prettier so here we were explaining that in the case that our user has BMI lower than 18.5 we are going to write out this message to him in the case that his BMI is not lower than 18.5 we are going to check whether it is greater than 25 and if yes we are going to write out that he’s overweight if no we are going to write out that he’s of normal weight and in each of these three cases we are coming here to this diamond shape here which is just just going to join these three potential situations and after this diamond shape here we are going to write out to our user what his BMI really is because I really want my user to know if he’s for example in this situation here or in this situation here I want him to know whether he’s BMI is let’s say 18.4 so he’s not that extremely underweight but he’s just a little bit underweight or in this situation here I want him to know whether his BMI is 25.5 so he’s just a little bit overweight or his BMI is really 35 let’s say that’s why I I want to write out my BMI to my user his BMI to my user okay so and then we just released upop our program so now I’m going to copy this diagram and put it next to my visual studio so that we can really translate this this diagram here into our code so here I have put this diagram and then also my visual studio and let’s now translate this diagram let’s translate this algorithm and to code because this here really is an algorithm explanation on how to solve a certain problem so the first thing that needs to happen is our user should enter his weight and his height and for that to happen I first need to declare variables for weight and height and for that I’m going to use type float because his weight and height can be decimal point number so I’m going to say float and then I’m going to say weight and height okay like this and after that let’s put a message out to our user so that he knows that he should enter his weight and height so I’m going to say C out weight and let’s put that here should enter his weight in kilogram and then also height but he should enter his height in meters like this so that he knows which which system he should use okay after we have written out this message here what I need to do is I need to use my C in command so that I can allow my user to enter weight and height and I’m going to say first thing that my user enters that’s going to be stored inside my weight variable and then after that the second value that my user enters is going to be my height so store that in a height variable like this okay and after we have entered weight and height next thing that needs to happen is we need to do this formula here and really calculate our BMI and in order to store this result the result of this expression in our BMI we need a variable for that and that variable is obviously going to be called BMI so let’s add here in this line here let’s add BMI so I’m going to declare another variable and that variable I’m going to use now and I’m going to say that my BMI is going to be used as follows so I’m going to use this formula here which is our weight divided by our height to the power of two for now I’m going to use use it like this okay and and in this line here we have really calculated our BMI so this code here and now after we have calculated our BMI what we should do is we should really check what is the value of that BMI and for that we are using this shape here which really translates into if if else case so first I’m going to say if and then the first thing that I want to check is if my user’s BMI is lower than 18 5 like this and in this situation where his BMI is lower than 18.5 I’m going to execute one line of code so that’s why I don’t need to put these brackets so I’m going to delete them because I just want to execute one line of code and that line of code is going to be indented here so that it is easier to read and that line of code is really to be going to be a message that I want to write out to my user and that message is going to be that my user is underweight unfortunately okay and let’s add one end line like this okay this is in the situation where his BMI is lower than 18.5 in a situation where his BMI is not lower than 18.5 we are going to execute something else so that something else we represent with else statement and I’m going to put that lse statement and what is happening in that else case it is happening another if else so here we have a situation where we need to check something else so here I’m going to put else if so if this here results as no please then check something else and for that we use this else if statement so I’m going to say else if please check if my BMI is greater than 25 then so in this situation we also want to execute one line of code and that line of code is going to be to write out to my user that his BMI is greater than 25 which means that he is overweight so let me copy this message here and we are just going to adjust it so I’m just going to say you are overweight like this okay so if his BMI is less than 18.5 I’m going to write out that he’s underweight else if his BMI is not lower than 18.5 I’m going to check whether his BMI is greater than 25 if yes I’m going to write out that he’s overweight if no we are also going to execute something else and that that is this situation here so in this case he has a normal weight so let me again copy this message here and let’s really put here a normal weight like this okay so in this situation where his BMI is not great not lower than 18.5 nor it’s greater than 25 we are going to write out our user that he has a normal weight and with this here with this code here we have really covered this part of diagram here okay and then after this code here executes we really want to write out our BMI to to to the user so we have to this shape here which really translates into our our C out command and we want to write out your BMI is and then let’s write out BMI like this okay and this line of code as you may already see from our diagram is going to execute in either this case or this case or this case because after we have finished with this decision part we have executed only this part of codee or this part of code or this part of code here so whichever of these three has been executed after this code here we are going to execute this line here in either this situation or this situation or this situation and that is going to be writing out to my user what his BMI really is and that is the last thing that we need to put so the last part of our code and now if I run my program okay it asks me to enter my weight and my height and my weight is I believe 6 66 kilg I think and then my height is 1.75 so let’s press enter oh no I’m overweight how my BMI is 6 6 okay guys for a moment I thought that I am overweight but I am not definitely this 66 BMI overweight so that means that we have a bug so where are you sneaky little buggy bug well if you can see where our bug is then write that in the comments down below and I already see where potentially our bug could be so this formula here it should really go like this so we should divide our weight with our height to the^ of two which means that we should put parentheses here like this because and that is this calculation here so in this situation our weight is going to be divided with our height and after that that number is going to be multiplied with our height so we are going to get larger BMI than we should really so we should put this code here in parentheses so that was the first bug that really happened in these tutorials okay I’m going to leave this part I’m not going to edited out okay and now if I enter my weight um let’s see 66 and then my height 1.75 okay so I am 21.5 that is my BMI and I am off a normal weight so we have really built our BMI calculator and this is the formula that you should use for metric system and if someone else wants to rewrite this program for pounds and inches and um feet you can write that in the comments down below and as I said I’m going to pin that comment up so that everyone can see it if you of course get it right I hope you like this video if you did make sure to write it in the comments down below and I’m going to put more videos like this and also subscribe to my channel and click the Bell icon so that you are first to be notified when I publish a new video share this video with anyone who would like to learn programming or also with Fitness people that would just like to use this program and if you don’t know how you can run this program without using using visual studio I’m going to link my first video here and at the end of that video I’m explaining how you can run the program from Windows and that should give you an idea on how you can really share your applications with other people so make sure to subscribe like leave a comment share whatever you want and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners in this video I wanted to explain the concept of turnery operator also known as conditional operator and also some people call it shorter version of IFL statement it is an important concept that you are definitely going to come across in your programming career and you should understand it and I’m going to explain this concept through building fun and simple program that is going to be a guessing game so let’s explain the rules of this game we need to have two users and our first user which I’m going to call host user has to enter a number after that we have to delete that number from our console so that our second user cannot see it and then after that our second user which I’m going to call guest user has to enter his guest so that he can guess what number our first user has entered so this way we can really make a fun program and learn an important concept at the same time and at the end of this video I’m going to give you an idea or multiple ideas how you can really upgrade this program on your own so make sure to watch the entire video now that we have explained the rules of this game let’s start building our program and the first thing that we need to do is create two variables since we said that our user is going to be guessing numbers those two variables are going to be definitely a number type and let’s say that that type is going to be integer so integer numbers and we need two variables the first one is going to hold our first user number so our host user number and then our second variable is going to hold the number that our guest user enters so let’s call those to host user number like this and then the second one let’s call guest user number like this okay and now after we have created these two variables the next thing that we need to do is let’s write out a message to our host user so that he knows that he should enter a number so let’s say see out and let’s just say host like this so that our host user really knows that he should enter a number and I’m going to accept that number in my console using the C in command and I’m going to store that number inside our host user number variable so with this line here we have entered our host user number and what we have said at the beginning of this video is that after our host user enters the number we really need to delete that number from our console so that our second user is not able to see it and the command that I’m going to use for that is going to be this command here so I’m going to say system and then I’m going to say CLS which is which is really going to clear our console so clear our screen and now if I run my program let’s see how far we have made made this program here we have this line This seventh line which says host user and it expects our host user to enter a number so I’m going to say for example seven and then after I press enter this number here really is deleted so our console has been cleared and now after this we need to ask our guest user to guess the number that our host user has entered so I’m going to stop the program and next we need to write out a message to Our Guest user so I’m going to say guest like this and after that we need to accept what our guest user enters inside our guest user variable so I’m going to copy this line here like this and instead of accepting that value inside host user number I’m just going to change this to guest user number and after this part here we really have both accepted our host user number and our guest user number and now we need to compare these two and see whether our guest user has guessed correctly our host users number so how we can do that if you are familiar with f l statements and I’m going to link that video here and if you are familiar with them that is really one way to solve this problem so you can solve this problem with IFL statement and I’m going to do that and then we are going to really convert that IFL statement inside into a tary operator so how we can solve this with IFL statements well it is quite simple solution so we have to say if and then inside these parentheses I’m going to really compare these two numbers so host user number and guest user number so I’m going to say host user number is equal to guest user number like this and then if this here results as true so if these two if these two numbers are the same we can write out a message to our user and that message is going to be let’s say correct like this so our user has guessed correctly but in a situation where this here results as false we can use our else statement and in this else situation let’s write out a different message to our user so let’s say see out and then inside this these quotation marks let’s say failed like this so that in the case where these two are the same number we can write out to our user that he has guessed correctly and then in a situation where these two are not the same we can write out to our user that he has failed in guessing so if I run my program now you see that it expects our host user to enter a number and I’m going in this situation to take the role of both host and guest user so I’m going to enter seven for example and then you see that that seven number has been deleted from our console because of this command here and now our program expects Our Guest user to enter his number and I even forgot what number I entered so let’s say that our host user has entered for example 11 and you see that our program says you have failed so you haven’t guessed correctly and one more time let’s enter for our host user number five for example and then let’s say that our guest user also says number five and our program says you have guessed correctly so this is one potential solution for this problem but I wanted to show you how you can solve this problem using Turner operator and now I’m going to comment this code here like this so this is a comment for multiple lines of code and let’s really translate this part of code here into a turn operator like this okay so how does theary operator work well it is the only operator that takes three op runs or works on three operands and the first opun is going to be a condition so I’m going to copy this condition here which is is our host user number equal to Our Guest user number so that is going to be the first uun and after that first uun we need to put this question mark and then after this question mark we have the following Behavior if this expression here so if this condition here is true we are going to execute one line of code or actually one part of code and that is going to be this line here but in the situation where this here results as false we are going to execute whatever we put after this column sign so that is going to be our C out failed like this and this is really the Syntax for our Turner operator and as you can see it really is a shorter form of if else statement so this line of code really means please check this condition here and if this condition here results as true execute this part of code here but if this code here results as false please execute whatever I put after my column sign so that is going to be our see out failed and now if I run the program it asks again for our host user to enter the number let’s enter number nine for example and then our guest user has to guess the number so let’s enter for example number three and you can see that our program says to Our Guest user that has failed in guessing so there are a couple of ways in which you can really upgrade this program and I’m going to give you a few ideas and the first one is for example you don’t have to use int type here so you can use different data type data type you can guess characters or you can guess even strings if you know how to and if some of you want to play with that and try that on their own and you get it right make sure to paste that code in the comments down below and I’m going to check it and if you get it right I’m going to pin that comment up so that everyone else can see it so that would be one way of upgrading this program another way to upgrade this program is to make it easier to your guest user and how do I mean easier well you can really limit your host user to enter a number that is going to be in a certain interval for example you can say to your host user that he can enter only a number that is going to be between let’s say 1 and 15 or 50 it depends on how difficult you want to make it to your guest user and that would be one way also of upgrading this program and another and the best way of upgrading this program is going to be when we learn loops and when we do that I’m going to link the video here and how you can upgrade this program using Loops well you can give your guest user multiple attempts so you can say for example let my guest user guess three times or five times before I say game over you have failed okay so I hope that you have liked this video and if you did make sure to subscribe to my channel share it with your friends and with other people that would like to learn programming and I’m going to see you in my next video bye hi guys welcome to my channel welcome to another C++ for beginners tutorial and I’m really excited for making this video because this video is going to be a milestone and in this video I’m going to show you how you can build your own calculator application so if you have watched my previous videos and you are also so watching this one and you build this application together with me you should really feel proud of yourself because you have made a great progress so let’s build that application now here I have opened my visual studio and let’s very quickly explain what we want to do before we start write code so I want my user to enter two numbers and also operation sign and then depending on those two numbers and that operation we want to calculate the result and then write that result to our user so so the first thing that we need to do is we need to declare variables that are going to hold those numbers and that operation sign and since we want our user to be able to enter decimal point numbers so not only integers but decimal point numbers as well we are going to declare variables that can hold decimal point numbers and those can be float and uh double so let’s declare float variable for example and let’s call it num one which is going to hold our first first number and also another one which is going to be called number two and hold our second number and after we have declared these two let’s also declare a variable that is going to hold our operation sign and since that is a character we need to use a character type for that variable so let’s say Char and let’s call that variable operation like this okay and now after we have created these three variables we can really allow our user to enter his numbers and his operation and we want to do that in the following order so we want to say C in and then first our user has to enter the first number and then after that we want our user to enter the operation sign so let’s say operate operation like this and after that our user has to enter the second number and this window here guys this is called Intellis sense and it really makes your code writing easier so you can navigate this list here using your arrows and then you can also use tab key so that it finishes your variable name for you if you want that okay so now we have entered our first number our operation and then our second number as well like this and another thing that I want to write out to my user because this is going to be the best calculator ever let’s write that uh title for user so let’s write here before he enters anything a quick title so let’s say see out and then let’s say that it’s going to be called code Beauty calculator you can call yours whatever you want so like this and let’s add these here so that it looks prettier okay and now if I run my program well you see that it really looks like this and now we are able to enter one number after that we want our user to enter operation like this and then we want to enter the second number like this and then after we press enter our program should really calculate the result of this operation in this current situation it is not really doing anything else because we haven’t written code for that okay so now let’s solve this problem that we have here so at this point you should really have an idea yourself on how to solve this problem using IF El statements and also nested IFL statements so if you are not familiar with those I’m going to link one of my previous videos here so make sure to watch that video as well but in this this video I want to show you the more appropriate way for solving this particular problem and that is going to be using switch case statement so what is switch case statement well switch case statement is as well as if else statement used when you want to execute a block of code depending on a certain condition so depending on a value of a certain variable but switch case statement is particularly used in a situation where that variable can potentially hold a lot of different values and if you look at our operation variable here you should really notice that this variable can hold six different values and what are those six well our user can decide to enter operation for adding subtracting multiplying dividing modulating and then what if our user does not really want to use our application but he wants to hack it so if he decides that he doesn’t want to enter one of these five but he really enters some nonv valid arithmetic operation we should also cover that case so we should really as programmers predict all these six situations and cover all these six cases I’m going to add one end line here so that my text is formatted nicely and then let’s really show how this switch case statement is used so the first thing that I want want to write is switch like this and then if you press tab you see that you are getting some default Syntax for this switch case for now I’m going to delete that and I want to show you how you can really write that yourself and inside these parentheses here I want to write out the name of my variable that I want to switch through or switch on and that is going to be my operation variable because depending on the value of this operation variable we are going to perform a different calculation on these two number variables so I’m going to say here operation and then in these curly brackets here we really want to cover all our cases so the first case let’s say that our first case is going to be a situation where our user enters minus operator so I’m going to use these single quotes and then inside the single quotes I’m putting the value that our variable can potentially hold and that is this minus operator here and after that I’m going to add this column sign here like this and then after this column sign I really want to put out whatever code I need to execute in the case that our operation corresponds to to this sign here and what I want to execute in this case is I want to say see out and let’s WR write out our first number and then let’s write out our operation and then let’s write out our second number so that our user really gets a night nicely for method text and after that I’m going to add this is equal to sign and here we are going to put really our result and that result is going to be our first number minus because our user has entered this minus minus so our first number minus our second number so let’s write out number one minus number number two like this and after we have covered this first case we need to add one more keyword and that is going to be break and this break keyword is really used so that our switch case statement knows where our first case ends so I’m I’m saying here whenever my operation is equal to this sign here you are going to execute this code and you should really stop executing this CA that case when you come to this break statement okay so this is going to be our first case our second case is going to be situation where our user let’s say enters this plus sign and in that situation we really want to write out to our user this text here so the same text we want to really write out our first number Plus in this case our second number and then is equal to and then we are going to add really this calculation here so number one plus number two like this and in this situation as well we want to add this break statement at the end like like this okay so these are two potential cases and now I’m going to really write out the rest of them so I have written out these remaining cases and in each of these cases we really want to write out our user his first number after that his operation second number and then this equal sign and then in each of these cases we are performing adequate calculation so we are performing that operation that are user has entered and writing that result out to our user and after each of these cases we really need to put this break statement and you really have to put this break statement because if you don’t you are going to get your program to behave as you don’t want it to and as you can see in this situation here so in this situation where we are trying to modulate two numbers we are getting an error and if we hover over this number here you can see that our error says expression must have integral or unscoped enum type what does that mean well if you haven’t watched my video on operators I suggest you to do so I’m going to link it here and here I’m quickly going to explain what is happening so this operation here this modulo operation is Operation that is really going to give you a remainder after dividing two whole numbers and since this number here and this number here are of type float we really cannot perform this calculation on these two variables so what we have to do is we first have to check whether this number is really a integer number whether it’s a whole number and this number as well and since our float can hold decimal point numbers and integers as well we really in certain situations only can perform this modulating operation and let me explain what I mean by this that our float can hold can hold decimal point numbers and integers as well well this variable here this number variable can really have a value of 5.5 for example but it can hold as well value of 5.0 which is really integer value so it’s 5 only so now what we have to do is following I’m going to delete this line here and we first have to check whether both of these numbers are integers and in that situation only we can perform this calculation but in a situation where one of these two or both of them are not integers we are going to write out to our user that that operation is not valid so I’m deleting this code here and then let’s create two variables so let’s create a variable of type Bull and let’s call it is num one integer so this variable is going to hold a value that is going to tell us is our our number one integer value and let’s also create another variable that is going to be called is num two int and as well this variable is going to tell us whether our number two is integer and let’s develop now an algorithm so that we can know whether our number one or number two is integer number and I’m going to ask you to write your Solutions in the comments down below so pause the video think about it and then write your solution and without using any already made functions as programmers as Engineers you should really be able to figure out the solution on your own so applying the knowledge that you have gotten from my previous videos or this one but I’m going to say from my previous videos so pause the video and write your solution so this question is not really a question of programming it’s more of a question of thinking in a logical way and if you remember when we were talking about casting operators we said that a casting operator is used to convert one data type into the other and I’m going to link that video here if you haven’t watched it make sure to do so and here we are going to try to convert our floating Point number so our number one and our number two into integer data type so into integer number and how we are going to do that well we are really going to say int so this here is going to cast whatever we put after it into int type and we are going to say please try to convert my number one into integer and how does this work well for example if you have a number one that holds value of 5.7 when you try to convert that value into integer what it does really it says that 5.7 so that7 part really bye-bye I’m going to take only this five part so only this whole part of a number so in that situation we are really going to get only five from this 5.7 floating Point number so what I’m going to say now is I’m going to say whatever this expression here this casting operation results with I’m going to really try to compare that with the initial value of my number one variable so I’m going to say num one is my converted number one so converted to integer equal to number one like this and how does this calculation here work well as I said for example if you have a number one that holds a value of 5.0 so 5.0 this part here is really going to take this 0 Z part of a number and delete it it’s going to keep only this five so only this whole part of a number and after that we have this is equal to operator and then we are trying to really uh trying to really check whether that is equal to 5.0 and the result of this is going to be true so we with this here we have checked really whether our number is going to be equal to our floating Point number so this result here we are going to assign this value to our is number one integer variable so here I’m going to say Please assign the result of this operation to our is number one integer variable like this and let’s put this expression here so this whole expression inside these parentheses so that we really can see that this is an expression and the result of this expression should be assigned to this is number one one in variable and now we are going to do the same for our is number number two so I’m going to say is number two integer and we are going to say that that is equal to I’m trying to convert my number two to in and then compare that to my float number two and after this we really can now do our algorithm to perform this modulo where it is this modulo operation on our two numbers in the case where both of them are integers so here I’m going to say if is my number one int and then also is my number two int like this in this situation here I’m going to perform this modular operation and as you can see here I have used only these two bull values here in this expression so I didn’t say please check is my number one int equal to True like this nor I have said please check is my number two equal to True like this because uh this here this part here and this part here are really redundant and what this expression here translates in it translates in is true equal to true and that is really redundant so I’m going to remove this part here and this part here and it should work fine like this for Boolean values so Boolean variables and in this situation here where both of these are integers we really can perform this modular operation so in this situation here I’m going to take this code and I’m going to copy it like this and I’m going to say please write out my number one then my operation then my number two is equal to sign and then here I’m going to put adequate calculation so I’m going to say take my number one convert it to to int and then modulate that with my number two that is as well converted to int like this okay so in this situation where both both of these are integers we are going to really write out the result of modulating operation to our user let’s add empty space here and here as well but in the situation where one of these two is not integer or both of them are not integers I’m going to say else and in that situation I really going to write out my user that this operation is not valid so let’s say just not valid like this so in any of these two situations whether we write out this line of code here or this line of code here after that we are going to come here and here we have to put our break statement like this so after each case comes break and now we can really test this part of code here that we have written so if I run my program the first thing that that I want to test is let’s say 5.5 Plus 2.2 and we get the expected result so 7.7 let’s run our program again and this time I want to check whether my modulo operation is working as it should because this code here is really specific so let’s say five modulo 2 like this and as well we have gotten correct result and let’s run our program one more time and here I really want to trick my program and try to make it to modulate two numbers that are not whole numbers so 5.2 modulated by two and as well our program works as it should so it says not valid operation okay so if you have noticed uh in this current situation I really have to run my program each time that I want to perform a calculation and that is because we haven’t learned anything about iteration or loops so far and those videos are coming so make sure to subscribe to my channel and also click the Bell icon so that you are notified when I publish those videos and in that situation where we learn iteration and Loops we are really going to be able to run our programs indefinitely which means to run our calculator application until we really decide to stop it okay so after we have covered these five cases here there is only one situation left to cover and that is situation where our user enters any other sign that is not one of these five here and that situation we are really going to write like this so we are going to say default so in the case where our user enters operation that is not going to correspond to any of these five signs we are going to execute our default situation and here we really want to write out a meth message to the user that is trying to hack our program so you can write whatever message you want to the user that is trying to hack the program that you have worked so hard to build and I’m going to put here okay you really can write out whatever you want to that type of user like this so in the case where our user has not entered this nor this nor this nor this nor this which means our user has entered some other sign we really want write out not valid operation and let’s test now our default case so I’m going to run the program and I’m going to take the role of a malicious user that is going to try to hack your application and I’m going to say please try to perform this calculation here so 5 z9 no not really you have been smart enough to predict his malicious behavior and you have said if you haven’t put here a valid arithmetic operation then no I cannot help you this is not a valid operation and there is really one thing that you have been wondering about for the most part of this video and that is this break statement here so why I have said that after each case you have to put break and now you want me to explain that do you do you really wouldn’t that be a spoiler for a situation where you decide on your own to delete this break statement here and then run your program even though I have said not to and you enter some calculation and you remember that I have said that you are going to get unexpected Behavior but was she lying was she not and then you decide to execute this calculation and boom here is your unexpected Behavior so what has happened here well your program has really recognized that this operation here that you have entered is this minus operation and after that is it has written out this number one so 7 and then operation minus and then this number two five is equal to and then it has calculated number one minus number two which is 7 – 5 and that is equal to 2 and after that it has really continued executing other cases which means it has come to this line here and then then it has written one more time your number one minus number two so 7 – 5 is equal 2 and then it has performed this number one plus number two calculation and 7 + 5 is going to be equal to 12 and then it has come to this first break command and it has has stopped the execution of your switch case so why has this happened it has happened because you haven’t put this break command here and in that case program is really going to be executing your cases until it comes to the first break command and if you have noticed here in this default situation I haven’t really put any breaks you can as well put your break statement here you can say break here but even if you don’t if even if you didn’t put your break here and your program really tries to execute something after this line here there is really nothing to execute so you are not going to get a problem in this situation here I hope that you like this video and that you have built your own calculator application and if you did make sure to subscribe to my channel and click the Bell icon as well share this video with anyone who would like to learn programming and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners and in this video I wanted to deepen your knowledge of switch case so if you’re not familiar with switch case Cas at all if you haven’t watched my previous video you definitely have to do that the link is going to be here and in this video I wanted to give you some more examples that you can practice on and introduce some things that I haven’t mentioned in my previous video as well so in this video we are going to build a program that is going to require from our user to enter a year and a month as well and then our program has to write out how many days there is in that month so how we are going to do that let’s see but first you have to subscribe to my channel and like this video and also share it with your friends as I explained there are two things that we need from our user to enter and those are going to be month and year so let’s create two variables let’s say year and month like this and now I’m going to write out the message to my user so that he knows that he should enter those two values so year and after that please enter month like this and let’s store now the values that our user enters so the first one is going to be stored inside our year variable and then the second one is going to be stored inside our month variable and now we have really covered everything that we need from our user now our program has to check these two variables and then decide how many days there is in that month of that year and we are going to do that using switch case statement but but there is one thing that I want to mention before before we start write our switch case and that is you have to take in consideration that there are years that are leap years and that have one day more than the year that is not a leap year and there is a rule for that so I’m going to copy the rule that that is going to help us to determine whether a year is a leap year or not and that is this rule here so any year that one modulated by four gives the result of zero and at the same time that year modulated by 100 is not equal to zero or a year so that same year when modulated by 400 is going to give the result of zero and this here is going to be a definition for our leap year which means a year that has 366 days or a February inside this year has 29 days that’s why we need this rule here so now let’s write out our switch case statement and if you haven’t watched my video on switch case statement I am going to link it here one more time because you really need to understand the switch case and then you can come back to this video and deepen your knowledge on switch case so switch this is going to be the syntax of our switch case but I’m going to delete this default syntax we want to write it ourselves so inside these parentheses here I’m going to put the variable that we are going to switch through and that is going to be our month variable like this so whichever value our user has entered for our month we are going to cover all the potential values that our month can hold in our cases so the first case that I want to cover is going to be the most specific one and that is going to be our February which means second month so I’m going to say in the case that this month variable is equal to 2 which means that our user is requesting from us to determine how many days our February month has we really in this case need to check whether that year is a leap year or not and then if that year is a leap year that means that our February has 29 days and if not which means if this condition here is not true that means that our second month has 28 days so I’m going to copy this expression here this condition and also if you are not familiar with Turner operator you have to watch that video because in this video we are going to use that tary operator you can do it as well with if else statement but I want you to practice also this because you will uh you are going to actually come across it in your programming career so here I’m putting this condition for a leap here and I’m going to say this question mark and then I’m going to say if this here is true you have to write out this line of code so you have to write out C out and if this condition here is true that means that our year is a leap year which means that our February our second month has 29 days so let’s say 29 days month like this okay and so that is in the situation where this here is true and in the situation where this expression here results as false I’m going to put here a column sign and then I’m going to write out a different message and that is going to be that this second month in the case where a year is not a leap here has actually 28 days so after we have written this expression here so this Turner operator we have really write out how many written out how many days there is in our February month and after each case I have to put break so in this case as well I’m going to put put our break command after it and with this example with this case here we have really covered our second month which was the most specific one now we are left to cover the remaining months which means ones that have 31 days and also ones that have 30 days and here I’m going to show you something that I haven’t shown you before let me scroll this code like this maybe okay and then there are some months that contain 30 days and some that contain 30 31 days and the ones that contain 30 days are following so I’m going to write out case like this and then the the first month that contains 30 days is going to be April so fourth month and then also our sixth month contains 30 days and then also let’s see 4 6 9th and then November as well like this and here we have written out really all the months that contain 30 days okay and now I’m going to at the end so at this case here write out a message let me copy this and I’m going to paste it and I’m going to say 30 days month like this okay and I’m going to put break command here as well so let me explain this part of code here because we haven’t seen this part of code here before this break statement what it does is when your program so when your program execution actually comes to this case here it says okay my month is equal to two so I’m going to execute this part of code here and then it comes to this break command and this break command really breaks the execution of your switch which means that when your program comes to the first break command it is going to stop the execution of your switch there which means that it is not going to check any more cases and because of that you can really hear so you can put more than one month you can put all months that have 30 days like this and then you can write out the same message and put one break command at the end and how that is going to really behave inside your program in the case where user enters for example month six your program is going to check whether that month is is equal to two and it’s not going to be then it’s going to check whether your month is equal to four not as well and then it’s going to come here and say okay this is sixth month this that our user has entered so please execute whatever code you find until you come to the first break and that is going to be this here which is empty so nothing and then this here which is as well empty so nothing and then it comes to this code here and that line so this code here contains this see out message 30 days month so it is going to write out this and then it really comes to this first break command and it breaks your switch which means that it is not going to check any other cases that come after this break okay so in any case whether your user enters month four or 6 or 9 or 11 your program is going to execute this line here and then break your switch so with this part of code here we have really covered all months that have 30 days and now we are left to cover the ones that have 31 days and those are months uh let’s see that is our January and then our third month and our fifth and our 7eventh month like this let me scroll it a little bit more okay and then we have as well case 8 and case 10 and as well our December has 31 days so there are seven months that have 31 days uh five five months that have 30 days and one month that has 28 or 29 days and in this line here we are going to write out a message that is going to say 31 days month and as well here we have to put our Brak command like this okay so I am going to run my program now and show you how this program behaves so our program is asking us to enter a year and month let’s say a year is going to be 2020 which is current year and then a month is going to be let’s see it’s April so 4th and it says that it is a 30-day month which is April April has 30 days and let’s run our program one more time and let’s say 2020 again and let’s say that we want check our second month and it says that February of 2020 has 29 days which is correct as well and there is really one more situation that I want to cover and that is going to be a situation where our user enters a number that is not a valid month number and that situation I’m going to cover with my default statement so I’m going to write out default and in this default case as well I want to write out a message to my user like this and inside here I’m going to say not valid Val which means that our user has not entered a valid month number so let’s run our program and if I enter year 2020 and for a month let’s say minus 5 for example you see that our program has written out not valid okay so I hope that you have liked this video and if you did make sure to subscribe to my channel and share it with anyone who would like to learn programming as well and today is May the 2nd that’s when I’m filming this video I don’t know when I’m going to publish it probably in a couple of days and currently we are a little bit over 900 people so let’s see how fast this channel can grow thanks for watching and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners and in this video I wanted to talk about loops more specifically about while loops Loop so what are Loops what are they used for how they are used well Loops are used when you want to execute a block of your code multiple times while a certain condition is true which means you give a certain condition to your Loop and you say you are going to execute this block of code while this condition here is true so however many times it is necessary and one thing that I want to mention before we start uh on our example is the following so selection sequencing and iteration those three are three main building blocks of programming languages and when you really learn and understand those three you should be able to solve any logical programming problem and in this video we are going to talk about iteration we have already talked about sequencing and selection in my previous videos so what is sequencing well that means that your code is executed sequentially which means one line after the other and then selection that would be really when you want to select one part of the code rather than the another one which means you are going to tell to your program under a certain certain situation it really has to select which block of code it is going to execute the same would be when you for example come to a crossroad and then you have multiple ways in front of you but you really have to select which one you are going to take because you cannot take all of them and then iteration is going to be really iterating or executing multiple times a certain block of code certain part of code while a certain condition is true so while a certain condition is fulfilled you are going to execute the same block of code so let’s see our example now so I’m going to paste the text of my program here and this is our task so it says that we have to write out all the numbers between 100 and 500 that are divisible by three and by five and how we are going to do that first I want to explain you the algorithm so how we are going to solve this problem and then we are going to translate that algorithm into our code so this interval here so this 100 to 500 interval we are really going to iterate through all of the numbers of of this interval and we are going to do that using while loop and then for each of those numbers so for each value of our counter we are going to check whether that counter value can be divided by three and by five and if yes we will write out that value if no we are just going to continue to the next iteration so as I already said we are going to use while loop to solve this problem and and we are going to start our V loop with the counter value of 100 and in each iteration we are going to really increase the value of our counter so 101 102 and three and we are going to stop the execution of our V Loop when this counter value comes from 100 to 500 And1 which means 500 value of counter is going to be our last iteration through our Loop so so the first thing that I’m going to do is I’m going to really declare a variable that is going to hold our current counter value so I’m going to call it int counter like this and initially I’m going to set the value of my counter to 100 okay so this is our initial value of our counter and then let’s see what is the syntax of of our while loop so so we write out while like this and then in these parentheses we are going to put our condition so how long our Loop is going to execute and then in these braces here so in this curly brackets we are going to put which block of code our while loop is going to execute while this condition here is fulfilled and let’s now fill these parentheses here so as I’ve said our counter has to go from 100 to 500 which means since it starts on 100 here we have to say to our while loop that the execution of this block of code here really has to stop when our counter comes to 500 or 5001 500 is still valid value so that we are going to say like this so we are going to say counter like this and then we are going to say there are two ways to write this condition the first one is to say while our counter is less or equal to 500 or you could as well said while our counter is less than 51 so that is a valid situation while you’re executing your Loop so while your counter is less than 501 or while your counter is less or equal to 500 I prefer it this way but whichever way you put it is going to work so now that we have put this condition here we really can write out this code that comes in this while loop block so what we are going to execute while this condition here is true and that is going to be following inside this block of code here we really have to check whether our current value of our counter is divisible by three and by five and if it is we have to write out that number to our user if no we just need to continue to our next iteration of our Loop and how we can check whether this counter value can be divided by three and by five well we use modulo operation for that if you haven’t watched my video on operators I’m going to link it here make sure to watch it and here we really put the following so we can say if and also if you’re not familiar with if statement going to link it here so if our counter value so our current value of our counter after you modulate it with three if the result of that is zero that means that our number so our counter is divisible by three and we also have to check whether our counter is divisible by five as well we use this modul operation here and we say whether our counter modulated by five is going to equal to zero and if this condition here is fulfilled we really have checked this part of our task and if this part here is true we have to write out that number so in this situation here I’m going to say C out like this and I’m going to put inside here let’s see we are going to write out the value of our counter like this and we are going to write out is divisible like this and let’s put one end line after this line of code here because we are definitely going to have multiple lines so we want our lines to be formatted nicely okay so this part of code here has really covered this divisible by three and five and write out that number if it is divisible by three and by five five and there is really one very important thing that you have to keep in mind each time that you write loops and that is the following so you have to give your Loop away so that it can stop executing this block of code here so under a certain condition it really needs to leave this block of code here because if you don’t do that you are going to end up with an infinite Loop you are really going to lock your program in an infinite Loop situation and this code here this Loop here that we have written this is really an infinite Loop so let me prove that I’m going to run my program and as you can see the console is empty so I enter any value enter whatever it is not really reacting and why has that happened well your counter value starts with 100 and then it comes to your while loop and your while loop checks this condition here so it checks whether your 100 is less or equal to 500 and the answer to that is yes which means that your program execution can enter this block of code here so it can start executing your Loop and then it comes here and it says please check whether my counter which is 100 is divisible by three and the answer is no so this part of code here is not even going to be checked because of the nature of this end operator it really needs both of these operant to be true in order for this expression here to be true because this here is not true it’s false 100 is not divisible by three it is not even going to check this condition here and this line of code here is not ever going to be written so after that it comes here to this closed curly brace and then because of the nature of while loop when it comes here it really goes back here and then it checks again your value of your counter whether that is less or equal to 500 but nothing has changed your counter value is still 100 and then it is going to enter again here and then really repeat that process over and over again and your program because of that is going to be locked inside an infinite Loop so let me prove that really because here we have an empty console and you cannot see what is happening in the background so I’m going to stop my program and I’m going to write out here let’s write out C out like this and let’s write out infinite Loop and if I run my program now as you can see your program has really entered in an infinite Loop and it is writing out this infinite Loop text for infinite number of times so as you can see it says infinite Loop okay and this execution here is not really going to stop until your program’s memory is full and then it’s going to crash so whatever code in the case that you had some code here it is not ever going to be executed so I’m going to stop this here and I’m going to delete this part of code here because we don’t really need it what we need is we need to put here a certain code that is going to help our Loop to really stop the execution at a certain point and the code that we need to put here is going to be really following it’s going to be counter plus plus like this what this code of here this code here is going to do in each iteration it is going to check whether this condition here is true if yes it is going to write out this part of code here and then in each iteration it is going to really increase the value of your counter by 1 which means in your first iteration is going to be 100 after that it’s going to be 101 102 103 and it is going to really go until it comes to 500 that is going to be the last iteration of your program and then when it comes to 5501 this condition here is not going to be true anymore and then your Loop is going to check really whether 51 is less or equal to 500 and the the answer to that is not going to be no so your program is going to really stop the execution of this Loop and come here and then really stop your program so if I run the program now I’m going to prove that okay so this has been executed very fast and as you can see this is the result of your program it has really written out all the numbers that are divisible by three and by five and you can check check all of these numbers using your calculator if you want so with this expression here with this counter Plus+ we have really both iterated through all the numbers between 100 and 500 because we check our counter value here and then also we have we have really given a way to our Loop to stop the execution of this block of code here when this counter value comes to 51 and this is the result of our program okay so I hope that you have liked this video any questions that you have write those in the comments down below and I’m going to answer those make sure to subscribe to my channel and share it with all your friends anyone who would like to learn programming and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners today I want to talk about while loop and I have already made one video on this topic and if you haven’t watched that I’m going to link it here so make sure to do so and why I’m making this video because I really believe that watching One video and doing only one example is not going to be enough for you to really Master a certain concept it wasn’t for me it is not going to be for you as well and that is completely okay so we are going to show you how this while loop is used on one more example and the program that I want to build today is going to be a program that has to count how many digits the number contains so our user is going to enter a number and then we are going to write a program that is going to count how many digits there are in that number so let me copy the text of that program like this and it says count digits of a number so let’s do do the first thing that we know that we have to do and that we know how to do as well so that is going to be write out a message to our user so that he enters that number and then also allow him to enter that number so I’m going to say see out and then I’m going to say number like this and then I’m going to allow my user to enter that number before I can enter it I have to really declare a variable that is going to hold that number so here I’m going to say int number like this and then here after we have written out this me message to our user we are going to say C in number like this so after our user has entered his number we really have stored it in our number variable and now we are going to write out the program that is going to count the number of digits of this number here so let’s first see one specific situation that can happen and that is going to be when our user enters number zero so this number holds a value of zero and in that situation I’m just going to write out a condition so I’m going to say if my number like this is equal to zero in that situation situation please just write out a message to my user like this you have entered zero okay like this and I don’t need semicolon here this looks better so this here is a situation where our user has entered number zero and in that case we just want to write out that he has entered zero and now we are left with the main problem of this task and that is to count the digits of the number that our user has entered and that part I’m going to put in my else block like this and if you’re are not fam familiar with if else statements make sure to watch that video it’s going to be linked here so here in this else block I want to write the program that is going to count the digits of a number which is not zero definitely because if we come here then our number is definitely not zero so how we are going to do that well let me show you the algorithm on an example let’s say that our user is going to enter a number 1325 for example that is the number that our user enters so how we are going to count the digits of this number well first we are going to have a variable that is going to be called counter like this and initially we are going to set the value of that counter to zero so that is initial count of our digits of number and then we are going to to use while loop to iterate through all the digits of this number and in each iteration we are going to do following so we are going to come here and then we are going to say okay take this last digit of my number and really remove it I don’t need it and as well increase the number that my counter is holding so increase the number of digits because we have removed one digit and then in the next iteration we are coming here and we are as well saying okay so take that last digit and increase my counter like this and then in the next iteration take the last digit increase counter and in the next as well take last digit increase counter and then when we come next time we really have nothing more to count so there are no more digits left our number at this situation here is going to be equal to zero and then we are going to know that there is nothing else to count and then we are really going to leave our Wild Loop and we are going to write out the value that our counter variable is holding which is at this situation for so let me show you how we how we can do that in our code so I’m going to delete this part here we don’t need it anymore and then here I’m going to write out the following so as we explained already first thing that we need to do is we need to create a variable that is going to be called counter like this or digits counter however you prefer and and that variable I’m going to assign a value of zero initially and after we have created this variable here now we need our while loop so while like this and then inside these parentheses here we really write out a condition that our while loop is going to check each time that it wants to run each time that it wants to iterate and that condition here is really going to be please check whether my number number is greater than zero okay so in any case where our number is greater than zero our while loop is going to execute this block of code that I put inside these parentheses and what we are going to put inside these parentheses well as we already explained previously first thing that we need to do is we need to say number is equal to number divided by 10 and what this line of code is going to do well let me show you that on an example let’s say that our number so our number holds the value of 1 2 3 like this and this line here really says the following it says Please do this expression here so calculate it and whatever this expression here results in assign that to my number variable and 1 2 3 so 1233 divide by 10 because this here is whole number so it’s integer and this here is whole number as well the result is going to be whole number and that is going to be 12 or 12.3 but but because result has to be the whole number that3 part is just deleted bye-bye so then this 12 in this situation is going to be assigned to our number variable okay so that means that this number divided by 10 is really going to take away that last digit which we have previously explained so I’m going to delete this part here okay and this line of code there is really one more way to write this so write the same thing so I’m going to comment this here and I’m going to write out number divided equal so number R like this 10 okay so this line of code here and this line of code here are exactly the same this is just shorter way to write this here and this looks prettier to me but you can leave whichever of these two you prefer okay so after we have taken the last digit from our number what we are left to do is to increase our counter variable by one so we can do that using our increment operator here so this part of code has really in each iteration taken away one digit and then increased counter variable so what we should do now after our while loop finishes we should really write out how many number how many digits our number contained so we are going to write out count number contains like this and then let’s write out the value that our counter variable is holding like this what is this oh it says count instead of C out okay and then let’s write out just digits like this okay so our number contains whatever our counter variable is holding digits so now I can really run my program and let’s do that so it asks us to enter a number let’s enter number 1 2 3 for example and it says that our number contains three digits which is correct so let’s very quickly explain one more time how this has happened so I’m going to stop my program and I’m going to say here that our user has entered number let’s say 1 2 3 so our program in this situation comes here to our while loop and it says please check whether the number that our user has entered is greater than zero and that number is 1 2 3 so 123 which is greater than zero so then our program can really start executing this wild Loop so it enters here and then it comes to the first line of this while loop and it says Please do this expression here and this expression here really just takes the last digit of our number away and that is going to be three so we are going going to take that away and then the next thing that needs to happen is increase the value of our counter since previously the value of our counter was Zero now the value is going to be increased by one so that is going to be one and after that our program comes here and because of the nature of the while loop it goes back here it checks again whether our number is greater than zero our number is 12 now so the one digit has been taken away so that is still greater than zero so then our number really comes here and then does does this line of code again so it says again please take away one digit from my number so then it takes away this two part and then it comes here it says now increase my counter because you have taken away one digit and that is going to be two now after that our program comes here and then goes back here checks whether our number so whether our one is greater than zero it says yes and then it comes here and it says says please divide my number by 10 so take away that one digit so that one digit is taken away and we are left with zero and then our counter is increased by one and then we are really going to come here again and then our program goes back and says please check whether my number so whether zero is greater than zero and then our program says no it is not so our while loop is not executed anymore and then it comes here instead dad and it writes out that our number contains three digits and there is one more situation that I want to show you and that is going to be what is going to happen in a situation where our user decides to enter a negative number so let’s run our program and let’s see how our program is going to behave in that situation so now I’m going to enter a number of Min – 335 for example and you see that our program writes out that our number contains zero digits why has that happened because our program has come here so it has increased our counter it has actually assigned our counter a value of zero and then it has come here and it is it has really tried to check whether our number is greater than zero but because our number is minus 335 that is not greater than zero so our program then really is not going to execute this block here but it’s just going to go here directly so then it’s going to write out that our number contains and the value that it has assigned to our counter here which is zero so our number contains zero digits and that’s the message that you see here so that is the explanation of this behavior and we need to solve that now how we are going to solve that well it’s pretty simple so here when we come to this else Block in this situation in this 14th line of code we are really sure that our number is not equal to zero because we have checked that here but here we really don’t know whether our number is less than zero or greater than than zero so here we really have to check whether our number is less than zero and if it is we really need to get the absolute value of our number which means if it is negative we have to convert it to the positive number so here I’m going to write out the following condition so I’m going to say please check if my my number is less than zero like this and then if this here results as true I’m going to write out here a way for my program to convert this number in case that it is negative into a positive number and how we can do that well we can really multiply that number by minus one so we can say number is equal to -1 * number like this okay so now if I run my program and if I enter value of Min – 236 for example our program now writes out that our number contains three three digits which is correct so how has this happened our program has come here it has really checked that our number is less than zero and then it has come here and it has said please do this expression here and then assign that to my number so -1 * 2 – 236 those minuses are going to really convert into plus sign and then 1 * 236 is going to be 236 and after that we are going to do the same logic here so we are going to write out that our program that our number contains three digits which is correct answer okay so that is the one situation that I wanted to explain and there is also another situation that I want to explain to you and that is what is going to happen in the following situation here so let’s see let me enter a number that is very very very large I I cannot even read this number here so how our programs going to behave in that in this situation here write that in the comments down below please before I execute this here and now if I press enter what is going to happen happen well our program has glitched a little bit and after that it has written out that our number contains 10 digits but this is definitely more than 10 why has this happened if you are not familiar with data types and overflow and concept Concepts that I have already talked about in one of my videos I am going to link that video here so make sure to watch that video and now I’m very quickly going to explain what has happened here but make sure to watch that video so when we have entered this number here let me stop this program and let me put let’s see let me put a breakpoint here and now I’m going to run my program and what this breakpoint is going to do it’s really going to stop the execution of my program at this line here and we haven’t talked about breakpoints or debugging programs previously but if you want me to include that in one of my next videos make sure to write that in the comments down below because I really believe that it is important for you to see how your program is executed line by line so to be able to follow it and in that way you can really discover any bugs and any situations that are tricky and that are happening inside your program so you can see and control that better so I have put here a break point and let’s now enter a number that is going to be a very large number okay so if I press enter now you see that my program has really stopped the execution here on this line here so after I have entered my number and then even though I have entered a number that is pretty large let’s hover over this number variable you can see that it is holding a value of 2 billion And1 147 million and something so this value here is definitely not equal to this value here but because our program has really gotten the value that is much larger than the maximum value that our int variable can hold it has really assigned the maximum amount that our int can hold to our number variable so that is what has happened in this program and if I click continue now you can see that our program writes out that our number contains 10 digits because it really was holding a 10 digigit number so that is the maximum amount that our int type can hold okay so that is the last situation that I wanted to explain to you and I hope that you like this video and if you have learned something new make sure to subscribe to my channel share it with all your friends anyone who would like to learn programming thanks for watching and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners and first things first make sure to subscribe to my channel and hit that Bell icon as well and share this video with everyone it really helps me to reach a lot more people and in this video I want to show you how you can reverse a number so your user is going to enter a number and what we want to do is we want to write out that number in Reverse so reverse the order of the digits of that number and here I have opened my visual studio so let’s copy the text of this task like this and the first thing that we need to do is we need to really create a variable so let’s say int number like this so that our user can enter his value in this variable here so let’s write out a message to our user so let’s say see out number like this so that our user knows that he should enter a number and then let’s accept that value in our number variable oh number like this now after our user has entered a number let’s really think about how we are going to solve this problem okay I get it so the first thing that I notice here is that we are missing a variable and that variable is going to be the one that is going to hold our reversed number so let’s declare it here I’m going to add it here and I’m going to call it let’s say reversed number like this and I’m initially going to assign it a value of zero okay and now the algorithm that we are going to use in order to solve this task in order to reverse our number so the algorithm that we are going to use is going to be the following so this reverse number here it is holding a value of zero initially and this number here is the one that our user enters so let’s say for example that our user enters a value of 123 like this so 1 23 so how we are going to reverse this number here the algorithm is going to be following so we are going to iterate through this number here so all the digits of this number here we are going to go through those and in each iteration we are going to do the following so the first thing is we are going to say please take this reverse number and multiply it by 10 so 0 * 10 is going to say to stay zero after that what we are going to do is we are going to take this last digit of our number that our user has entered and we are going to add that number to our reverse number so 0 + 3 and that is going to be equal to three and after that we are going to really remove that last digit of our number so I’m going to delete that digit and then in our next iteration what we are going to do is the same thing so we are taking this reverse number we are going to multiply it by 10 so I’m going to say 3 * 10 that is going to be equal to 30 and after that we are going to take this last digit of our number and we are going to add that digit to this number here so to this reversed number so 30 + 2 that is going to be equal to 32 and then we can really remove this last digit here so I’m going to deleted after that we are going to the to the third iteration and we are doing the same thing so we are taking this revers number multiplying it by 10 so 32 * 10 is going to be equal to 320 and then we are taking the last digit of our number this is this one here and we have to add that digit to our reversed number so 320 + 1 is going to be equal to 321 and after that as we did before we can really delete remove this digit here and then in my next iteration we are going to come here again and our Loop is really going to see that there is nothing else left to iterate through so there are no more digits and then it is really going to leave that Loop and at that moment we are really going to have our reverse number so 3 2 1 that is reverse number from the one that our user has initially entered so let’s now translate this algorithm into our code so the loop that we are going to use in order to solve this problem here is going to be while loop so I’m going to write while and then in these parentheses here I’m going to really give it an expression that is going to check each time that it wants to run an iteration and what we are going to iterate through are the digits of this number here so the number that our user has entered so I’m going to say please iterate through this part here so execute this block of code here which is currently empty while our number so number where is it is different than zero like this okay and inside here we are going to write out the algorithm that I just explained and the first step of that algorithm was really multiplying our reverse number by 10 so I’m going to say reverse number is equal to reverse number time 10 like this okay and the shorter way to write this expression here is going to be like this so you can really take this part here delete it and say it like this it is going to do absolutely the same thing so it is going to really multiply our reversed number by 10 and then whatever that results in it is going to assign that back to our reversed number so that was the first step the Second Step In order to reverse our number is going to be really to take the last digit of this number that our user has entered so how can we do that we can do that by modulating this number here by 10 so I’m going to declare a variable let’s call it um last digit like this and as we already said we can get that last digit if we modulate this number here where is it so this number that our user has entered by 10 okay that is going to give us the last digit of this number here and then what we really want to do is we really want to add that last digit to our reversed number that was one part of this algorithm so the next step so I’m going to say reversed number plus equal to last digit like this or you can write it a longer way but this way I pref I prefer to write it um this way but you can as well say reverse number is equal to reverse number plus last digit and after we have added that last digit to our reverse number there is one more step left to do and that is to really take that last digit from our number and how we can do that well we can really do that by dividing our number by 10 so you can say number is equal to number divided by 10 or number divided equal to 10 whichever way you prefer okay so this is really the algorithm that we explained and this step here you can can really write out the shorter way if you want so you can say for example reverse number plus equal to number modulated by 10 like this so you can make your code a bit more readable if you prefer it this way or you can leave this part here and then really add your last digit to your reversed number so now that we have done this algorithm here let’s write out our reverse number and see what we have done so I’m going to write out C out and I’m going to say reversed like this okay and I’m going to write out our reversed number okay let me delete these empty spaces and if I run my program now it asks us to enter a number so let’s enter number 1 2 3 and now if I press enter you can see that our program has really reversed this number here so let’s quickly one more time explain how this has happened so our while loop has really iterated through this number here so that in each iteration it is going to do the following so it is going to take our reverse number and initially that reverse number is equal to zero so it is going to multiply that by 10 so it stays zero and after that is going to take that reverse number and add it the last digit of our number that our user has entered so that is going to be in a first iteration number three so our reverse number in that case becomes three so 0 + 3 is equal to three and after that we are going to really take that last digit from our number that our user has entered so it has taken this three part away and then in the second iteration is coming here it is going to check again whether or not number is not equal to zero and our number is going to be 12 in that situation so that is not equal to zero so it enters here again it executes this block of code here so it says again please multiply my reverse number by 10 and assign that again to my reverse number so our reverse number was three and if you multiply by 10 if you multiply 3 by 10 you are going to get 30 after that it is taking the last digit of the number that our user has entered which is two now because three we have taken away in our previous iteration so it is going to add that two to my reversed number so number reverse number is going to become 32 now and then it is really going to take that last digit from our number away it is going to divide it by 10 so now we are left with number one and our reverse number is equal to 32 and then it goes back to to our third iteration it checks again whether our number so whether our remaining number is equal to zero and this one here is the value of our number so that is not equal to zero so this condition here is fulfilled it can enter the while loop so it does this algorithm here again so it says please multiply my reverse number by 10 our reverse number at this point is 32 so when you multiply that by 10 you get 320 and then after that it takes the last digit of our number which is going to be this one so this one here which is the last one remaining so it is going to take this one and add it to my reverse number as you can see here and then it is really going to take that last digit away so it is going to take all of these three digits away so it has taken them in a previous situations and now it has taken this one away as well so our number is equal to zero now now and then it goes back to our fourth iteration it checks whether our number is not equal to zero but in this situation our number is equal to zero so it really leaves this Loop here so it is not going to enter in this Loop anymore and then it goes here and it really writes out the value of our reversed number which is this line of code here so that is how we have reversed our number so I hope that you like this video and that you understand this explanation if you have any questions write those in the comments down below and also share this video with all your friends with anyone who would like to learn programming and thanks for watching I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners if you are already a subscriber welcome back and if you’re a new viewer make sure to subscribe hit that Bell icon as well and welcome to my channel so in this video I want to talk about the while loop and in my previous videos we have already talked about while loop and iteration in general so if you are not familiar with those make sure to watch that video I’m going to link it here but in this video I want to explain to you how and when is used do while loop and I’m going to give you an example of a program that is usually used and for example applications such as when you try to unlock your phone and then you are given three attempts to guess your pen and if you guess it correctly then you can proceed loading your phone’s applications further or for example if you want to unlock your credit card so you put your credit card in ATM and then it gives you three attempts to unlock your credit card if you guess your pen correctly you can proceed further but if you fail so if you don’t guess your pen correctly in those three attempts your account gets blocked so that is the application that we are going to make today and for that we are going to use do while loop so there are three things that we need actually three variables so I’m going to declare those three here the first one is going to be our let’s say the the pin of our user so let’s call it users pin like this which is going to be really the correct pin the one that you want to guess so I’m going to put here let’s say 1 2 3 4 that is going to be our user pin after that that we need another variable which is going to be really the pin that our user enters so let’s call that variable just pin okay like this and then the third variable that we need is going to be really counter of our errors so the counter of the attempts and errors that our user has made so I’m going to call that variable error counter like this and let’s set that value initially to zero okay and the next thing that we need to do is we really need to give our user three attempts in order to guess this pin here and if he guesses it correctly in those three attempts we are going to write out that the application can be loaded further so we are going to write out application is loading or something like that but in the case that our user does not guess this pin here so this pin here correctly we we are going to really write out that the application or a credit card whatever has been blocked and how we are going to do that so we are going to use do while loop so I’m going to write out do like this and then here I’m going to put while because that is the syntax of our do while loop okay and here you need semic colum because if you don’t put it you are going to get a syntax error now here we put the block of code that we want to execute in each iteration and here we put the condition that we want to check each time that this code here is executed we come here and then we check that condition and if this condition here that we put is correct we are going to go again here and do one more iteration and then again check and then do more one more iteration and we are going to repeat that until this condition that we put here is no long longer true when it when this condition here when it becomes false we are going to leave our do while loop okay so what do we want to enter so what do we want actually to do in this block of code here we really want to write out a message to our user so that he knows that he should enter a pin a pin and then we want to really accept that pin that our user enters in this variable variable here so let’s do that first I’m going to write out C out like this and then then let’s just write out pin like this and let’s accept the value that our user has entered in our pin variable like this okay and then after we have done this what we need to do is we really need to check whether this pin here so this pin that our user has entered corresponds to this pin here so whether this value is equal to this value here and if they are not that means that our user has failed so in that case we are going to increase our errors counter so I’m going to put here if pin is not equal to users pin like this what we want to do is we want really increase our errors counter variable like this okay and this code here so we want to give the the attempt to user so that he can enter a pin and then we want to check whether that pin is correct three times or until this pin here and this pin here are the same so until they are equal or until until our user has used all of these three attempts that we have given to him okay so here I’m going to really put that condition so here inside these parentheses I’m going to write out please do this block of code here here while our e let’s say while our errors counter is less than three like this and then also at the same time while our pin is not equal to users pin like this which means that this block of code here is going to be executed while our user has not used all of these three attempts that we have given to him and while this pin here so this pin here and this pin here are not equal in a situation where this here results as false we are going to come here and after that when we come here we really want to check whether we have left this do while loop here because our errors counter has reached three or because these two were the same in a situation where our errors counter has reached three that means that our user has used three attempts and that he has not guessed his pin correctly so I’m going to put here if our errors counter is less than three like this and this condition here really means that this do while loop has been left in a situation where our user has not used all of these three attempt that we have given to him that means that this while condition has really failed on this part here so that means that our user has entered 1 2 3 4 as pin and in that situation 1 2 3 4 is not equal to 1 2 3 4 that results as false so this whole expression here this whole condition results in false as false and then we leave our do while loop and in that situation if our user has not used all of these three attempts that really means that he has guessed his pin correctly so here in this if situation we can write out to our user see out you have guessed or you can proceed with loading of of this application so I’m going to write out just loading like this but in a situation where our while loop has been left because this errors counter is greater than three actually it is going to be equal to three that means that our user has failed in guessing his pin three times and that means probably that he’s not trying to unlock his phone or his credit card so in that situation we are really going to block that account so we can say else see out blocked like this okay and I’m going to put one more here like this so this is going going to be a program that we really use to allow our user to enter three attempts of his pin and in a situation where he guesses that pin correctly in those three attempts we are going to write out to our user that he has successfully unlocked his phone for example and we can proceed with loading of applications but in a situation where he really uses all of those three attempts and fails to guess his pin we can write out that the application is blocked oh blocked now now it’s now it’s okay so let me run this program and let’s try to use three attempts so I’m going to say that my first attempt is going to be for example 1 one one one like this so it says you have not guessed correctly please enter pin again this is what it means after that I’m going to try for example 2222 it tries to prompt me to enter my pin again because I have failed previously and let’s say that my third third attempt is going to be 3333 like this and then it has said it has written out a message that my account has been blocked because I have missed three times okay so that is one scenario and and the other scenario is going to be really where we guess the one where we guess our pin correctly so let’s say that our first attempt is going to be um 111 one again and after that we really remember what is the correct pin and we enter 1 2 3 4 like this and then if I press enter it says really you are loading you can you can load your application now or the further loading of your application is going to begin now because we have T the correct pin so let me very quickly explain one more time how has this happened so this code here when we come to this do while loop the first thing that is executed is this line here so it says please enter your PIN and then after your user enters the pin your program really checks whether that pin is equal to user’s pin so the previous pin the one that he needs to guess and if those two are not equal that means that your user has not guessed correctly and then you want to increase your errors counter so the number of attempts that your user has used and failed actually in using so we increase in our first iteration that errors counter to one so from 0o to one and then we come here and then our Loop really checks whether that errors counter is less than three so one is less than three that is true and also it checks whether that pin here is is not equal to the pin that we need to guess and in this situation it is not equal so it also this this whole expression results as true and it now can go here again so it prompts you again to enter the pin so you can enter the pin now and in this situation we enter 2222 for example and then it guesses it it checks again so it checks whether pin that you have entered so 2222 is not equal to 1 2 3 4 for those two are not equal so it increases the errors counter once more and it becomes two now then you go here and then it says okay please check whether my user has used all of these three attempts it it it says that your errors counter is still less than three so you have used only two attempts so far and then this condition here results as true because you have entered 2222 and that is not equal to your PIN so 1 2 3 4 and then it goes back here tries to prompt you one more time to enter your PIN and then if you fail this time it increases your errors counter to three like this and then it goes here and it checks whether your errors counter is less than three your error errors counter in this situation is uh has become three so this condition here results as false and your while the while loop actually is now broken so you are not going to continue the execution of your do while loop but you come here and then it checks whether errors counter is less than three because this here fails because this here results as false you are presented with this message here so blocked your phone your account has been blocked okay and in a situation where your user guesses correctly that errors counter is going to be less than three and you are going to be presented with a message of loading and there is one important thing that I want to mention and is really the difference between while loop and do while loop in do while loop you first do so you first execute this block of code here and then you check the condition and then if that condition results as true you go back and you again do this block of code here and then you check and then you do but in a situation of while loop you first check the condition and then if that condition results as true then you can really proceed with executing your block of code that comes after your while but in this situation here so in the situation of do while loop you first do this block of code here so uh irrelevant whether this condition here is true or not you are going to do this block of code here at least once I hope that you like this video if you did make sure to subscribe to my channel and click that Bell icon as well and also if you want me to post more videos like this make sure to like this video because then I will know that you liked it and well that’s all I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners in this video I want to talk about four Loops but before we start make sure to subscribe to my channel and hit that Bell icon as well okay so what I’m going to explain today I want to explain to you how for Loop works and I’m going to explain that on a simple example which is calculating the factorial of a number so let me copy the text of our task here like this and I’m going to comment it out so what is the factorial of a number factorial of a number is usually defined as a product of all numbers between one and that number that you want to calculate the factorial of that means for example if your user enter here is number six let’s say and you want to calculate the factorial of number six you are going to do that in a following way so 1 * by 2 multipli by 3 4 5 and oh sorry six okay and then this here is going to result in 720 so this 720 is really going to be the result of your factorial of six and this example here is what we want to really translate in our program so we want our user to enter a number and then we want to calculate the factorial of that number and write that result out to our user and we are going to use for Loop for that so let me quickly declare a variable which is going to be called number okay like this and let’s prompt our user that he should enter a number and then let’s really store that number that our user enters in our number variable okay so after we have acquired a number that we want to calculate our factorial of let’s explain how we are going to really do that so I’m going to do that using for Loop and what is the difference between four Loop and while loop and do while loop because we have previously talked about while loop and do while loop as well you can find those videos on my channel if you’re interested and I’m going to link those videos in the description of this video if you want to watch them so the difference is that usually when you use for Loop beforehand you should know how many iterations that Loop is going to make so for how long your Loop is going to run so let me show you the syntax of our for Loop you say four and then you use these parentheses here in which we are going to put certain expressions in a moment and after that you put these curly brackets in which we are going to write out the code that is going to be executed in each iteration of our four Loop so inside this parentheses here we need to put three things what are those three things the first one is going to be to set the initial value of our counter variable that is the first one after that the second is going to be to really put the condition that your for Loop is going to check each time that it wants to run this block of code here so before each iteration it is going to check that condition so that is going to be the second thing and after that the third thing is going to be really a way for your counter value to come to that condition condition which means that you have to either increase or decrease your counter variable in order to reach that condition at certain point and then when that condition results as false your for Loop is not going to be executed anymore okay so those are three things that we need to put in these parentheses here and let’s now look at this example here because from here we are going to really uh deduct which conditions which Expressions we are going to put here so if you notice this algorithm here because this is really algorithm nothing else um you see that this is really a counter this behaves as counter so it goes 1 2 3 four and it really starts at one and then it goes up to that value that your user has entered so we can use that for our expression here the first expression is initial value of our counter we need to declare that variable and we are going to call that variable I you can call it count or whatever you want really uh your compiler is not going to give you an error because of that but a common practice is to use a single letter variable name for this counter that you use in your for Loop so I’m going to use name I for that and the initial value of my I counter is going to be one because as you can see here it really starts on one and then after that we need to put the condition that your for Loop is going to check each time that it wants to run a new iteration and that condition as you can see here is really this this expression here this algorithm runs until it comes to this six until it comes to the value that your user has entered so we are going to put that here we are going to say please run this block of code here until my I is less or equal to number like this so whichever number our user enters and then the third thing is going to re be really to give our Loop a way to reach this condition here at some point which means to make this condition here false and then leave our four Loop and again we look here and you can see that your counter is really increasing in each iteration by one okay so here we are going to put I ++ which is in each iteration going to increase the value of our I counter by one okay so after we have put these expressions in these parentheses of our for Loop we need to really put a block of code that is going to be executed in each iteration of our four Loop and I’m going to declare before this for Loop here I’m going to declare another variable which I’m going to call tutorial and I’m going to give it a type of int okay like this and I’m going to assign the initial value to my factorial factorial variable and that is going to be one and then how I’m going to use this factorial variable in each iteration of my four loop I want to say that my factorial is going to be equal to whatever my factorial was previously holding multiply that by my current I value which means by the current value of my counter and how this is going to behave so let’s say that our user enters for example number three and then your execution comes here and it says please declare a a variable which is going to be called factorial and it is going to have a value of one so we have assigned that value here and then it comes to your four Loop the first thing that your four Loop is going to do is it is going to declare this I variable here and assign it a value of one so we we are going to write that here so we are going to say I is equal to 1 and then it really goes here and checks this condition here before it determines whether it’s going to run this block of code here or not so if this here results is true it is going to run this block of codee here and if not it is going to leave your for Loop so is your I less or equal to the number that your user has entered so your I is going to be one and then that is less or equal to three so your for Loop is going to execute this block of code here which means that it is going to come here and say factorial multiplied by one which is going to be 1 multiplied by 1 and that is going to result as one and it is going to assign the result of this here to your factorial variable which is going to stay one after that your for Loop comes here and at this close bracket here it really goes here and it increases the value of your I and this increment operator is going to increase it by one so your I now becomes two and then after that it goes back here and checks this condition here so it says please check check whether my two is less or equal to the number that your user has entered so two is less or equal to three so it is going to run this block of code here again and then it is going to do following so it is going to say please multiply my factorial value so the previous value that my factorial was holding which is one by the current value of my I which is 1 * 2 that is going to result as 2 and that is going to be assigned to your factorial variable so we assign two here okay and then it goes back to these Clos brackets and then it goes here increases the value of your I again so your I now is three and then it really checks this condition here so whether three is less or equal to three the result of that is going to be yes so it is going to do this expression here again so so the current value of your factorial which is going to be two multiplied by the current value of your counter variable which is going to be three so 2 ultied by 3 that is going to be six and that is going to be assigned to your factorial variable after that it goes here and then it really increases again the value of your I variable so of the counter that you are using in your for Loop and that value becomes four now after that it is going to try to check this expression here so this condition here and four is not less or equal to three so your for Loop is not going to execute anymore and then your really your your execution comes here and at this line of code here it has the value of six in your factorial variable which means that we are left only with the need to write out this to our user so we need need to say um let’s say it like this number okay and then let’s say factorial of that number is going to be equal two and then let’s write out the result so the result we are holding in our factorial variable like this so This should really write out the result of this here so the the factorial of this number that our user has has entered and if I run my program you can see that it asks us to enter a number I’m going to say let’s say for example three and then if I press enter it is going to give us the result of six which is the correct result the one that we previously got let’s run it one more time just check this number here so I’m going to say please give me a factorial of six and it calculates that the factorial of 6 is 720 as we did before so this is going to be really one way of writing this for Loop for this current example so for calculating factorial I want to show you another way how you can do exactly the same thing so I’m going to really comment this part of code here so I’m going to say um comment this block of code here and then there is another way to write these parenthesis here so as you can see this example here is what we have used in order to write these Expressions inside these parentheses but there is really one more way to calculate your factorial and that is going to be to start at this maximum value which means 6 factorial is equal to 6 multiplied by 5 by 4 3 2 1 and and that is going to be equal to 720 so now you start your counter at this maximum value so this number that your user has entered and then you really decrease the value of your counter and you go until your counter value comes to one and that is the condition when you stop so your one value of a counter is going to be the last iteration through your for Loop so now we are going to write out um so actually we are going to really write our for Loop using this algorithm here so we are going to say four and then also put this these parentheses which we are going to use for the block of code that we want to run through in each iteration and in these parentheses here I’m going to set the initial value of my counter that is going to be I is equal to and as you can see here our I starts at six so I’m going to put here six which means a number that our user has entered in this current situation it is six but since we want to calculate the factorial of whichever number our user decides to enter I’m going to use that variable here okay and then the condition that your for Loop is going to check each time that it wants to make a new iteration and that is going to be please run this block of code here until my I is greater than or equal to 2 1 okay and then the last thing that we need to put here is going to be really a way for our for Loop to stop at a certain point and if you look at this example here you see that your counter is decreasing by one in each iteration so we are going to put that here we are going to say I minus minus which means use this decrement operator on our I variable and then we are going to really use the same expression here here okay and this is going to give us the exact exactly the same result as this code here did so if I run my program you can see that it asks number again so if I enter number six for example it is going to give us 720 as this code here did as well so let’s repeat one more time there are three things that you need to put in these parentheses here in order to set up your four Loop and the first one of those three is going to be to set the initial value of your I variable which means the initial value of your counter after that you need to put the condition which is going to be checked each time that your for loop wants to run an iteration and if this condition here results as true your for Loop is going to run this block of code here but if this condition here results as false your for Loop is not going to run this block of code here but you are really going to leave your for Loop and come to the next line after it and then the third thing that you need to put in these parentheses here is going to be really to either increase or decrease value of your counter variable so that it can reach this condition here at certain point and make it false so we have decreased it in this example here and we have increased it in this example here so those are three things that you need to put inside these parentheses here and you really need to separate those three using this semic colum sign okay and there is one more thing that I need to mention and that is going to be really you can upgrade this program here so that it checks whether this number here is greater than zero because you cannot calculate factorial of negative number so our program is not going to be behave as expected if you enter negative number and also the factorial of zero is going to be equal to 1 so you can put that code here and determine whether your user has entered positive or negative number and in case that your user has entered negative number make sure to write out appropriate message to your user so I think that is all if you like this video and if you learned something new make sure to subscribe to my channel and hit that Bell icon as well share it on your social media because it really helps me to reach a lot more people so that all of us can learn and upgrade our knowledge in programming and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners in this video I want to talk about nesting Loops in my previous videos we have talked about while loop do while loop and for Loop and now I want to show you the example on how you can really combine and Nest those Loops so if you haven’t watched my previous videos or you are not familiar with loops at all I recommend you to watch those I’m going to link those in the description of this video and also before we start make sure to subscribe to my channel and click that Bell icon as well so what are we going to do in this video I want to really show you how you can nest for Loop and do while loop and the example that I’m going to use to explain that is going to be following so I want my user which is going to be a student let’s say to enter three grades or more if you want we are going to use three for desk exam and I want to those grades I want those grades to be between 1 and five and then I want to write out the average grade so the average grade between those three that he has entered so let’s start building our program the first thing that I need to explain is since we are going to enter three grades that means that we are going to repeat that entering process three times and for that I’m going to use for Loop so I’m going to write four and then in these parentheses here I’m going to put my initial set setup of four Loop which is going to be the first thing is going to be initial value of our counter so I’m going to declare a counter which is going to be called I and initially I’m going to give it a value of zero that is the first thing the second thing that I need to put in these parentheses here is going to be the condition that our Loop checks each time that it wants to run a block of code which is going to be this block of code that we put inside these parentheses so that condition we put here and that condition is going to be while our I is less than three so we want to make three iterations in the first iteration our I has the value of zero in the second iteration it’s going to have the value of one and then in our third iteration it’s going to have the value of two so we have made three iterations after that it gets the value of three but this condition here is not going to be true anymore so then our for Loop is not going to be executed anymore so that is the second thing and then the third thing which we need is going to be really increasing that value of our counter in each iteration so after age iteration and I’m going to put here this increment on our I variable okay so in this block of code here we are going to repeat three times something so what is that something that is going to be really the process of our user entering the value for his grade and since we said that that grade needs to be between 1 and five I am going to use do while loop for that how well I’m going to write out do okay and then here I’m going to put while which is going to be the condition so here we prompt our you user to enter the grade and then we we say here you are going to have to enter that grade while that grade is less than 1 or greater than five so since we don’t have any variable that we are going to use to enter that grade I will have to declare it here so I’m going to say here int grade like this okay and then let’s prompt our user in this block here to enter his grade so I’m going to write out C out and let’s say enter grade and I am going to use the current value of my counter so that I can really say um which grade my user is entering so enter first grade second grade and third grade so I’m going to say I + 1 like this y + one well because our counter starts at zero and it wouldn’t make much sense to say to our user please enter grade zero so that is why I’m increasing this value of my I when I want to write out a message to my user okay like this so here I’m going to write out please enter a grade and after that I need to accept that grade that my user enters in my grade variable so I’m going to say C in grade like this and we are going to repeat this process here for a single grade while our user enters a grade that is less than one or grade is greater than 5 okay so our program is really going to prompt our user please enter grade one and then he enter six for example and then our program checks this condition here and says okay you have missed this part here so go back and then please enter grade one so first grade again okay so if I run my program now let’s just prove that very quickly so our program our program says now enter first grade so enter grade one and then let’s say that I enter nine for example it prompts me again to enter first grade because I have not obeyed this rule here this part here so I’m going to say this time for example five and then it says okay you have successfully entered first grade let’s now enter second grade so for that second grade let’s enter for example zero and then it prompts me again you have not entered correct value for grade two please enter it again and let’s say that this time I’m going to enter one and then it asks for third grade and let’s say that this time third grade is going to be three so our program now has successfully accepted this third grade and now our program is going to really uh finish its execution because we haven’t written anything else after this part here so there is something else that we need to write and since we want to calculate the average grade of our user what we are going to need is going to be really to sum all of these grades that our user enters so I am going to declare a variable which is going to really hold that sum of our users grades so I’m am going to put it here I’m going to call it sum and initially I’m going to assign it a value of zero and later I’m going to explain why so I have declared a variable that is going to be of type int and I have called it sum and assign it a value of zero so this sum here is going to each time that our user enters a grade and we really um we really check that that grade is valid grade after that we want our sum to hold whatever it was previously holding plus the value of our grade like this okay so why I have assigned a value of zero to my sum because zero is a neutral value when it comes to adding numbers you add zero to whichever number and then you get that same number and in a situation where we didn’t assign any value to our sum we would get really um an error so if I try to run my program now you see that it prompts me with an error and it says uninitialized local variable sum used because it does not have any value assigned to this variable here it is going to say really that it does not know how to add the value so the value of our grade to our sum so that is why we need to assign some value to our sum variable okay so and that value is going to be zero because zero is neutral when it comes to adding numbers okay so now I can really run my program and as you can see um that error has disappeared so I’m going to stop my program and let’s very quickly write out this sum that we have calculated here so I’m going to say see out and I’m going to write out sum like this sum is equal to and then let’s write out the value of our sum and let’s add end line like this so I am going to run my program once more it prompts me to enter first grade I’m going to say that my first first grade is going to be for example five and then my second grade is going to be for example six uhuh that’s an error so please enter your second grade again so I’m going to enter this time two let’s say and then for my third third grade let’s enter one for example and it says that the sum of my grade grades is equal to 8 which is correct so 1 + 2 + 5 that is going to equal to 8 so what we are left to do now we should only really divide our sum by the count of our grades which is three because we have hardcoded this value here to three so our user can enter only three grades you can really use a variable for this or you can part code it to any other value that you want and need so here after this line of code here what I need to do is following so I’m going to say C out out and then let’s say average is equal to let’s say average grade is equal to and let’s write out the result so that is going to be sum divided by three okay is it though so I’m going to run my program and let’s enter for example value two and then again value two and then let’s enter for example value four and then it says that average grade is equal to two which is not correct but why has this happened so as you can see here we have used two integer variables so our sum is integer value and then three is integer value as well so when you divide two integer values the result is also going to be integer value so in order to get a floating point a decimal point Point number you really need to convert one of these two into a float type so you can say either here you can say float so please convert my sum variable into float and then divide or you can put here this 0 Z or you can leave both of these whichever you prefer so I’m going to leave both of these like this so if I run my program once more it prompts me again to enter first first grade so I’m going to say that my first grade is equal to five and then my second grade is also five and then let’s say that the third grade of our user is going to be nine so our program prompts us again to enter the third grade because it needs to be in this interval here so I’m going to say now that uh the value of my third grade is going to be three which is correct value and after that as you can see it writes out that the sum of these three grades is 13 and then average grade is 4.33 333 so that was an example on how you can really combine and Nest Loops in order to solve complex problems and thanks for watching make sure to subscribe to my channel click the Bell icon also share it with anyone who would like to learn programming and I’m going to see you in my next video bye hi everyone welcome to to my channel welcome to another video of C++ for beginners in this video I want to talk about nesting four Loops so I want show you how you can solve more complex problems using nested for Loop but before we start please subscribe to my channel and click the Bell icon as well and also like this video so this example that we are going to do today is going to be really multiplication stable so I’m going to show you the example that I want to program so that would be this here and if you look at this multiplication table that we want to make you can really start to notice a pattern and what pattern I mean you see here this this first end in this first part of my multiplication table so in this first table as you can see this first end is one and it stays one in this table here after that you can look at this second table you see that this end is now two and then in my third table it has increased to three 4 five and it really increases so it increments by one and it goes up to 10 okay so that is the first operand so this first part of my multiplication after that we can look at this second operand and you can really notice that it increases in each iteration by one so it goes one two three four five up to 10 and then it does the same thing in this second table so it goes from 1 to 10 again and then the same in my third table fourth fifth and it really behaves the same in this 10th table as well so that is the pattern that I want you to notice so this first oper end can be really represented using one for Loop so let’s say that that is going to be outer for Loop and it is going to increase each time that my second upper end Counts from 1 to 10 and that second opont we can really represent using another for Loop which is going to be really inner for Loop so the program the algorithm for for solving this problem is going to go like this you have outer for Loop which is going to count from 1 2 3 4 up to 10 so it is going to count from 1 to 10 and then you are going to have inner for Loop which is also going to count from 1 to 10 but for each value of my counter of my outer loop okay so that means that my outer loop counter changes only after my inner loop has counted from 1 to 10 so let’s copy the text of our task here I’m going to say multiplication table and let’s comment that text out so as we said in order to solve this problem we are going to use nested for Loops so I’m going to add empty space here and then the first thing that I need is going to be my outer loop so I’m going to really set up my outer loop I’m going to say four and then we need to declare a counter which I’m going to call I so I’m going to say int I is equal to and then since our outer let me show you this image one more time since it goes from one to 10 I’m really going to set its initial value to one so I’m going to say I is equal to 1 and then I’m going to write the condition so how long this for Loop is going to run and that is going to be while this I is less than or equal to 10 like this and then each iteration it is going to increase by one so that is going to be i++ okay and then I’m going to put this block of code here that is going to be executed in each iteration of this for Loop so what we need to do in each iteration of our for Loop let’s look at our image one more time and as you can see it really says that in each iteration of this outer for Loop you need to count from 1 to 10 so in this iteration here some other counter really counts from 1 to 10 and then new iteration so our I is now two and then counter counts again from 1 to 10 and then three but also we have this inner counter that counts from 1 to 10 okay so let’s really write that code so that inner counter that counts from 1 to 10 for each value of our I we are going to represent with another for Loop and that is going to be really that nested or that inner for Loop so I’m going to write four and that inner counter I’m going to call J so I’m going to write int J and since our J also Counts from 1 to 10 I’m going to set its initial value to one and I’m also going to say please iterate through this for a loop while our J is less than or equal to 10 as well and then in each iteration please increase the value of J by one so do this increment operation and then in each iteration of our J Loop we are going to execute this block of code here so what we are going to do in this block of code here well let me look at that image once more and as you can see in each iteration of our J so this is one iteration of our J this is second third fourth fifth and then 10th iteration of our J after that we change our I counter value so we increment that and then our J starts again starts one more to count from 1 to 10 so in each of these iterations as you can see really we have to multiply our I value counter with our J value counter and write the result of that so I am going to write that here I am going to write C out like this and then let’s write out the value of our I counter like this and then let’s write out this this multiplication operation and then let’s write out J like this this is just for maing our text so I’m going to put this equal sign and then I really want to multiply those two counters like this and let’s also add one end line here so that our text is formatted nicely because we are going to have multiple multiple lines so with this we we have really covered this multiplication part and there is only one more thing left to do and that is to add one more end line because I really want to make separation between these so I want to make separation between these tables this is going to be one logical part after that this is going to be another then third fourth and I want to put uh empty space space between those two so that I can group them group them so I’m going to add here C out and line like this and that should be it pretty much so if I run my program now as you can see it has generated this multiplication table from 1 to 10 okay and it has done that in a matter of less than a second so as you can see here we have this outer counter oh I cannot Mark that only so we have this outer counter which is going to be our I that counts from one to 10 but for each value of our outer counter for so for each value of our I we have to really count from 1 to 10 with our inner counter which is going to be our J as you can see here okay so these are the results of our multiplication operation and I hope that you have seen now how you can really use programming to your advantage how you can use it to solve not really complex but tedious tasks you would really need 20 or 30 minutes in order to write this yourself and you can now generate it with just couple of lines of code and less than a second of your computer’s power to to Really execute this code here that you have written and the most amazing part is that you don’t really have to put 10 here here so we have put 10 here because I want to make this example manageable for this video but you don’t have to hardcode these values here you can really use whatever values you want you can say for example let’s say 2,000 okay so I want to generate multiplications stable up to 2,000 how long would that take in real life so without programming okay now you see a few moments later okay so it is still running and it is going to generate around 4 million records which is a lot and it is going to do that in a matter of minutes so how long would it take for you to even just count from 1 to 4 million it would take you probably days if not months and also there were a lot of errors that you would probably make in that process so I hope that now you can really recognize the power of programming now if you needed to do this task you would really program your computer to do it and now you can just go take a break and your computer is going to finish that task for you so I hope that you can recognize now how you can use programming in order to solve different types of tasks that you can come across in your school in your job or life in general and if you like this video make sure to subscribe to my channel also click the Bell icon and share it with anyone who would like to learn programming and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners in this video I want to show you how you can draw shapes using C++ so before we start make sure to subscribe to my channel and also hit that Bell icon as well so that you are notified when I publish my next video and let me show you now what we are going to do in this video so here where is it here I have image of the task that we are going to do today and that is going to be really to draw this rectangle shape with the desired height and width and the symbol that our user enters okay so that is the task that we are going to do in this video and let’s very quickly analyze this problem and see how we are going to solve it so the first thing that we need is for our user to enter height width and then symbol and after that we will really need to figure out a way so figure out the algorithm on how we are going to draw this rectangle shape here so if you look at this rectangle shape you can really notice that it has the height that is going to be three of these symbols that our user has entered and then it has the width that is going to take four of these symbols that our user has entered for width okay so we are going to use nested for Loop in order to solve this problem here and if you’re not familiar with nested four loop I am going to link that video here because I have already made one video with nested four Loops so make sure to watch that video and in this video I am going to really use that nested for Loop in order to control height and width of our rectangle shape so we are going to have outer for Loop which is going to really control this height and then for each iteration of our outer for Loop we are really going to control this width with another loop and we are going to count from one up to four because four is the width that our user has entered and draw that shape in each iteration of our inner for Loop okay so that is what we are going to do so let’s now translate that into code I’m going to say here first thing that we need is going to be from our user to enter width and height so I’m going to declare two variables the first one is going to be height like this and then with Okay so let’s write out a message to our user so that he knows that he should enter height okay now let’s accept that height value in our height variable and after that let’s really copy this and do the same thing for our width like this and accept that value in our width variable so that is the first step the second step is going to be to declare another variable which is going to hold the symbol that our user wants to use for drawing that shape so I’m going to declare a variable of type Char and I’m going to call it symbol like this and let’s write out a message to the user so that he knows that he should enter that symbol like this and let’s say C in symbol so after we have acquired all the necessary inputs from our user which are height width and symbol that we are going to use to draw our shape now we are left to write the algorithm that is going to draw that rectangle shape so as I already said for that we are going to use nested for Loop so I am going to set up my outer loop the first Loop and I’m going to say four and then I’m going to declare the counter which is going to count the iterations of my outer loop since that outer loop is going to control the height of our rectangle I’m going to say int H so I’m going to give the H name to our outer counter and I’m going to set its initial value to zero like this and then we are going to put the condition how long our outer loop is going to run so while our H is less than height that our user has entered like this and then after each iteration of my outer loop we have to increment the value of our H counter so that would be the setup of our Outer Loop and let’s Now set up our inner loop so I’m going to write four and for this inner loop I’m going to declare another counter which I’m going to call W like this because this inner loop is going to control the width of rectangle shape so I’m going to set its initial value to zero as well and then I’m going to say that it is going to run while this W is less than with that our user has entered like this and also after each iteration we really need to increment the value of this W okay so what we are going to do in this Loop so in this inner loop if you look at this image here where is it okay here you can really notice that in each iteration of our inner loop it just draws this symbol that our user has entered so I am going to very quickly just see out so write that symbol out I’m going to say see out and then write out that symbol like this and there is really one more thing that we need to put and that is going to be after this four Loop so this outer for Loop finishes single iteration we really need to put something else at the end so here at the end of our outer for Loop and let’s look at our image so as you can see here after each iteration of our outer four Loop because it is controlling height and it is really iterating through this inner four Loop in each iteration of our outer four loop at the end of that iteration of outer for Loop we really need to put empty space so here I need to put empty space and that corresponds to this line here so here I’m going to write out C out end line like this and now if I run my program we should really expect to see the shape that rectangle shape of Dimensions that our user defines so let’s say for height four and then for width let’s say for example six and the symbol that we want to use to draw that shape let’s say that that is going to be this plus symbol okay so as you can see our rectangle has the height of four and then it has the width of 2 4 6 so it has the width of six of these symbols that our user has entered okay so there is one more thing that I notice here and that is going to be really to format this shape uh to be a bit more pretty so for that we are going to use the library which is called IO manip so I am going to include that Library here I have to say include IO manip like this and this Library here really contains a function that is that is called set W which means set width which is really going to set the width of the field that comes after it so I’m going to put here that function I’m going to say I’m going to put these redirection signs here and then in this space here I’m going to write out set V so set W actually and then here I want to put what is the width of the field that comes after this function so what is going to be the width of our symbol field and let’s use three for DOT for example so if I run my program again but let me stop it first and if I run it again oh no I haven’t stopped my program so I’m going to close it okay and now if I run my program you can see that it asks height and width again so previously we have entered four and six and then for the symbol let’s enter plus again and now as you can see our shape looks much nicer because of this set width function because it has really assigned to each symbol to each character that our user has entered the width of three Fields okay so let’s run our program one more time and let’s say that that height is going to be five and width is going to be seven and let’s enter another shape so let’s say for example minus and as you can see it has written out the shape so the rectangle shape of these Dimensions that our user has entered and it has used this symbol that our user also has entered so I hope that you enjoyed this video if you did make sure to sub subscribe to my channel and click the Bell icon as well also if you want me to make more videos like this so if you want me to use programming to draw more shapes make sure to write that in the comments down below so that I can know that you really enjoyed this video and like it as well and thanks for watching I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ for beginners and this video is going to be sort of a continuation to my previous video in which I showed you how you can draw rectangle shape using C++ and in this video I want to show you how you can draw triangle and reversed triangle shapes using C++ as well so if you’re not familiar with how you can draw rectangle I would suggest you to watch that video first I’m going to link it here because in my opinion it is a bit more difficult to draw triangle than it is to draw a rectangle shapes so what one more thing I have also created Instagram and Twitter accounts so if you are interested in how developer life looks like and also if you’re interested in behind the scenes of these videos that I post on YouTube make sure to follow me on my other social media so at true code Beauty Instagram and Twitter accounts I’m going to put the link to those in the description of this video so without further Ado let’s start drawing our triangle shape so here I have the shape that we are going to draw in this video and that is going to be this equilateral triangle shape that I have here and there is a couple of things that I want to explain before we start to write code the first thing is going to be that this height our user really has to enter this height so we are going to let our user decide how many of these symbols are going to be on this side and this side and this side as well and the second thing is going to be that our user defines this symbol so which whichever symbol our user wants to use in order to draw this shape we are going to use that symbol that he enters those are two things that we need from our user and then it is up to us to really think and figure out the algorithm that we are going to use in order to draw this triangle shape here so let me explain to you how we are going to do that so we are going to have two four loops and those are going to be nested for Loops so if you’re not familiar with for Loop nesting I’m going to link my video on that here so make sure to watch it so our outer four Loop is the one that is going to control this height of a triangle so in this particular case it is going to have five iterations 1 2 3 4 five and then our inner for Loop is the one that is going to control the width of this triangle and if you really look at this triangle you can notice that our outer for Loop really goes from one up to the number that our user has entered so it has five etration in this particular case because our user has entered five for the length of this triangle so for the length of its sides so that is the behavior of our outer for Loop it goes from one up to five in this particular case and then the behavior of our inner for Loop is going to be the following so in the first iteration of our outer for Loop loop our inner for Loop has only one iteration after that in the second iteration of outer for Loop inner for Loop has two iterations then in the third iteration of outer for Loop inner for Loop has three in a fourth iteration of outer for Loop inner for Loop has four and so on so that means that our inner for Loop goes from one up to the current value of the counter of outer for Loop so let’s translate that into our C++ code let me return this picture here okay so before we start to write that algorithm I am going to create a variable which is going to hold the length that our user enters so I’m going to call that variable length Okay and then let’s write out a message to the user so that he knows that he should enter length like this and let’s store the value in our length variable so that is the first thing that we need from our user and then the second thing is going to be a symbol so that is going to be of type Char like this so I’m going to write out oh see out symbol like this so that our user knows that you should enter symbol as well and let’s store that symbol and our symbol variable so after we have acquired these two things from our user let’s translate the algorithm that I just explained into C++ code so as I said we are going to have two for Loops outer for Loop which is going to have counter that I’m going to call let’s say I and that outer for Loop its counter goes from one up to the value that our user us has entered and it increases by one in each iteration okay so I’m going to say I is equal to 1 and then I is I is less than or equal to length so length that our user has entered and then please increment that I in each iteration so that is going to be outer for Loop and then inner for Loop is going to go from one up to the current value of outer for Loops counter so in the first iteration is going to execute only once so it is going to have only one iteration in the second iteration of outer for Loop it is going to execute twice in the third it is going to execute three times four times five times so how we are going to write that we are going to say four int and then our inner for Loops counter I’m going to call J and J goes from one up to the value of our I so up to the value of outer four Loops counter and it increases by one as well in each iteration and in each iteration it has to write out this symbol that our user has entered so let’s say see out symbol like this okay and then there is one more thing that I want to explain and that is going to be after each iteration of outer for Loop really the next iteration starts in a new line so we are going to write that here here I’m sorry so at the end of our for loop our outer for Loop we are going to say see out end line okay and now if I run this program it asks for length I’m going to say that length is let’s say five and then symbol is going to be star symbol and as you can see it has really written out a triangle that has the length five of these symbols that our user has entered and there is one more thing that I want to do in order to format this shape here to be a bit prettier and that is going to be really to add a function here which is going to assign to each of these symbols that are user enter so that our for Loop writes out uh width of two fields and that is going to be done using a function that is called set W meaning set width and this function here is going to give the space of two fields to the symbol that comes after it and in order to use this function here we have to include its library and that is going to be iio manip Library so I’m going to say include like this IO manip oh okay and now this underline has went away so if I run my program once more and I enter let’s say for length six and then for symbol I enter this this symbol here as you can see our triangle shape looks better now so it has the height or actually the length of six of these symbols that our user has entered and it has also used this symbol here in order to draw this triangle shape and there is one more thing that I want to show you so at the beginning of this video I promis that I’m going to teach you how you can draw triangle shape and then reversed triangle shape as well so there is one very beautiful thing about programming and that is going to be code reusability and about real code reusability you are going to learn when we learn about classes and functions and when I make my videos on functions I’m going to link that video here so make sure to watch it because functions really are one of the most important things that you have to understand not only for C++ programming but for programming in general so make sure to watch that video and let’s say that for now we are only going to really adjust this code here so we are going to modify it a little bit in order to draw this shape here so if you look at these two shapes you can really notice similarities and what are those so this inner for Loop behaves about the same in both of these triangles but there is one key difference and that is going to be that in this triangle shape our outer for Loop Counts from 1 to five and then in this reverse triangle our outer for Loop really counts from five to one so if we modify that we are going to have this reversed triangle so let me move these and then I’m going to really copy this here so I’m going to use contrl C and then control V and then let’s really add a couple of end lines between these two okay like this and then let’s modify this triangle shape that we have just explained and drawn and as you could see on this image here our outer for Loop Counts from five to one which means from the size that our user has entered from one so that means that if we change our outer for Loop we should get that reverse triangle shape so it goes from length like this and it really goes while our I is greater than or equal to 1 and then our I decreases in each iteration so it decrements and now if I run my program it asks me for length let’s say that length is going to be five and then symbol is going to be let’s say this plus symbol and when I pressed enter we have gotten both of these triangle shapes so this one that we have drawn first and then this reverse triangle shape as well I hope that now you have really seeing the real beauty of programming you learn some Basics and then you can use those basics in the most creative ways that you can think of so I hope that you like this video if you did make sure to subscribe to my channel click the Bell icon as well so that you are notified when I publish my next video and a lot more great videos are coming thanks for watching I’m going to see you in my next video bye hi everyone welcome to my channel welcome to the first video of C++ functions if you’re a beginner at C++ make sure to check out my playlist on C++ for beginners and there you can find some of the most important Concepts that you will need to understand and know in order to work with C++ and in this video we are going to talk about C++ functions so functions are one of the most important Concepts that you will need to understand and use in order to be a good programmer and that is not only case in C++ but in most programming languages as well the first thing that I want to explain is what is function function is a block of code that is grouped together and usually that block of code solves a specific problem meaning it performs a specific task and that block of code is executed only when you call that function meaning only when you invoke the function the body of that function is going to be executed and each C++ program contains at least one function and that is going to be main function so this here and the execution of your program starts on the first line of your main function and then it ends when it comes to this last line so this Clos bracket of your main function and also it can end when it comes to return statement but we are going to talk about return statements later in this course so main function is the main one so the first one that is executed and whatever code you put in your main function that code is going to be executed as part of your program so if I say here for example see out and I say hello from main function like this and let’s add a new line like this and now if I run my program you can see that our program has written out hello from Main and that is the first function that our program starts executing besides from main function you can also create your own functions and let’s explain how you can do that so there are a couple of things that you need to know in order to create your own function and that is going to be following so the first thing that you need to really write when you create your own function is going to be return type of your function and that is going to be void for now which means that this function is not going to return anything and we are going to talk about return types more in detail later in this course so for now we are going to use void return type meaning that this function that we declare in this line is not going to to return anything and then you write out the name for your function and let’s call it function like this and then in these parentheses you can put arguments or parameters that your functions that your function really uh receives and for now these parth parentheses are going to be empty meaning that this function does not receive any arguments and then in these curly brackets we are going to put the body of our function so this here is going to be the function that we created ourselves and you have to specify specify the return type of your function after that you write out the name of your function then in these parentheses you can put any arguments that your function receives in this particular case these are empty and we are not going to send any parameters to our function and then here is going to be the body of your function and what we are going to put in this body let’s say see out hello from function like this and let’s also add end line at the end of this function so now we have really created our own function which is called function and if I run my program now what do you think that is going to happen write that in the comments down below so I am going to run this program and as you can see nothing has changed really our program has written out hello from Main as it did before so before we created this function that is because as I explained your function is not going to execute until you call that function so until you invoke it it is not going to execute so in order for our function to be executed we need to invoke that function and how that is done so here in this line after hello from Main we are going to put function and then these parentheses so this here is the invocation of our function and now our function is going to be executed so now if I press this play button you can see that now our console has changed so now we have hello from Main and then also hello from function so that is because we have invoked this function here and you can invoke this function wherever you need it so you can I have invoked it after this line here but you can invoke it as well before this line here if you need to and if you want to so now if I run my program you can see that it first says hello from function and then it says hello from Main because this function has been invoked first and then we have written out this line of code here and that you can see in this console window and let me show you one thing so I’m going to return this after this line of code the invocation of our function I’m going to return here and now I’m going to take this code here so this uh definition of our function and I’m going to paste it after our main function and let’s see what is going to happen now so let me delete these these blank lines and now if I run my program you can see that our program has failed to build so we have compile time errors and in this window here in this error list you can see that it says function identifier not found which means that this function here is really uh our compiler does not understand what this function is because C++ code is executed from top to bottom and when it comes to this line here so when it comes to the invocation of your function it is really not familiar with this function it it does not know what this function is so you have to either put this code before your main function as we did before or you have to do um other thing which is really the recommended way to create your functions and that would be to create definition and Declaration of your function so what are definition and Declaration of function Declaration of function is going to be really this here so I’m going to copy this and put it before our main function and this declaration of function is going to tell to your compiler what is going to be the return type of your function what is the name of your function and then if there are any parameters that your function really receives you are going to put those in these parentheses here so that is going to be Declaration of your function and that part decoration of your function goes before your main function and after that you are going to write the definition of your function and the definition of your function is going to go after your main function and in that way you are going to make your code more readable meaning that when someone else comes to your code and if your code had 200 or three or 400 lines of code then it would be really hard to to read that code and for someone it would be really hard to manage and uh go through your code so he is going to find it much easier if he has all the Declarations of your functions before your main function and then if he needs to read a specific definition of a function he can rightclick the name of that function and click go to definition and then he’s going to be taken to the definition of your function so the specific one that he needs to read so that you don’t bombard um that user with all the functions all the definitions of a fun functions that you have in your program and in this particular situation it is only one function but uh your program is going to consist usually of more than one function and that is really unreadable when you find a lot of definitions of a functions before your main so you can right click on your function and say go to definition or you can press F12 so you can click on your function and then press F12 and you are going to be taken to the definition of your function Okay so so this here is the recommended way to create functions and now if I press play button you can see that our program has executed actually has written out the same as it did before so we have uh really removed that error that we had when we um moved this definition of a function after our main because we have added this declaration of a function before our main function and there is really the most most important thing that I want to mention in this video and that is going to be what is the reason why functions exist okay so we have seen that they make your code a bit more readable so they group the parts of your code together so that you can manage your code easier and the most important characteristic of functions is that they make your code reusable meaning that you can write a specific code once so you can solve a specific problem once put the solution the algorithm the code for that problem in a function and then you can really invoke that function however many times you need it so here I can really copy this and then paste it let’s say three times if I needed it and then if I run my program you can see that this function has been executed three times here and that is really the most important thing about functions which means functions make your code reusable you don’t have to write the same code over and over again and you should really try to group your code in functions so that each function does a specific task nothing more than that and in that way you are going to make your code easier for yourself and for other people as well that are going to read your code so I hope that you understood what functions are and how they are used and we are going to talk about function functions in more detail in my later videos so make sure to subscribe to my channel and click the Bell icon because only then you are going to be notified when I publish my next video and also follow me on my other social media platforms so Instagram and Twitter accounts through code Beauty thanks for watching and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ functions in this video I wanted to talk about function parameters or arguments and the first thing that I want to explain is what are parameters so what are arguments sometimes a function needs to receive a certain value a certain variable and then perform a certain task on that value so on that variable or it really needs the value of that variable in order to perform a certain task so let’s explain how we are going to create a function that receives a parameter in C+ plus so here I’m going to add a function of return type void and I’m going to say that that function is going to be called let’s say um introduce me introduce me like this and then in these parentheses here we can put parameters that our function is going to receive and after that in these curly brackets we are going to put the body of our function so as I said in these parentheses here we are going to put parameters and they are specified in a following way so first you are going to write out the type of your parameter and that is going to be string like this and then we are going to specify the name of our parameter and that is going to be let’s say name so we want to pass in this function that is going to introduce our user we want to really pass the name of our user and then what our function is going to do we are going to put in these curly brackets here and that is going to be really to introduce our user so our function is going to write out C out um let’s say my name is and then let’s write out this name that we have received in this function so I’m going to say name and I’m going to add end line like this okay okay so this is the way to pass a parameter so to pass a value to your function and as I said in my previous video you can really separate this um declaration and definition of a function so you can put really um Declaration of your function here so before your main function and then you can put definition of your function after your main function and if you don’t know how to do that make sure to watch my first video of this playlist okay so now I want to invoke this function here and how we do that well we specify the name of my function so I’m going to say introduce me and then since this this function receives um argument so since this function function receives a parameter here we really have to pass that parameter to our function and that I’m going to put here so here I’m going to pass an argument to my function so I’m going to say Salena like this and this here really is called argument so it is a value that we passed to our function and then here it is called parameter okay so now if I run my program you can see that my function has successfully introduced me so it says my name is Salina like this okay so here we have really here we have really um defined our function so the parameters that our function receives name return type and then uh the block of code that our function is going to execute and then here we have invoked our function and passed an argument which is this string of text here to our function which is going to be received in this name variable and then our function is going to really treat it as a variable which means that it is going to write that value here okay okay and now if I try to invoke this function once more so I’m going to copy this this line and then paste it here and let’s say that I’m going to pass it a different value so I’m going to say for example Anna okay you can see that uh now our function has successfully introduced two users so it has said my name is my name is Salina and then my name is Anna and there is also another thing that I want to explain and that is going to be how you can pass multiple arguments to your function and that is going to be in a following way so here in these parentheses where I have specified my first parameter you can put comma sign and then you specify your second third fourth fifth and um your other parameters so you specify the type which is also going to be string in this particular case and let’s say that my second parameter is going to be called City like this and let’s add another one so let’s say int H like this so now in this function I’m going to write out also where is my user from and also how old my user is so I’m going to write out C out um I am from and then let’s write out this city that we have passed in this function and add also end line and let’s write out also I am I am um age so I am however many years we have passed in this function and then let’s say years old old like this okay so now we have really introduced our user in more detail and we have passed to this function name City and age information for our user but as you can see here our function so our compiler really says that this function is not valid it has too few arguments in a function call meaning that this invocation this call of a function does not really contain all the necessary parameters or all the necessary arguments that we need to pass to our function which means that it really lacks the city and age parameters so here here I’m going to put comma as well and then I’m going to specify my city this here is bolded so it is bold and that means that we have to really pass a city value to our function so I’m going to write out mostar like this and then I am also going to specify age so I’m going to say 25 and let’s do the same for our ni user so I’m going say that our Anna is from New York and let’s say that she has 27 years old for example so now when I run my program you can see that we have successfully introduced two users so my name is Salina I am from mustar I am 25 years old and then also my name is Anna I from New York and I’m 27 years old okay so and we have really written this code only once and then we can reuse it however many times we need to so in this particular case we have reused it two times okay and another thing that I want to explain is going to be a default parameter so what is default parameter sometimes you really don’t want to pass a value to your function meaning that in a specific situation you don’t have that value or you don’t want to pass it and in that case you want to Define define find um theault value for your parameter so in the case where you use where your user does not pass that argument to your function for example you can say that your age parameter here has the default value of zero okay and that means that you don’t really have to pass this age parameter anymore so here I’m going to delete this age for my Anna user and let’s let’s run our program now in order to see how this function is going to behave for this first user and then also to see how it is going to behave for this second user user which did not specify this age parameter so this age argument and when I run it you can see that my first user has been introduced as it was previously so I am 25 years old and then the second user which did not specify age argument it really said I am zero years old meaning that our function has really taken this default value and it has written out I am and then this default value and then years old oh we are missing s here okay so I’m going to add s and I want to do a following thing so in the case where our user does not pass this value for our AG parameter I’m going to really check whether we have that value or we have this default value and then in the case where we have only this default value I don’t want to write this line of code here so I am going to say here if age is not equal to zero meaning that our user has specified some other value which is not zero then and only then we are going to write this line of code but in the case where we have only this default value we do not really want to write out that our user has zero years old cuz that does not make sense okay so as you can see now my first user has been introduced as I am 25 years old and then my name is Anna I from New York since Anna did not specify how old she is we haven’t written out her age here because of this check that we have added okay so that was about default parameter and there is one thing also that I want to um that I want to say about this default parameter and that is going to be really if I delete this is equal to zero and then I try to assign theault value to this city parameter so I say let’s say that city is equal to default like this you can see that the compiler is really underlying this function and it says okay it says theault argument not at end of parameter list meaning that you can specify theault argument only at the end of this list so if you want to specify default value for this parameter you will have to specify default value for this one as well so if I add theault value here so I have added zero then we can really add a default value for this second parameter and then we can add default value for this this one as well and now we are really uh left with so our compiler is not underlying this uh introduce me function name anymore okay and this does not make sense so I’m going to delete it but I wanted to explain that you cannot specify default value for your middle parameter if you did not specify a default value at the end of your parameters list and there is one more thing that I want to show you and that is going to be that you don’t have to hardcore these values as we did here so I’m going to comment these out and then I’m going to let my user Define his own values for these three variables so I am going to declare uh two variables of type string which are going to be name and City and then also I’m going to declare a variable of type int which is going to be called age and then I’m going to say see out so please enter a name like this it’s not capital letter okay and then see in name and then see out City so after my user has entered name I want him to enter City so C in city like this and then also I want to enter age and C in age like this so now after my user has has specified these three variables so name City and age I can pass these three in my introduce me function so I can say introduce me and then pass name and City and age like this and if I run my program now as you can see my program really asks me to enter a name so let’s say that name is going to be Tom for example and then let’s say that city is going to be London and age let’s say 32 okay and now as you can see my function has successfully introduced Tom so it says my name is Tom I am from London and I am 32 years old okay and you don’t really have to use these names for the variables in your main function so the names of these variables here do not have to be the same as these names here here so you can call these for example name one city 1 age 1 but you are going to have to use these names in your main function so you will have to say name one city 1 age one and here as well but these names do not have to be the same as these names here your program is going to work correctly you have to however have the same type here and here so this here has to be string if it has been declared as string here and this one as well and then age also has to be of type int if it has been declared as int here and once more if I run my program you can see that it asks for name so Tom New York and then age is going to be 22 and as you can see my name is Tom I’m from New York I am 22 years old so your program works just fine so I hope that you have understood how parameters behave in a function and how you can pass multiple parameters to your function and also how you can specify a default parameter in your function and if you enjoyed this video If you learned something new make sure to subscribe to my channel click the Bell icon so that you are notified when I publish my next video also like it and share it with your friends and follow me on my other social media so Instagram and Twitter account true code Beauty thanks for watching and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another video of C++ functions in this video I wanted to talk about return statements and return types of a function so what is return type of a function well return type of a function can be any data type that can be used in order to declare variable so it can be int float bull charart double um it can be any user created data type and it can be void as well which means that that function is not going to return anything and as we explained in one of my previous videos function is a block of code it is a code that is grouped together and that code is going to perform a specific task and here I want to make a difference between functions that return value and functions that don’t return value and functions that don’t return value can be for example um an example of a function that does not return value can be a function that writes out a menu to your user so you call that function that function writes out a menu to your user and the job of that function is done there so you are not expecting any value to be returned from that function but an example of a function that returns a value can be a function that performs a specific calculation let’s say on arguments that you pass to that function and after that calculation is performed you are EXP expecting a certain value so result of that calculation to be returned from that function and an important thing is that function can return only one value so only one return statement inside your fun function is going to be executed and I’m going to show that on the example that we are going to do in this video and now I’m going to show you really how functions make your code easier to read easier to understand and how they make your code reusable so we are going to show that on an example which we are going to first make without using a function so we are going to make a program that is going to determine whether a number is prime number or not and we are going to do that first without chosing functions and then I’m going to refactor that program so that it uses functions and then I’m going to show you how that program becomes instantly more readable more understandable and also how that makes your code reusable so as I said we are going to create a program that is going to determine whether our number is prime number or not and the definition of a prime number is a number that is divisible only by itself and by one so if that number is divisible by any other number so not one and not itself but any other number that means that that number is not prime number so the first thing that I’m going to do here is I’m going to declare a variable of type in call it number and then I’m going to let my user enter that number so I’m going to write out the message to my user and then use the C in command in order to get that number and store it in my number variable and then let’s explain the algorithm that we are going to use in order to determine whether that number is prime number or not the definition of a prime number is a number that is divisible only by one and by itself which means that if you can find any other number number besides one and that number itself that you can use to divide it by this number that are user has entered and get the remainder of zero that means that this number is not prime number so how we are going to solve this problem well we are going to iterate through all the numbers between two and this number minus one and then we are going to check whether this number is divisible by that count value so I’m going to write out for INT I is equal to 2 and then I is less than number and in each iteration I want to increase the value of my I counter so why haven’t I used one here and why haven’t I said here that this I is less than or equal to the number because every number can be divided by one so we do not need really to check that and also every number can be divided by itself and we do not need to check that as well so here we are going to iterate through this interval so between two and while I is less than that number that our user has entered and here we are going to check whether this number that our user has entered can be divided by any value of ouri so in any iteration which we are going to go through and in the situation where it can be divided we are going to use a flag which I’m going to declare here and that flag is going to be of type bu and let’s call it is prime flag like this okay and this flag is initially going to be assigned a value of true so we are going to initially say that all the numbers are prime numbers and then we want to prove that wrong for the numbers that are not so we are going to use this flag in a following way here I’m going to check in each iteration if our number that our user has entered can be divided by that current counter so by the current value of I so I’m going to use this modular operation and I’m going to say if this number modulated by I is going to give us the result of zero so when you divide this number by I do you get the remaining of zero and if this operation here results as true we are going to say that our is prime flag is going to be false so we are going to assign the false value to our is prime flag because we have clearly found one number which is not one and that number itself that can be used in order to make this condition true so here I’m going to write out is prime flag and then I’m going to assign it a value of false and and then another thing is going to be really if we find one number so even one number is enough to find that that really fulfills this condition here in order to say that our number is not prime number and in that situation we do not really need to check any more numbers because we have determined that our number is not prime number so here I can use break statement so I can say if you find only one number please do not iterate through this for Loop anymore because we do not need to find any more numbers so in this situation here we have iterated through all the numbers between two and that number that our user has entered we have checked whether that number can be divided by any value of our I counter and if it can be divided meaning that it gives the remainder of zero after that division we are going to set our Prime flag to false and we are going to break from our four Loop and after we have have successfully fulfilled this so after we have successfully iterated through this for Loop or we have fulfilled this condition in one iteration we are going to leave our for Loop and we are going to come to this line of code here and here I want to check what has happened with my is prime flag meaning I want to check whether this is prime flag has has stayed true which means that our for Loop has executed all of its iteration and it has not find any number that can be really used to fulfill this condition here and in that particular situation that flag is going to stay true and that is going to mean that our number is prime number so here I’m going to write out if is prime flag meaning if this value here if this variable here holds the value of true we are going to write out C out like this and we are going to write out prime number like this okay and let’s also add end line here but in a situation where this here does not hold a value of true that means that in uh one iteration so it can be the first one it can be the last one it can be any in between we have really found at least one number that can be used to fulfill this condition and that has set our is prime flag to fals and in that situation we are going to use our else statement and we are going to copy this and we are going to really modify it to say not prime number like this okay so now if I run this program you can see that it asks us to enter a number I’m going to enter a number um let’s say five for example and it says that five is prime number which is correct five is a prime number and let’s run it one more time and let’s say that we want to check six this time and as you can see our program has written out that six is not prime number and six really is not prime number because it is divisible by two and by three besides from one and itself okay so now you have seen how we can really solve this problem and let’s try to refactor this program here in order to use functions so how we are going to do that well the first thing that we need to do is we need to create function and I’m going to do that here and since this part of code here this is the part of code that we are going to move to our function and since this part of part of code here determines really whether our number is prime number or not and then sets this flag to true or false let’s say that we are going to create a function which is going to check as as well whether the number is prime number or not and then it is going to return a value true or false meaning the number is prime number or the number is not prime number and that we are going to write here so here I’m going to put the return type of my function and that is going to be bull so that is the return type of my function and then the name for my function is going to be is prime number like this and then let’s say that we want to pass an argument to that function and that argument is going to be of type int and that argument is going to be really the number that we want to oh the number that we want to check so we are going to pass the number to our function that function is going to in this body here check whether that number is prime number or not and then it is going to return bull value meaning true or false so if the number is prime number it is going to return true and if the number is not prime number it is going to return the value of false and how we are going to do that very simple so you see this code here I’m going to comment this code here and I’m going to really um oh I should have copied it before so I’m going to copy this code here and I’m going to move it to my function okay and we have previously explained this code here so we do not need to explain it again but what I’m going to say here is after this algorithm is done so after we have really uh stored the value of true or false so we have stored the value of false in our is prime flag or we have really kept this True Value inside our S Prime flag what we need to do is we really just need to say return is Prime oh is prime flag like this so after we have checked whether our number is prime or not we can really just return that value from our function okay so we can now after we have moved this code to our function we can delete it from here like this okay and then as you can see we have been left with this is prime flag which is not declared now so identifier is prime flag is undefined meaning that now we have to really declare it and we have to assign it a value so I’m going to say here bow is prime flag like this and then the value that I’m going to assign to my is prime flag variable is going to be whatever value our function returns so here I’m going to say please call my function is prime number oh this prime number like this okay and then pass to that function this argument here so our function expects a value and that is going to be this number that we want to check whether it’s prime or not and then after this function here is done it is going to return a value of either true or false and that value is going to be stored inside our S Prime flag variable okay so now if I run my program you can see that it asks us again to enter a number so so I’m going to say let’s check number nine for example and it says that number nine is not prime number which is not because it is divisible by three and let’s check number um 13 for example and 13 is prime number okay so after we have really used this function in order to perform this particular task I want to show you one more thing and that is going to be how we can refactor this fun function here in order to make it even more simple and if you look at this function here you can say you can see that this is prime flag is set to True here and then in this situation where we find number that can be used to fulfill this condition here we set that is prime flag to false and then we use this break statement in order to break our for Loop so we are not going to iterate anymore through our for Loop when we come to this break statement and then we really go here and then we return that is prime flag but we can do that in a more simple Manner and that is going to be really here so here in a situation where we find number that is really fulfilling this condition here we can say at this very line of code we can say return false like this because this and this code of this code here we can delete and what I can use this return statement because this return statement is going to really break our for Loop and it is going to return the value from our is prime number function meaning it is going to do two things so we do not really need to set the flag and then return the value of that flag at the end but we can really say if you find any number that can fulfill this condition return false immediately so break for Loop and also return the value from our function meaning and the execution of that function because we have found our result and then here as well we do not really need to declare this is prime flag to true and then return it because it is clearly true so we can say here return true and then remove this flag from here like this and then as well I can remove these curly brackets and make my code even more readable so now I want to explain what we have done so here we are passing a number to our s prime number function then we are iterating through all the numbers between two and that number we have skipped one and that number itself because all the numbers are divisible by one and by that by that number itself and then we are checking whether any of these I values between two and number fulfill this condition here that would mean that that number is divisible by that current value of I and that would mean that our number is not prime number so at that very moment we can say please return false because this number is clearly not prime number but in a situation where this for Loop has really iterated through all of its iterations and it has not found any number that fulfills this condition here that means that this return false statement has not been executed and then that in that particular situation this return statement here is going to be executed and this return statement here says return true okay so this part of code here has not changed nor it is going to change and if I run it again you can see it asks me to enter a number I am going to enter number seven then it is going to check whether that number is prime number so it is going to call our function and assign the result of that to our is prime flag variable and I press enter here you can see that we get result prime number because 7 is prime number okay and that is really making your code much more readable and reusable as well so this main function here is much more clean than it was before and now I want to show you the most beautiful thing and that is going to be how we can make this is prime number function reusable so let’s say that I am going to delete all of this code here so I’m going to delete it and now I have I know that I have written out this is prime number function and I know that that I can really invoke this function and use it whenever I need to determine whether some number is prime number or not so let’s say that now we want to check all the prime numbers between 1 and a th000 for example so how we are going to do that well we are going to iterate through all the numbers between one so I is equal to 1 and then I is less than or equal to 1,000 and increase our I in each iteration by one like this and then in each iteration I really am going to just send this I to my function and I’m going to really get the result from this function so this function is going to tell me whether that I is prime number or not so here I’m going to declare a variable of type ball and I’m going to call it is prime and I’m going to assign to that variable a value of is prime number when I pass it the value of I so whatever this function here returns whatever this function here results in I’m going to assign that result to my is prime variable okay and now what I want to write out is all the numbers that I that are prime numbers between one and this thousand value so I’m going to say if is prime like this I’m going to write out C out and then I want to write out the current value of my I counter because that is number that we are talking about so I is prime number number like this okay and I’m going to add a new line at the end so this code here is really going to check all the prime numbers between one and a th and it is going to write out all the prime numbers in that interval and for solving this particular problem so whether the number is prime number or not we have used this is prime number function which we have written once and we have really tested it once we know that it works as it should and now we can really close it we do not need to really think about how we are going to perform this task never again so we have written it we have oh we have tested it and now we have this algorithm stored in our is prime number function and we know that this function Works in a following way so we pass it a value and then this function return returns us true or false variable meaning that the number is prime number or not so once more if I run my program you can see that we have gotten the expected result so we have gotten all the prime numbers between one and a th000 okay and there are a lot of numbers okay so you can check these numbers on your calculator if you want and let’s say now that we really want to check all the numbers between not just one and a th000 but let’s say 1 and 10,000 or 100,000 okay we can call just this is prime number function pass it the value of our I and then check the value that this function has returned and this function has really told us whether the number is prime number or not and if the number is prime number we just want to write that number out to our user so if I run this function again you can see that it is writing out all the prime numbers between 1 and 100,000 and there there is a lot of numbers again so you can even count how many prime numbers there are in this interval so between 1 and 100 ,000 and let’s say that I’m going to leave that part to you so if you want to really uh upgrade this program so that it counts how many prime numbers there are in this interval you can write that code in the comments down below and I’m going to check it and as well you can leave it for other people to see and use if they need it so I hope that you have understood this return type of a function and in this particular situation it was Bull type but you can have as a return type from your function you can have any other data type so you can have float chart int uh string whichever data type you can really uh use in order to create a variable you can use that data type in order to be um return type of your function so in this particular situation I wanted to show you an example of a bit of a more complex function let’s say that is going to determine whether your number is prime number or not and it is going to really receive that number that you want to check as an argument as parameter and then it is going to return only true or false meaning that number is prime number or that number is not prime number and once you have written this code and once you have tested it you do not really need to bother with this function anymore you do not need to remember this code nor read it nor even understand it in order to use this function only in a situation where you have a b bug and that bug is in this particular function only in that situation you are going to need to really reread this function and fix that bug in any other situation you can really just use this is prime number function uh and as well we could have made other functions with other return types we could have created for example a function that is going to um to sum two numbers so you are going to pass to a function two numbers let’s say number a and number B which your user enters and then that function is going to sum those two numbers and it is going to return the result to your main function from where you are going to invoke that function and as well I can leave that to you so you can really practice writing functions on that example for example and you can put the code for that um exercise in the comments down below as well and I’m going to check it and in this particular example as I said I wanted to show you a bit more complex example so that you can really understand why we need functions and why they make our code understandable and reusable and if someone else comes to your code now and he sees that he has um is prime number function he does not really need to open this function and read how this function works it is pretty clear from this this declaration itself that this function receives a number and determines whether that number is prime number or not and returns that true or false back to the invoker back to the function that called that invoked this is primed is prime number okay so I hope that you like this video I hope that you enjoyed watching this video if you did make sure to like it and also subscribe to my channel and follow me on my other social media so Instagram Twitter account at true code Beauty the link is going to be in the description and I’m going to see you in my next video bye hi everyone welcome to my channel welcome to another C++ functions video and in this video I want to talk about concept that is related to functions which is function overloading so I want to First explain what function overloading is it means that you can create multiple functions with the same name but those functions are going to have different parameters so here I have opened my visual studio stud and let’s create a function which is going to be called for example sum and I’m going to put the return type of that function to be int so integer if you’re not familiar with function return types I’m going to link that video here so make sure to watch it so first thing we write the return type of our function and then we write the name of our function and as I said our function is going to be called sum and then I want to pass parameters to this function so I really want to declare which parameters this function is going to receive and those are going to be two parameters the first one is going to be integer and I’m going to call it a and then the second one is also going to be integer and I’m going to call it B like this so this is going to be our first function and let’s also say that we want to create another function which is also going to be called sum but this time it is going to be of return type double like this and then I’m going going to call it sum as I already said and this function is going to receive two parameters as well but those are going to be of type double so I’m going to say double A and then Double B like this okay and as you can see these functions are underlined because it says function definition for sum not found meaning we have declared our functions here but these functions are missing the the definition so definition is missing for this function and and then this function as well and let’s also create one more function so I’m going to say for example float and then I’m also going to call it sum and let’s say that this time I’m going to give it um three parameters so I’m going to say float um let’s say float a and then float B and then float C like this okay and then what I want to do is I want to create definitions for these functions which I’m going to do here here so the first one that I want to Define is going to be this one here so I’m going to copy its declaration and then I’m going to add these curly brackets and here I’m going to put a definition for this function so let’s say that I’m going to create a variable of type int and I’m going to call that variable result like this and I’m going to assign it the result of a + b like this and then what I want to do is return this result from my function fun so here I’m going to say return result like this so this would be one way of returning the result from your function and then another way to do the same thing is going to be to say really return here so instead of creating a variable that is going to hold this result you can really return this result itself from your function so you can say here return A+ B and now we can delete this line and as you can see this function became shorter and more readable so now I want to Define this function here so I’m going to say double okay I should have copied this and then double b as well like this so here I want to put the definition for this function here and this function oh I’m missing the name for my function it is called sum so this is return type this is the name of our function and then these are parameters of our function and here I want to say return a + b as well but this time our a and our B are going to be of type double so in this situation there were integers and then in this situation there are decimal numbers okay and let’s also create a definition for this function here so this float function and this time I’m going to copy it okay and I’m going to add these curly brackets and here I want to write return and then A + B + C like this okay so now how we are going to test this we can call these functions we can invoke these functions here in our main function and the first one that I want to invoke is going to be our in function so I’m going to say C out and then this line of code is going to write out whatever our function returns so I’m going to say please write out whatever my sum function returns and then I want to pass to my sum function two numbers and those two numbers are going to be two integers and as you can see here we have really helped from our Intel ense which says you have three possible functions that you can call so you have overloaded three functions and you can navigate through these using your arrows so the first one is going to be our function that has float return type and that function return that function receives three parameters of type float and then the second one is going to be the one that has return type double and that function receives two double parameters and then the third one is going to be the one that returns integer and that function receives two integer values so here I want to call our integer function and I want to pass to that function four and three for example and let’s also add end line like this and now if I run my program we can expect to see the result of this line of code here and as you can see the result is seven meaning that our function has been invoked and then this line of code has received the result of summing these two numbers and that has been written out in our console and the result of summing four and three is seven Okay so now I’m going to stop this console and I want to test these other two functions so I’m going to say see out and let’s test our some function that receives two double parameters so it is going to be this one here so I’m going to say 4.4 and then 3.3 for example and I’m going to add end line at the end as well okay and now if I run this program you can see that our second line is is 7.7 meaning 4.4 + 3.3 is going to be 7.7 which is correct so we have successfully tested this function here so this double function that returns double and receives also two double parameters and then let’s also test this third function so here I want to say C out sum and then here I want to pass three parameters the first one is going to be let’s say four .4 and then the second one is going to be 3.3 and then oh three decimal points 3.3 and then the third one is going to be 2.2 for example okay now this invocation should invoke this float function so when I run my program as you can see here this third line contains the result of summing these three numbers and you can check this out using a calculator but this is the correct result so as I already explained this represents function overloading and we have created three functions the first one is the one that receives in types and returns in type as well then the second one receives double so two double uh variables two double parameters and returns double as well and then the third one really contains three arguments so three parameter parameters but it has the same name as these previous two and then it returns float type as well and then now when we call when we invoke those functions we can really pass different data types and depending on those data types we are going to call adequate function so in this first line we are going to invoke this and then in this second line we are going to invoke this and then in our third line we are going to invoke this okay so I hope that you understood what function overloading is and also I hope that you have understood that you don’t have to create functions with different names when they are doing uh almost the same thing so these three functions these three are doing the same thing they are summing the numbers that we pass to them only in this particular situation the first one is summing integers the second one is summing decimal point numbers and then this third one really sums three numbers and we have overloaded this sum function and we didn’t have to create functions that are for example sum int and then some double oh double and then some float like this so we didn’t have to name these like some int some double and some float but we can use the same name as long as we have different parameters different number of parameters different return type or something similar in that particular situation our compiler is going to decide on its own which function it should call I hope that you have understood what function overloading is and how you can use that thanks for watching and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and give this video a thumbs up as well now in this video I want to show you how you can build an ATM application and the first thing that I want to do in order to build this application is going to be to plan the functionalities that this application is going to have so I’m going to put a comment here and then here we are going to write all the functionalities that our ATM is going to have so the first one is going to be to check the balance so I’m going to write that so check balance and then another functionality is going to be to deposit money so let’s say deposit like this okay and then what else well we should be able to withdraw money as well so let’s write that okay and I definitely want to show the menu to my user so I’m going to write that as well so I’m going to say say show menu like this okay now after we have written these functionalities that we want to implement the first one that I want to implement is going to be this show menu function so I’m going to put the code for this show menu functionality in a function and let’s create that function here so I’m going to say void it is going to be of void return type let’s call it show menu like this oh like this okay and then what I want to do here is I want to write out the menu to my user so I’m going to say see out and then let’s add these star symbols four 5 6 7 8 9 10 that is going to be enough like this and then I’m going to copy this on the other side so that it looks proportional okay and then the first option that I want to write out my user is going to be uh let’s add first and then check balance so that is going to be the first option that our user can choose and then let’s add couple more of these so the second option is going to be deposit so I’m going to write that like this and then the third option is going to be to with draw so I’m going to copy this here and then paste it here okay and then let’s add these star symbols at the end of our menu like this okay now after we have created this show menu function what I want to do is I want to invoke this function so here I’m going to say um show menu like this okay and if I run my program as you can see we have this menu shown to our user now what should happen next is we should allow our user to choose one of these three options so the next thing that I will do is I will create a variable so let’s create it here let’s say int and that variable is going to store the option that our user chooses so I’m going to call it option like this and then here after I have written out this menu so this menu here to my user I’m going to say see out and then please choose an option or whatever message you want to show so option like this and then I want to accept the value that my user enters in this option variable so I’m going to say C in option like this okay now if I run this program again as you can see it asks for option and if I select any of these three options if I press three for example you can see that my program closes because we haven’t written any code after this line here so what I want to do now is I want to handle these three options and I’m going to handle these three options using a switch case so I’m going to write switch okay and then here I want to put this option variable so that is going to be the value that I want to check so I’m going to say switch option and then in the case that our user has entered option one what I want to do is I want to write out the balance to my user and in order to do that I’m going to create a balance variable because we do not have a balanced variable yet so I’m going to give it a type double like this and call it balance okay and let’s assign it initial value of uh for example 5 100 okay so that is going to be the initial value of our balance um variable so here I’m going to say C out and then let’s say balance is okay and then let’s write out this balance variable and I’m going to add this dollar symbol at the end and I’m going to add end line as well okay so that is going to happen in the case that our user enters value or option one and I’m going to add break at the end of each case so here as well okay now our second case case is going to be when our user enters option two like this so in that second case we should allow our user to deposit money so here I’m going to say see out and then let’s write out um deposit amount so we want to ask our user how how much money he wants to deposit so in order to accept the value that our user enters I’m going to create a variable which I’m going to call deposit amount so let’s create that variable it’s going to be of type double and it is going to be called deposit amount like this and then I’m going to accept the value that our user enters here in my deposit amount variable like this okay and now what I want to do with this deposit amount is I want to add it to my balance so I’m going to say balance is now going to be equal to whatever my balance was previously holding plus this deposit amount like this and then let’s add break at the end as well okay and then our third option our third case is going to be when our user wants to withdraw money so I’m going to copy this case here and then paste it here and here I want to write out a different message to my user so I’m going to say withdraw amount let’s copy this okay and then this variable I’m going to call it withdraw amount like this okay and then here I want to enter that withdraw amount and what I’m going to do with my balance in that situation is going to be I want to subtract that withdraw amount from my balance but I want to subtract that withdraw amount only in a situation where this withdraw amount is equal to or less than this balance so I’m going to say here if withdraw where it is withraw amount is less than or equal to balance like this then I want to give that money to my user else meaning if our user has has requested more money than he has I’m going to write out a message so I’m going to say not enough money like this okay and that is going to happen so this code here is going to happen in the case that our user enters option three which is this one here okay so now if I run my program you can see that it asks option and let’s see that we want this first option so this check balance and it says that balance is 500 and if I press anything else as you can see my program is going to close and that is because we haven’t implemented any Loop that is going to iterate this code here so let’s add that Loop now so here I’m going to say do so do while loop is the one that I want to use and then in inside my do while loop I’m going to put all of this code here so let’s add a curly bracket close curly bracket at the end like this and then I’m going to say please do this block of code here while and then I want to do this code here while the option that my user has ento option that my user has entered is not equal to four four and when my user enters option four we should stop the execution of our program so in order for our user to know that for means exit we are going to write that here in our show menu function so here I’m going to add this fourth option which is going to exit our program like this okay now if I run this code you can see that it says that it has this menu here and then it asks for option so I’m going to enter option one for example okay and this option one says that balance is $500 now let’s enter this second option let’s enter this deposit option so I’m going to press two and as you can see it says please enter deposit amount so let’s say that I want to deposit $700 for example okay now if I check my b balance again so I have entered this option one and then I press enter as you can see it says that my balance is $1,200 and there is one thing that I want to do with this program because I don’t like the way that this looks it is not really readable so I’m going to add um clear screen so clear console command and I’m going to add that here so after my user chooses his option I want to clear the console so I’m going to say system and then let’s add CLS which is a command that is going to clear my console clear my um window so if I run my program again as you can see again we have this menu and then it asks us for the option so let’s say that I want to check my balance it says that my balance is $500 now let’s try to withdraw money let’s say that I want to withdraw money and I want to withdraw $600 for example and then it says not enough money because we had $500 um on our account so now what I want to do is let’s say for example that I want to deposit $100 okay and then I want to withdraw $600 okay and if I check my balance now as you can see balance is zero and in order to test this exit functionality let’s press for and then if I press enter as you can see my application is going to close now another thing that I want to check is going to be what is going to happen if our user enters option that is not valid meaning an option that is not any of these for that we have covered here so if I run my program again as you can see um it asks us to enter options so I’m going to enter option five for example which is not valid and nothing has happened happened if I enter option n nothing has happened again 99 okay now you get the point what is happening here is our program tries to find this adequate option so option five option n whichever one we have selected and since that option does not exist in these cases here our program continues to this while check and then it checks that the option is not equal to four and because our option is five or 9 or 99 which is not equal to 4 then our DU while iterates again and then it writes this show menu again and then it asks us to enter this option again so these three options are the only valid options that you can select and then four means that you are going to exit your program now here we have created this show menu function that has uh check balance deposit withdraw and then this exit option and what I want to suggest to you is how you can upgrade this program on your own so for example you can create two users two accounts and what you can do with those two users is you can Implement transfer option meaning um transfer money from one account to the other account and I’m going to leave that up to you and if some of you decide to write that code make sure to paste it in the comments down below and I’m going to review that code and and some other people may find it useful as well so I hope that you enjoyed this video if you did give it a thumbs up don’t forget to subscribe to my channel and hit that Bell icon as well and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos so if you’re interested in those please subscribe to my channel and give this video a thumbs up because that helps me a lot to reach more people and spread programming knowledge and in this video I want to talk about generics and templates in C++ so what are generics the idea of generics was born when programmers decided that they want to be able to use the same code but with different data types so the logic is the same but data types are different or if you want another explanation when we talked about functions we said that we can pass parameters to functions and what we said as well is that types of those parameters had to be known beforehand so you had to hardcode types of those parameters whether it is int or Char or float or any other data type that had to be known beforehand but with this idea of generics what we should be able to do is we should be able to pass that type itself to a function so we should be able to determine which type we want that function to operate on and the best way for you to understand and this will be on an example so that is exactly what I will do so let’s say that in my visual studio I want to create a function that will swap the values of two variables and if you don’t have an idea how you can swap values of two variables I have a video where I explained that uh in a very visual way so you can use that video I will link it here so here I will just type very quickly the code for that and if you need explanation again you can watch that video so I will say that that function will be of return type void and I will call it Swap and then what I want to pass this function will be two integer variables so I will say int a and then int int B like this okay but because I want to have both of these variables available in my main function which will invoke my swap function I will have to pass these two variables using a reference and again if you’re not familiar with passing parameters by a reference I will link another video here where I talk about that and I think that I made more than one video on my channel talking about passing variables by a reference to a function so I will put those in the description as well so the code for this swap function will be the following so I will say int temp I will create a temporary variable and to that temporary variable I will assign the value that my variable a is hold holding and then I will assign to my variable a the value that my B is holding like this and then to my B I will assign whatever my temp variable is holding again if you need to understand this in more detail I will put video in the description of this logic here okay so now this function should give us the ability to swap values of two variables and the way to test this function is the following so I will say please create two variables call them A and B like this and to my a variable I will assign the value of five for example and then B will have the value of s so let’s write out values of these two variables let’s say a and then just make a separation and then value of B like this okay so now what I want to do is I want to invoke this swap function I will say Swap and then I will pass my a and then B like this and if I copy this line after the invocation of my swap function we will see if the values had indeed been changed so if I run my program as you can see this line of code here has resulted with this output so 57 and then after invoking our swap function the values inside these two variables so A and B have successfully been swapped so now it is 75 okay excellent so that means that this function does what it should do now what happens if I want to swap values of two Char variables for example what I would have to do is I would have to copy this function paste it here and then I will adjust it to be able to work with Char variables so I will say Char and then I will change this with Char as well and then here I will put Char as well now this swap function should work with char parameters and this is called function overloading if you’re not familiar with function overloading I made that video explaining how it works so I will link it here if you want to watch it and I will put it in the description as well so this function should be able to work with Char parameters now and in order to test this function let’s create two variables of type Char so Char C and then d as well and to my C variable I will assign the value of letter C and then to d i will assign the value of letter D excellent so I will copy this line of code here and I will just adjust it so that now we write out the value of our C variable and then here we write out the value of my D variable like this and then after this line of code here I want to invoke this swap function so I will say Swap and then pass it C and D like this and I want to write out my C and D variables after this line of code here so after I have swapped the values just to check that they successfully have been swapped so if I run my code as you can see we have this out output here so c and d and then after swapping the values inside these two variables we have this output here which means that they have been swapped successfully now um I see a problem with this approach here and the problem is following what happens if I want for example to swap values of two float variables and then two doubles and then two booleans and so on well I would have to create this swap function for each data type and that is repeating code and we shouldn’t really be repeating code unless we really need to and in this particular situation we don’t so there is a simple solution for this problem and that simple solution will be using generics and the idea is to be able to use the same function the same logic but with different data types so again we will be solving this problem using generics and the way that generics are implemented is using templates so how do you make a template in C++ well you use keyword that is template like this and then inside these angled brackets you will put the name of your type so you will say type name and um most common type name that is used is called T but you can use whichever type name you want you can say type as well if you want that should work but I will use T because that is the recommended way so now that I have created a template and I have said I want to create a new type which is called T I will have to use this T in my swap function so now I will change this int with my T so I will say t here and and then T here and I will create T here as well okay now this T is called generic data type and this swap function is now a generic function which means that this swap function can be used on any data type whose variables can be swapped using this logic here so in one invocation it can be swapping integer variables and then in another it can be swapping characters and then in the next one floats and then doubles and booleans and so on on so now that means that we can really remove this swap function because we do not need it because as we said this function here is a generic function and it can be used with different data types so now I will delete this swap function here and if I run this code this code should work as it did before so let’s press this button okay and as you can see 57 and then 75 this function indeed works for integers and then CD and then DC so it works for characters as well and let’s close this okay now what you can do here as well here in the invocation in this 15th line invoking this function for two integer variables what you can do is you can specify the type itself as well so you can say I am invoking this swap function for my integer type so now when this function is invoked it will go here and this T keyword will be changed with this int so it will be int here and then int here and then int here and it will do the swapping this function here will do the swapping for INT data type and then in this 20th line here you can say as well Char if you want you don’t have to that process will happen automatically because you have declared C and D as charart type so those C and D of type chart will be passed here and then Char will be here here and Char will be here and then Char will be here as well so what we have acquired with this template function here is to be able to use the same code with different data types we have just demonstrated how that can be invoked for integer data type and then how we can use it for character and you can do on your own float or double or bull and so on okay and then what you can do as well is you can use any other name for your generic type T is something that is used commonly in literature but you can use type as well if you want so you can say oh you can say type and then you would have to say type here and then type here and here as well okay that should work the same way that it did previously and then another thing that you will find in literature is instead of this type name keyword you can use class um it really depends on you whichever one you want to use you can and again if you know what classes are class is a type as well so it is userdefined type so this should work as well the same way that type name keyword did and if you’re not familiar with classes and objects and objectoriented programming I have course which I will link here so make sure to watch that so I hope that this video helped you understand what are generics and templates and what is the main idea behind them and we will be talking about this of course in my future videos and in more depth so so make sure to subscribe to my channel and hit that Bell icon as well so that you don’t miss a video that I publish and if you like this one please give it a thumbs up that helps me to reach more people and help more people with programming so thank you very much for watching and I am going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and in this video I want talk about recursion and recursive functions in my previous videos we already talked about functions in general so you should be familiar with that and then in this video I want to show you on an example what is recursion and how it works so what is recursion recursion is a process where a function invokes itself and that function that invokes itself is called recursive function that is pretty simple definition so let’s show that on an example and the example that I want to give you today in this video will be the sum all the numbers between M and N which are the numbers that our user defines and the first thing that I want to show is we will solve that problem using Loops which is a solution that you should already be familiar with and if you don’t understand Loops you can find videos on Loops in my C++ for beginers playlist so you can watch that and then after we solve that problem using Loops I want to show you how you can solve that same problem using recursion so if you want to skip directly to the recursion part I’m going to leave the link to that part the time stamp in the description of this video so that you can navigate yourself directly to the recursion part if you want now as I already said the problem that we want to solve in this video is going to be to sum all numbers between number M and N so let’s write the text of our task here I’m going to say sum number first between M and N okay so this is going to be our task and then here as I already said I will first solve this problem using Loops so considering that we have to sum numbers between number M and N I will create two variables so int M and N which are two variables to hold the numbers that our user enters you can do that part I’m going to hard code those values here so let’s say that my number M that my variable M holds value of two and then n holds the value of four for example uh and one thing if you are going to ask your user to enter the numbers for these two variables what you should do is you should make sure that your user enters smaller number in your variable M and then greater number in your variable n and if that is not the case there are a few ways to solve that problem so you can swap the values in those in these two variables if your user enters bigger number here and then smaller one here and if you don’t know how to swap the values into variables I’m going to put a link which contains the explanation for that here and then you can as well just ask your user to enter again and again and again until this number is smaller and then this one is greater uh and as I already said I’m just going to hard code these two values here okay so now that we have our number M and our number n what I’m going to do is I will write a loop which is going to solve this problem here and the easiest way to solve this problem here is going to be using four Loops so I will say four and then I will declare a counter in I and that counter should start at the value of our variable M so I is equal to M and then it should count until it comes to the value of n so while our I is less than or equal to n oh sorry so while our I is less than or equal to n and then in each iteration I’m going to increase the value of my counter by one okay so what I should do in this for Loop here is I will just sum all the values that my counter holds in a specific iteration so I will create a variable here which is going to be of type int and I will call it sum and initial Val for our sum will be zero because zero is neutral when it comes to adding so here I will write sum is equal to whatever our sum was previously holding and then to that I will add the value of my I counter like this so this should be pretty much it to solve this problem and at the end of this for Loop we should have the sum of the numbers between two and four in this variable here so let’s write that out I will say C out out sum is equal to and then write out the value that are sum variable holds and if you are not familiar with this code here you should watch my video related to for Loop and I’m going to link it here or leave it in the description down below so let’s run our program and test this code here okay it says that our sum is equal to 9 so 2 + 3 is 5 + 4 is 9 so that is the correct result so this approach here will solve this problem using loops and this is something that you should already be familiar with and now I’m going to show you how you can solve the same problem using recursive functions so let’s comment this code out because I don’t need it anymore like this and then what I want to do is I want to create a function of return type int and I will call it let’s call it recursive sum like this okay so this recursive sum function will receive two parameters and those two will be integer number M and then integer which is called number n so we will pass these two values to our recursive sum function and what should we do inside this recursive sum function well the same way that we do not want this four Loop to be an infinite Loop we do not want our function to be an infinite recursion so here we specified the condition when our for Loop is going to stop to execute so here when our counter I reaches the value of n that is the point when our for Loop stops running and then here in our recursive sum function we will have to define a base case which is going to break this recursion so as I already said it is called base case and for this recursive sum function our base case will be following so I will say if our m is equal to n in that case what I want to do is I want to return from this recursive sum function value of our M or n so I will say return M it doesn’t really matter which one you return because here we have checked that they are equal okay so this here is our base case something that is going to break our recursion so that we do not end up with an infinite recursion okay and then what I want to do here is I want to do that recursive part so that part where the function is invoking itself and what we should do here is the following so I will say return and here I want to return the value of M so the current value of our M parameter and then I will add to that the following expression so I will invoke again our recursive sum function but this time I will pass to this recursive sum function the value of our m + 1 and then the value of our n variable okay so what this code here is going to do the same way that here we have increased the value of our I counter in each iteration here I’m increasing the value of our M parameter in each recursion so that at a certain point we are going to reach this condition here so our M and RN will be equal and at that point our recursion is going to stop so how is this code here going to behave well let’s write out invocation of this function here and then we are going to go line by line through the execution of this program so that you can understand how this recursion is going to work so let’s say see out sum is equal to and then I will invoke this recursive sum so recursive sum and here I want to pass the value of M and N like this so when we come to this 14th line of code here what is going to happen is we will pass values of two and four to this function here so I will write that my M holds the value of two and then n holds the value of four okay and then we come to this line of code here and we check is our m equal to our n so is 2 equal to 4 the answer to that is no so we do not execute this but we go here and what this line of code here says it says please return from this function the following so return current value of my M which is two and then to that I will add whatever this recursive sum function returns to me if I pass it m + one and N so here I will invoke function again and I’m going to use these parentheses to indicate invocation of a function so here we are invoking recursive sum function again and this time we are passing it values of n + one which is three and then n stays the same because our n is our upper limit so that we can know when we are going to break this recursion so here is the invocation of this function again and then we have entered this function using these two parameters so we go here again and we check again is our m equal to r n so is 3 equal to 4 the answer to that is no so we do not execute this line of code but we go here again and what this line of code here says it says please return current value of M which is in this recursion three and then add to that whatever this recursive sum function returns for these parameters here so here I will invoke recursion again and I will indicate that again with these parentheses so we inv invoke our recursive function again with parameters of n + 1 which is four and then n stays the same so we go here again and we check is 4 equal to 4 and the answer to that question now is yes so now we return the value of RM which is four so here I will say that this recursion Returns the value of four and then we go backwards so here we have all the invocations of this recursive of some function so the result the end result of this recursion is going to be the result of this expression here so let’s calculate what this recursive sum function is going to return finally at the end so this is one recursion and that recursion Returns the value of four so I’m going to delete these parentheses and then the recursion prior to that returned the value of 3 + 4 which is 7 and then if we come out this recursion as well so I will delete these parentheses the result of our recursive sum function will be the result of this expression so 2 + 7 will be equal to 9 so this is going to be the result of our recursive sum function so if I run my program now we should get this nine as the result of executing this recursive Su function so let’s run our program and as you can see some is equal to 9 and if I increase these M and N values so if I say for example please calculate some between 1 and 4 we should get 10 this time so 10 okay and then here if I add number five for example we should get value of 15 right so we get that the result is 15 and what you can do here now of course you can say please calculate some of the numbers between 1 and 551 for example but this is going to be hard to check so if I run my program as you can see we get uh this very very big number okay one very important thing that you must have each time that you work with recursion is the following so every time that you work with recursion you must have this base case here which will stop the recursion from happening again and again and again so this base case here will stop invoking recursive some function at a certain point when a certain condition is reached and unless you have that base case which will stop the recursion you will have that function invoking itself infinitely so infinite number of times and you will get an error so in order to demonstrate that I will remove this base case here so let’s comment it out and if I run my program now as you can see we get un stack Overflow error which means that as I already said this recursive sum function is invoking itself again and again and again infinite number of times and this recursion never stops and in order to stop it you need to have a base case which will stop the recursion so I’m going to close the program now and I will return this code here which is our base case and if I run my program again as you can see we get the expected result and the error has disappeared so that is something very important to remember each time that you use recursion you must have a base case to break that recursion from happening infinite number of times so I hope that you understood what is recursion and how it works and in order to practice on your own I will leave a task for you and the task is following so I want you to do the factorial of a number using recursion calculate factorial of a number using recursion and if you are not familiar with what factorial is or how it is calculated I will leave a video link here where I’m calculating factorial of a number using loops and then what I want you to do is to do that same task using recursive functions and you can paste your code in the comments down below so I hope that this video helped you understand what is recursion and how it works and if it did give it a thumbs up and subscribe to my channel and also you can follow me on my other social media if you want the links are in the description thank you very much for watching I’m going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and give this video a thumbs up as well now in this particular video I want to talk about objectoriented programming so what is objectoriented programming well oop or object-oriented programming allows you to represent real life objects in programming and those real life objects are going to be represented together with their attributes and then their behaviors as well one of the most important concepts of oop are classes and objects so let me explain difference what is a class and then what is an object a class is going to represent a template meaning a blueprint and then an object really represents an example of that class an instance of that class for example a class can be a fruit and then an object of that fruit class can be an apple a banana a peach or another example a class can be a car and then an object of that class can be a Volvo a Ford a BMW and so on and that car class for example has some attributes and then it has some behaviors as well now what are attributes of car class well a car has a name for example that’s an example of a one attribute and then it has a price and Max Speed and color and so on and then behaviors of a car can be drive for example and then break a as well and then change color and so on so here we are going to create an example of a class and then we are going to create an object of that class so the class that I want to create is going to be called YouTube channel so in order to create a class you write class and then you give a name to your class so here I’m going to say YouTube channel like this and then you put these curly brackets and at the end you will have to put a semic column and now here you will put the members of this class now one thing that I haven’t mentioned is that class is user defined data type we have already talked about data types in this course or in C++ for beginners course so if you haven’t watched that make sure to do so I’m going to link it in the description and then here as well so we have talked about basic data types such as integer floating Point number and then Boolean character double string you know but a class is going to be a user defined data type which is going to allow you to represent a real life object in programming and in this particular situation I want to represent a YouTube channel so let’s think of attributes of a YouTube channel what is something that describes a YouTube channel let’s say for example that each YouTube channel has a name so so that is going to be a string variable let’s say string name like this so these attributes are going to be represented with variables so one of the attributes is going to be name and then let’s say another is going to be string owner owner name like this and then let’s think of another attribute let’s say that each YouTube channel has a subscribers count so in subscribers count like this and then let’s as well create a list of published videos so I’m going to say list and let’s call it published video titles so that is going to be type string and it is going to be called published like this now here it says that that this list cannot be used because we haven’t included um it here so here I’m going to say include list and now this should be accessible okay now we don’t have that error anymore and what I want to do here is I want to represent a YouTube channel that has four attributes it has a name then owner name you can use email here or whatever and then it has subscribers count and published video titles now here you can use another class here you can say for example list of videos you can create another class that is called video and then in that video class you can put all the attributes that describe a video so that can be for example a title and then a description number of likes number of comments number of views um the duration of that video the video itself you know but for the Simplicity of this particular exam example I’m going to use just the title of those videos so now after I have created this YouTube channel class what I can do is I can create an object of that class so let’s say here that I’m going to create an object so how you do that you say the type so you put the name of your class which is YouTube channel and then you give a name to that variable let’s call it YouTube channel like this okay now what I want to do is I want to assign values to these properties of my YouTube channel and how do I do that well you say YouTube channel and then you put dot and here is nothing available but it should be these properties should be available when you say YouTube channel dot now why these are not available it is because all the members of your class are by default private and that means that these members because they are private they are going to be accessible only within this class here and in order to make them public in order to make them accessible outside of this class you put Public Access modifier so you say here public okay like this now this public is an access modifier and it is going to make all of these members of your class accessible outside of your class which means that now if I press dot here as you you can see name owner name published video titles and then subscribers count is available for me now so I’m going to say that the name of my YouTube channel is going to be for example code Beauty like this and then owner name let’s copy this a couple more times so owner name is going to be my name so Salina like this and then subscribers count well let’s say this is an integer variable so we don’t need the quotation marks let’s say 1,800 uh people and then these published video titles so let’s add value to my published video titles list so here I want to say that I have published for example um C++ for beginners oh let’s say video one okay and then let’s say HTML and CSS video one okay and let’s add as well this video so let’s say C++ o o video one okay now after I have assigned value to these properties what I can do is I can write out these properties so I can say here for example see out and then let’s say name and let’s write out the name of my YouTube channel like this and I’m going to add an end line and I’m going to copy this couple more times like this so here I want to write out owner name so let’s put YouTube channel. owner name and then here I want to write out a subscribers count so YouTube channel do subscribers count and then published video titles now this property here published video titles is not a simple property meaning this property here is a list and in order to write out a list we are going to use a loop so I’m going to delete this and then here I want to create a loop and let’s say that we will be using for each Loop so I’m going to say four like this and then here I’m going to write out the type of the value that I’m going to use in my list so I’m going to say string because this published video title is of type string and then here I want to give it a name so I’m going to say video title like this and then I’m going to specify the list that I want to iterate and that list is going to be this one here so it is going to be a list of published videos in my YouTube channel object okay and now in these curly brackets I am going to specify the block of code that is going to be executed in each iteration so what I want to do with this video title is I want to write it out so I’m going to say see out and then let’s write out video title and I’m going to add an end line at the end as well and one more thing that I want to write out before this list is going to be just see out videos like this okay and let’s add end line at the end so that it is formatted nicely so now if I run this program as you can see it has written out information about this object that we have created here it says that the name of YouTube channel is code Beauty and then owner name is salinaa subscribers count is equal to 1800 and then the this channel has published three videos so C++ for beginners and then HTML and CSS video one and then C++ objectoriented programming video one okay so uh I have created a class that is called YouTube channel I have uh created four attributes so four properties and made them public which means that these four properties are going to be accessible outside of this class and we have accessed those properties here so we have said uh that we want to assign value to our name property and then owner name sub subscribers count and then we have as well um assigned value to this published video titles list okay and then here we have written out information that is stored inside these properties so that was an example of how you can create a class and then how you can create an object of that class as well and in my next video we are going to be talking about Constructors and class methods because there is a more simple way to do this that we have done here so make sure to check that video out as well so I hope that you enjoyed this video if you did give it a thumbs up and also please subscribe to my channel and click that Bell icon and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos on this channel so if that is something that is of interest to you consider subscribing and give this video a thumbs up as well and in this particular video I want to talk about Constructors and class methods so here I have some code that we have written in a previous tutorial of this course so if you haven’t watched that make sure to check it out I’m going to link it here and in the description as well and let’s explain very quickly this code that we have here so I have created a class called YouTube channel and that class has four public properties so four attribut attributes those are name owner name subscribers count and then a list of published video titles and then here I have created an object of that class I have assigned the value to the to these properties here okay and then here I have written out information about this object that we created and as I said said in this video I want to talk about Constructors and class methods and how am I going to explain these well let me first introduce a problem to this approach here so that you can really understand the need for these two so what happens if I want to create another object of this YouTube channel class how am I going to do that well in this current situation what I would do is I would copy this so I’m going to copy it and then I’m going to paste it here like this and let’s give it a name let’s say that this channel is going to be YouTube channel to and let’s call it let’s say YouTube channel 2. name let’s say that it is going to be called Amy SS for example and um owner is going to be a girl called Amy and then she is going to have let’s say 2,000 subscribers okay and and then the videos that she has published are going to be songs obviously because she has a channel named Amy sings so let’s say that she has published for example um let’s say Johnny B cover like this so that’s one song that’s one video and then let’s say that another video that she has published is going to be for example um Laura Li so that’s a song from scorpions and she has made a cover of that song like this now these two are two of my favorite songs and you can put whatever you want here in this list and I’m also wondering and you can write that in a comments down below what are your favorite songs so please write that in the comments just don’t put links because I think that YouTube has this policy of treating um links as U advertising or spam or something so just write out the name name of your song and then uh the name of a singer I guess and I’m really looking forward to reading your comments so now that I have created this second YouTube channel what I want to do is I want to write out information about this channel but I noticed that I’m missing here uh this YouTube channel to so here I’m going to say YouTube channel 2. ownername and then YouTube channel 2. subscribers count and then YouTube channel to. published video titles as well okay now if I wanted to write out information about this second Channel what I would do is I would copy this so I’m going to copy it and then paste it here and here I will say that the name is Youtube channel 2. name and then owner name is YouTube channel 2. ownername and then here as well and then YouTube channel 2. published video titles okay now if I run this program we should get information about these two channels so the first channel is this one that we already had it’s code Beauty owner is me and then this is subscribers counter and then these are three videos that I published so far I mean I published more than three videos uh but this is just an example and then here is our second channel it’s called Amy sings and then owner is Amy she has 2,000 subscribers and then she has published two covers now this here has a problem if we wanted to create a third a fourth a fifth object we would have to copy this code and then each time that we want to write out information about those channels we would have to copy this code and this really collides with a principle called don’t repeat yourself meaning do not repeat your code if you don’t really have to repeat it and in this particular situation we do not have to repeat it so how we are going to solve this problem well the first thing that I want to talk about are going to be Constructors and what a Constructor is it is a special method that is going to be invoked each time that you create an object of a specific class meaning that method is going to be called on the construction of that object so there are two rules when it comes to Constructors and the first rule is that Constructor has the same name as your class and then the second rule is that Constructor does not have a return type so let’s create a Constructor for this YouTube channel class here I’m going to say YouTube channel like this and then I’m going to put these parentheses and I’m going to put these curly brackets as well now what I want to do in this Constructor is I want to receive two parameters I want to receive two arguments and those are going to be string name and then string string um let’s let’s say owner name okay now these are two parameters that I want to pass to my Constructor and what I’m going to do with these in my Constructor is I’m going to assign these to these properties here so I’m going to say that name is going to be equal to whatever my user has passed to this Constructor so name is equal to this name here and then owner name in my class is going to be equal to this owner name that I have received in my Constructor now since since this Constructor here is invoked each time that I create object each time that it is constructed that means that at the beginning when a channel is created it has zero subscribers so I’m going to put here zero like this we do not even have to pass this value in our Constructor because when you create your YouTube channel you do not have subscribers okay now what am I going to do here is I do not need to write this code like this anymore so I’m going to delete I’m going to actually comment this code here and as you can see this is underlined it says that no default Constructor exists for class YouTube channel now what we have to do is we have to use this Constructor here that I have just created and how you do that well you put parentheses here and then inside these parentheses we are going to pass these two parameters so the first parameter is going to be the name so that is the name of our Channel and that is this name here so code Beauty like this and then a second parameter as you can see here is owner name and that was this value here so I’m going to pass that here like this and now after I have created this YouTube channel we do not need this code um anymore so I’m going to delete it okay and what we can do is we can do the same with this second channel so with this YouTube channel too so I’m going to really comment this code for a moment and then here I want to invoke a Constructor for this YouTube channel class and pass it as first value the name because we are receiving name as our first value in our Constructor like this and then then the second value that we need to pass is going to be owner name so I’m going to copy that as well and now we have successfully created this second YouTube channel okay so now I can delete this code here as well okay and now if I run my program as you can see it has written out information about my two channels the first one is code Beauty owner is me and then I have zero subscribers because I have just created my channel and we have assigned that here and then our second channel is Amy sings uh owner is Amy and she has zero subscribers as well and as you can see both of these videos published videos lists are empty because we haven’t uh really added any videos okay so how do you do that I’m going to delete this from here and then move it here like this so now we have both of our objects being created at the beginning here so what I want to do now is I want to add a couple of videos to this channel here so how do you do that well you say YouTube channel like this and then I’m going to put dot and here in this published video titles list I’m going to invoke a method push back which is going to add at the end of this list another element and that element is going to be of type string because this publish video titles is a list of strings so here I’m going to add a string and I’m going to call that video C++ for beginners like this so that is one video and then let’s add a couple more videos let’s say uh HTML and CSS for beginners and then let’s say um o op for beginners Well no let’s say C+ plus o o like this okay so we have added three videos for this first channel and then this second channel does not have videos yet so if I run my program again you can see that this first channel has three videos and then this check this second channel does not have any videos yet because we have deleted those that we previously had okay now that was the example on how you can create a constru structor and how you can use a Constructor so this here is the Constructor of this YouTube channel class and we have passed two parameters two arguments to this Constructor and then here in our Constructor we have really assigned initial values to these properties here that we have in our YouTube channel and then this here is the way that you use your Constructor so you put these parentheses and then you pass the values that your Constructor has to receive and here we have created two objects and we have done that without repeating code now what is going to happen with this here so this here and then this here is repeating code as well and in order to solve this particular Pro problem we are going to use class methods so class method is going to describe a behavior of a class and since here we are writing out uh the these attributes of our class since we are writing out information about our YouTube channels what I want to do is I want to create a method that is going to be called get info for example so that method is going to be of type void let’s call it get info like this and what I want to do in this particular method is I want to copy this code here so I’m going to cut it and then paste it here and in this particular method in this get info I want to say please write out name and then the name property we don’t need this YouTube channel object anymore because we are in the class itself okay and then here as well I’m going to delete this here and then here and now this code should work so whenever you invoke this get info method on your object it should write out all of these properties of of that object so now I can remove this code here as well so this um information about our second Channel and how am I going to get how am I going to invoke this method well I say the name of my object so for example YouTube channel Dot and then as you can see I have this get info method available here because it is public so I say get info and I invoke it like I would invoke any other function and let’s do the same for our second YouTube channel let’s say YouTube channel to get info like this and now if I run my program as you can see it has written out information about my two channels so code Beauty Sal owner and then subscribers count my videos and then info about my second Channel as well so I hope that you understood the concept behind behind a Constructor of a class and then behind a class method here we have demonstrated how you can create an object using a Constructor and then here as well so you don’t really have to repeat your code each time that you create an object and then here we have invoked a method of these objects which we have implemented here so you do not have to repeat that code as well each time that you need to get information about your object you just invoke this get info method and now our main function looks much cleaner and all the implementations of the construction of our objects and then this get info method are in this YouTube channel class so I hope that you enjoyed this video if you did give it a thumbs up and also subscribe to my channel don’t forget to hit that Bell icon and I’m going to see you in my next video bye hi everyone my name is Salina and I make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and give this video a thumbs up as well and in this particular video I want to talk about encapsulation here I have code that we have written in the previous videos of this course so if you haven’t watched those make sure to check them out I’m going to link them in the description of this video and I’m going to go over this code very quickly so that you can understand what we have written here so here I have created this YouTube channel class which has four public attributes and those are name owner name subscribers count and then list of published video titles and then here we have a YouTube channel Constructor and then this get info method as well that just writes out information about our YouTube channel and here’s the code of our Constructor okay and then in our main function we have created an object of this YouTube channel class and we have assigned it these two values for our name and our owner name and then we have as well added three videos to this published videos list and here we have written out information about our channel so if I run this code now as you can see this is how it behaves it writes out information about our YouTube channel now now what I want to talk about in this video is encapsulation and what does this principle of encapsulation says well encapsulation says that these properties here should not be public these should be private and then the way to change the value the data that you store inside these properties should be really using methods that you Expose and then you give access to those methods to your user and then using those methods obeying the rules of those methods your user can change the value of these properties okay so let me introduce a problem here so that you can really understand why we need en encapsulation so what happens if I say for example that YouTube channel so this that we have created has let’s say a million subscribers okay this is a million now we have assigned a million subscribers to this YouTube Channel and this should not be allowed if I run this program you can see that this YouTube channel has a million subscribers now but this is not the natural way to gain subscribers for your YouTube channel what you should do instead is you should give your user the ability to invoke subscribe and unsubscribe methods and then using those methods you are going to increase or decrease this subscribers counter so let’s do that let’s first hide these properties from our user so from whoever is going to use this class here so how do you hide these well you make them private so here instead of using public access modifier I’m going to use private access modifier so I’m going to say private oh private like this and then these should stay public so I’m going to put public here like this okay and then as you can see we have a problem here it says that this subscribers count is inaccessible and then this publ this published video titles as well is inaccessible meaning that we cannot access these private members of our class these private members can be accessed only within this class itself so how we are going to give our user the ability to change this subscribers counter well as I said we are going to create methods so let’s create two methods the first one is going to be void void subscribe like this and then what this method should do it should increase this subscribers counter so here I’m going to say subscribers counter like this plus plus using this increment operator we are going to increase the value that we store in this variable by one and then we are going to have as well unsubscribed method so I’m going to copy this paste it here and let’s say unsubscribe and then here I’m going to say that this subscribers count is going to be decremented so now if I I will have to comment this code here and then I’m going to delete this code here and now if I want to increase or decrease subscribers for my Channel what I will have to do is I will have to invoke one of these two methods so let’s say for example YouTube channel do subscribe like this okay and let’s invoke this method um three times for example and now if I run this program as you can see my channel so code Beauty channel has three subscribers and then let’s say for example that I haven’t published videos in a while so people decide to unsubscribe so I’m going to invoke this unsubscribe method like this and if I invoke this get info after that I should have two subscribers well oh as you can see I have two subscribers now because three people have subscribed and then one person has unsubscribed from my channel okay so we have this code here as well that was underlined meaning that it has um compile time error and it says member published video titles is inaccessible meaning that now we cannot access this published video titles list anymore because it is private now so I’m going to expose a method that is going to publish videos for me so here I want to create a method of type void which is going to be called publish video publish video like this and then here I want to add a video to my published video titles like this so I’m going to say published video titles push back and then what I want to do is I want to pass a value here which value well we are going to receive that value as a parameter in our function so here I’m going to add an argument a parameter and that is going to be of type string and let’s call it title like this and now we are going to use this title here so we are going to push this title in our push in our published videos titles okay like this now if I want to invoke this method I’m going to invoke it like this so I’m going to say YouTube channel dot publish video like this and then I’m going to do the same thing here and then here and here as well okay so now we have created these methods that we invoke and then using these methods we change these properties and these properties should be private meaning these should be encapsulated okay and now as you can see we have a channel called code Beauty and then owner is myself and subscribers is two and then these are videos that I have published so far now if you look at this unsubscribe method you will notice that it has a bug what is that bug well let’s delete these three lines of code let’s delete these subscribe method invocations and if I run my program now what is going to happen as you can see it says that my subscribers count is equal to minus one and that does not really make sense so you cannot have minus one subscriber and we are going to fix that problem if we put here inside this unsubscribed method a check so we want to check if our subscribers count is greater than zero and only in that situation if that subscribers count is greater than Z zero only then we can decrease this subscribers count value so if I run my program now as you can see we have invoked this unsub subscribe method but we still have zero subscribers and now we have fixed that bug that we had because if no one has subscribe to our Channel then no one can unsubscribe from it okay so that was a bug that I wanted to fix and then what you can do here because here we have exposed these three methods that really manipulate the values that are stored inside these properties and then these two properties there is no way to change them outside of this class so what you can do for dam for example is you can create a getter and a Setter meaning a method that that is called for example get name and that method is going to return you the value that is stored inside this name variable and then a set method meaning set name method that is going to receive um parameter which is going to be called name for example and inside that set name method you are just going to change the value that is stored inside this name property and then you can do the same for this owner name uh as well so you can create a get and a set method so let’s go over this encapsulation rule one more time so encapsulation rule says that all of these properties should be private meaning they shouldn’t be accessible outside of this class and you do that by making them private so you put this private access modifier here and then how do you change the values that you store inside your properties well you expose a public methods that are going to change the data that you are storing inside your properties and here we have created these three public methods that are going to change the data stored inside our subscribers count uh property and then inside our published video list property here so you can for these two properties again you can create getter and Setter method and if some of you do that please put that code in the comment section and I’m going to review it so if you enjoyed this video give it a thumbs up and don’t forget to subscribe to my channel and hit that Bell icon as well and I’m going to see you in my next video bye hi everyone my name is Salina and I make it and programming related videos on my channel so if that is something that is of interest to you consider subscribing and give this video a thumbs up as well and in this particular video I want to talk about inheritance so here I have my visual studio and here I have some code that we have written in the previous tutorials of this course so if you haven’t watched those make sure to watch them and now I’m going to go very quickly over the code that we have here so I have created a class that is called YouTube channel and that class has four private properties name owner name subscribers count and then published video titles and then we have a couple of public methods as well we have a Constructor which just assigns initializes these variables here and then we have get info method that just writes out information values that are stored inside these properties here and then as well we have subscribed and unsubscribe methods our subscribe method increases our subscribers counter and then our unsubscribe method decreases that subscribers counter but first we have to check if that subscribers count is greater than zero so those would be subscribe and unsubscribe methods and then we have this publish video method as well that just adds a new item adds a new title to this published videos list okay now what is going to happen if I want to create another type of YouTube channel a specific type of YouTube channel which is going to be for example um cooking YouTube channel so I should copy these properties right well that can be one approach but the quicker and better approach is going to be to inherit this YouTube channel class so let’s do that let’s create another type of channel so let’s create another class let’s say class and then let’s call it cooking YouTube channel like this okay now I have created a class that is called cooking YouTube channel and in order to inherit this YouTube channel class what I’m going to do is I’m going to put this column sign and then I’m going to say public you YouTube channel like this and in this way my cooking YouTube channel is going to inherit my YouTube channel and that means that my cooking YouTube channel is going to have everything that my YouTube channel has now this public access modifier means that whatever is public in this class here it is going to be public here as well so in this derived class and one very important thing is is to know that this class is called Base Class so the one that you inherit from that is base class and then the derived class is going to be this one here so the one that inherits from the base class is called derived class and in this particular situation our cooking YouTube channel is derived class now what is going to happen if I want to create for example an object of this cooking YouTube channel so I’m going to say cooking YouTube channel and let’s call it YouTube channel like this okay now as you can see it says that default Constructor of cooking YouTube channel cannot be referenced now that is because we do not have a Constructor for this class here so let’s create one let’s create a Constructor so I’m going to copy this name and then add these parentheses okay and now if I hover over this it says that the Constructor is inaccessible so we still have an error but we have a different error which means that we are making progress so because this Constructor is inaccessible I will have to make it public to make it accessible outside of this class here so I’m going to say public okay and now my uh Constructor is going to be accessible but there is another thing that we have to do and that is going to be inside this Constructor we have to receive properties that we are going to initialize in our Constructor and those are going to be these two properties name and owner name so here I’m going to say that my Constructor receives two properties so string name and then string owner name so the owner of that channel and what I’m going to do when I receive these two properties I’m not going to initialize them here in my Constructor because I’m inheriting from a class that already knows how to construct how to initialize these two particular properties so I’m going to call the Constructor of that class so I’m going to call the Constructor of this YouTube channel class so here I’m going to put this column sign and then I’m going to invoke its Constructor so I’m going to say YouTube channel and then I will pass these two parameters here so I’m going to say name and then owner name like this and now our Base Class our YouTube channel class is going to construct to initialize actually the value of these two properties and as you can see here we have an error that asks us to pass these two values to our Constructor so we are going to pass two parameters the first one is going to be the name of our channel so let’s say that that is going to be for example Amy’s kitchen like this and then owner is going to be um girl named Amy let’s put that here as well okay now after I have created an object of this cooking YouTube channel what I should be able to do is I should be able to access all of these public methods that my base class has so all of these methods from my YouTube channel class should be available for my uh cooking YouTube channel so here I’m going to say YouTube channel Dot and then as you can see these are available so get info publish video subscribe unsubscribe let’s write out info about my channel like this and if I run it now well you you can see that we have Amy’s Amy’s kitchen Channel owner is Amy it has zero subscribers and it has no videos as well so let’s publish a couple of videos for Amy Amy’s kitchen channel so here I’m going to say okay before I this get info function I’m going to say YouTube channel Dot and then let’s say publish video of for example apple pie like this this and then let’s publish another video let’s say for example chocolate cake like this and uh now if I run this program as you can see we have oh we have two videos and let’s as well say that people like these videos so they decide to subscribe to Amy’s kitchen channel so I’m going to say YouTube channel do subscribe so uh for example two people decide to subscribe to her Channel and if I run my program again you can see that now we have two subscribers as well so we have been able to invoke all of these methods that are implemented in our Base Class so here from the object of our derived class so from the object of this derived class class here now this derived class this cooking YouTube channel can have its own members meaning things that are specific for this class only so let’s say for example that we want to create a method that is going to be called uh for example practice so I’m going to say void practice like this and then what this method should do it should just make our user that has this cooking YouTube channel it should say that this user is practicing cooking and then learning new recipes and things like that so I’m going to say see out like this and that let me copy this part of the code because I don’t want to make a typo like this so it says that uh our YouTube Channel’s user is practicing cooking learning new recipes and then experimenting with spices cuz I guess that’s something that you should do if you have a cooking YouTube channel Channel and let me add endline here as well like this so now if I decide to invoke this method this method is going to be available only for objects of this cooking YouTube channel so I’m going to say YouTube channel like this and then let’s say practice like this now um let me very quickly change the name of this YouTube channel object because I want to create another object that is going to be of uh this YouTube channnel type and I want to call that YouTube channel so here I’m going to say um cooking YouTube channel and I’m going to paste that name here and then here and here here as well okay now we have named this object cooking YouTube channel and if I run this program you can see that at the end it says practicing cooking learning new recipes experimenting with spices is now as I already said this practice method should be available only for the objects of this cooking YouTube channel class so that means if I decide to create an object of this class here this Base Class that class is not going to have this practice method and let’s prove that very quickly so here I’m going to create an object of YouTube channel Class Type and let’s call that object YouTube channel that’s why I changed the name name of this object like this and let’s say that that is going to be my channel for example um code Beauty like this and then let’s say that owner is my name Salina so now if I try to access this practice method on my YouTube channel so I press dot you can see that that method is not available for this Base Class it is available only for this cooking YouTube channel class because this practice is very specific method meaning um this code Beauty Channel which is oriented to programming and it does not really benefit from learning uh recipes and experimenting with spices practicing cooking and things like that so this channel should probably have some other way of practicing uh because it is different type of channel so I’m going to delete this code here okay and let’s repeat this one more time so we have created a derived class which is called cooking YouTube channel and we have inherited everything that our YouTube channel class has by saying public and then the name of our Base Class and this public access modifier is going to allow us to have whatever is public here to have that public here as well and then we have created a Constructor for our cooking YouTube channel and in that Constructor we receive two parameters name and owner name and since our Base Class Constructor knows how to initialize those two we have here invoked this Base Class Constructor and then here we have created as well a method that is called practice and that method just says that our user is practicing cooking and he is learning new recipes and experimenting with spices now there’s one more thing that I want to show you and that is going to be to Let’s create another object of this cooking YouTube channel and that is going to be uh let’s say cooking YouTube channel to and let’s call it um John’s kitchen like this and owner is going to be for example John now what is going to happen if I say for example that this second YouTube channel so this cooking YouTube channel to John’s YouTube channel is practicing I’m going to um comment these two because I don’t need them and if I run my program now you can see that it says two times practicing cooking learning new recipes experimenting with spices and then the same line here so you cannot really differentiate between which which one is Amy’s Channel and which one is John’s channel so in order to make that a bit more understandable I’m going to add a property here so here I want to add the name of the owner of this channel so here I’m going to say let me copy this property like this so owner name and then let’s say that that owner is practicing cooking learning new recipes experimenting with spices and so on but as you can see this this property here is not accessible it says that member owner name is inaccessible and why is that the case why is it inaccessible because it is private and if you remember private members can be accessed only within that class and then public members can be accessed from outside of that class but what happens if you want to have for example a certain property to be accessible in in the derived class now there is an access modifier for that as well so that is going to be protected access modifier so I’m going to say here protected like this and then I’m going to move this owner name to my protected area like this so now this owner name property should be accessible in derived classes as well and as you can see the error has disappeared so now if I run my program you can see that it says that Amy is practicing cooking learning new recipes and experimenting with spices and then this second line here says that John is practicing cooking and learning new recipes and experimenting with spices so now we have made a difference we have specify the name of our owner of the channel and we can understand what the code is doing um a bit more I hope that you understood what is inheritance and how it works in C++ and if you did please like this video don’t forget to subscribe to my channel and click that Bell icon as well and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and give this video a thumbs up as well and in this particular video I want to talk about polymorphism now the first thing that I want to do is I want to explain what is polymorphism polymorphism describes the ability of an object for example to have multiple forms now that means that you can have two or more objects that inherit from the same Base Class those objects can have the method that has the same name but different implementation meaning different behavior and I’m going to demonstrate that on an example now here I have some code that we have written in my previous courses so if you haven’t watched those make sure to do so the links to those videos are going to be in the description and I’m very quickly going to go over this code so here I have created a class that is called YouTube channel that class has three private prop properties name subscribers count and then published video titles and then it has one protected property which is owner name now um this private properties these are available only in this class and then these protected properties these are available in derived classes as well and then here we have public methods as well and this here is our Constructor so it just initializes values of um these properties I’m going to collapse death and then here we have get info method with which just writes out information about our YouTube channel and then these subscribe and unsubscribe methods these just increase or decrease the the number of our subscribers let’s collapse that and then publish video method it just um adds another video to this published video titles list so that would be our YouTube channel base class and then here we have cooking YouTube channel which is a specific type of YouTube channel that inherits from this Base Class here and that cooking YouTube channel has its own Constructor which is this one here and here we have just invoked the Constructor of our base class and then here we have implemented a method which is called practice and it says that when you practice and you have a cooking YouTube channel you will have to be practicing uh cooking and then learning new recipes and experimenting with spices now what I want to do is I want to create another derived class so I’m going to copy this okay and then this derived class should be called for example what let’s paste the name here so singer YouTube channel and I’m going to paste the name uh here as well on my Constructor so this is going to be a YouTube channel about singing now as you can see this singer’s YouTube channel has this practice method as well but this implementation is not right for my singer YouTube channel because if you’re a singer you are not going to benefit really from practicing cooking and then from learning re PES and experimenting with spices but what you should do instead is you should practice something else so I’m going to copy that so that I don’t make a typo like this so someone who is trying to make a singer’s YouTube channel is going to take singing classes and then learning new songs and then he should as well be learning how to dance because I believe that singers should know how to dance as well okay so now we have implemented this singer YouTube channel and as you can see as I already demonstrated it has this same method which is practice but this method has different implementation than this one and as you can see here this is the implementation of our practice method on our Cooking Channel and then this here is the implementation on our singer YouTube channel so what I want to do now is is I want to create this singer’s YouTube channel here we already have cooking YouTube channel so it is a video it is a code from one of my previous videos and then what I want to do is I want to create um singer YouTube channel so let’s call it um Singers YouTube channel and let’s say that it is going to be called for example John sings like this and then owner is going to be a guy named John like this okay now after I have created this cooking YouTube channel and then this singer YouTube channel you can probably call it singing YouTube channel or something like that uh this is the best name that I could come up with okay so now what I want to do is I want to invoke methods on these two classes and the methods that I want to invoke are going to be these two so practice and then this practice method as well so here I’m going to say cooking YouTube channel do practice like this and then let’s invoke the same method for my singer YouTube channel like this and now if I run my program as you can see it says that Amy is practicing cooking and then John is taking singing classes and learning new songs and learning how to dance I can imagine that okay now after I have invoked these two methods you can really see oh I have stopped my program I’m sorry so here you can see that this cooking YouTube channel has invoked its own method so this cooking YouTube channel has invoked the method that has been implemented in this cooking YouTube channel class which is this one here and then this invocation so this singer YouTube channel when you invoke practice method on that this method here is going to be invoked so this method that is implemented in singer YouTube channel class now let’s add another thing and that is going to be here in this practice method and then in this practice method as well what I want to do is I want to say that each time that our user practices cooking and then practices singing what is going to happen is the quality of that YouTube channel is going to increase so in order to track that quality content rating I’m going to create a property and that is going to be here in this YouTube channel in this Base Class so I’m going to make that property uh protected protected because I want to have it accessible in my cooking YouTube channel and then in my singer YouTube channel as well so I want that property to I want to access that property from my derived classes and because of that I’m going to make it protected so here I’m going to say int and let’s call it it content quality like this okay and initially I want to set the value of this content quality variable in this Constructor here I want to set it to zero like this because I want to say that when you create your YouTube channel this Constructor here is going to be invoked and you are going to have zero subscribers and you are going to have a zero knowledge about content quality so your rate content quality rate is going to be zero so let’s close that and then what I want to do here in this practice method is I want to say each time that you practice your content quality should increase so let’s increase that variable content where is it content quality Plus+ so I’m going to increment that content quality and then I’m going to do the same here like this now let’s say for example that my um this singer here John he’s going to practice a lot so he has practiced five times more than Amy did so if I run my program now you can see that John is practicing and then again and again and again so he is practicing a lot and Amy is not really so what do I want to do with this content Quality Property well let’s say that I want to implement another method and that method is going to be as well implemented here in this Base Class and that method is going to be called uh check analytics because that check analytics method should be available for all YouTube channels so that is not something that is specific for this cooking YouTube channel only or for this singing YouTube channel so singer YouTube channel but that method should be available for all YouTube channels and because of that I’m going to implement it in this Base Class so let’s say void check analytics like this okay and now what I want to do in this check analytics method is I want to use this property this content Quality Property in a following way so I want to say if content quality is less than five I want to write out a message to my user so I want to say see out and then let’s write out the name of this YouTube channel like this and then let’s add um a message which is going to say let me copy that okay this is going to say that this channel has a bad quality content because this content quality has a rating that’s that is less than five and let’s add in line and then what is going to happen if this content quality is not uh less than five so if it is equal to five or greater than five I’m going to put else block here and what I want to say here okay is that this channel has a good content great content actually okay like this now what am I going to do with this check analytics method well I’m going to try to invoke it here but the way that I want to try to invoke this um this method that we just implemented is going to be in a following way so there is one very interesting thing when it comes to polymorphism and that thing is related to pointers um at this point I haven’t made videos about pointers on my channel but if you’re watching this video sometime in the future sure then those videos are probably available on my channel so make sure to look for them on my channel and then in the description of this video as well so let’s create a pointer and that pointer is going to be a pointer of our Base Class so a pointer of type YouTube channel okay like this and let’s call it YouTube one and what I want to do is I want to assign to this pointer here address of this cooking YouTube channel and you can do that you can assign to a pointer of a base class an address of derived class so I’m going to say here please to this pointer assign address of this cooking YouTube channel class like this and then what I want to do is I want to create another pointer let’s call it YouTube 2 and then to this YouTube 2 pointer I want assign the address of my singer YouTube channel like this so one more time a pointer of Base Class can point to a variable of derived class now what I can do using these pointers is I can invoke method that I have just created which is this one here so check analytics so let’s do that let’s say YouTube channel one and then um let’s invoke this method let’s say check analytics like this and if you just noticed it has changed my DOT to this symbol here because this is used when you want to invoke methods using pointers so I’m going to copy this again for my second YouTube channel like this and now if I run my program as you can see our John has been practicing a lot and then Amy has been practicing only one time so here after I have invoked these two methods this first method says that Amy’s kitchen has bad quality content because she hasn’t been practicing enough and then after this YouTube channel to check analytics you can see that it writes out this message that John sings channel has great content because he has been practicing a lot as you can see here okay so here we have used polymorphism in order to implement this practice method in our cooking YouTube channel and then in our singer YouTube channel so implementation of our singer YouTube channel practice method is here and then for our cooking YouTube channel our practice method looks like this and we have done that uh thanks to the polymorphism so different implement ations but the same method name and then I have demonstrated here how you can use pointers to point to an object of derived class and you can store that in a pointer of Base class and then we have invoked this check analytics methods using these uh pointers of Base class so I hope that you enjoyed this video if you did give it a thumbs up up and don’t forget to subscribe to my channel click the Bell icon as well and I’m going to see you in my next video bye hi everyone welcome to my channel in this video I want to talk about pointers so I will go straight to the point uh pointers are very important Concept in C++ and they have many uses in C++ now pointers are not limited only to C++ they exist in other programming languages as well but in most of those programming languages they are really hidden they work behind the scenes so you cannot see them and you cannot use them yourself but it’s C++ you can use them and you can create your own pointers and they can be pretty powerful if you know how to use them but they can get quite confusing as well if you don’t understand them well so the first thing that I want to explain is what are Pointers so we already have explained the concept of a variable and you should be familiar with that that so we said that a variable is just a container so a container that stores certain value now a pointer is container as well but instead of storing value pointer stores an address so a memory location and let me show you how pointers work in Visual Studio let’s create a variable of type int and I’m going to call it n and assign it a value of five and let’s write out this n like this so nothing new here we are already familiar with this and if I run my program as you can see we get this value of five so that was expected now as I already said this N is a variable and that means that it is a container which is storing certain value now because it is a container that means that it has its address inside memory so it has its physical location so how can we get that location how how can we check which address this n has so in order to do that you use this ENT symbol like this so you put it before your variable name and that should give you the address of that variable so if I run my program now as you can see now we have an address um and this is the address of our n variable so this is its physical address where this value of five is stored and because this is pretty hard for humans to remember this is just a random numbers and characters because of that we use meaningful names and we access these values that we store in our memory using these meaningful names which are our variables so I’m going to close this now and what I want to show you here is I want to show you how can you create a pointer that is going to hold this address of our r n variable so in order to create a pointer you give it a type first so you say int and then in order to indicate that you are creating a pointer you use this star symbol and then you give it a name so let’s call it PTR pointer and I’m going to assign it the address of our n variable so I’m going to copy this and paste it here okay now our pointer is holding the address of our n variable and in order to prove that I’m going to write out the value of my pointer like this so as expected we have this first line here which is writing out the address of our n variable and then this second line of code is this line here and as you can see we have written out the value of our pointer and that is the same address as this one here here which means that our pointer really is storing the address of our n variable now you may ask how can I access or can I access the value that is stored on that address using pointers and the answer to that question is yes you can and in order to do that in order to access the value that is stored on this address that your pointer is holding you have to dreference your pointer so how do you do that well let me copy this so as you already have seen this line of code here is going to write out the address and in order to write out the value on that address you dreference the pointer and in order to do that you add this star symbol before your Pointer’s name so if I run my program now as you can see in this last line of code we have the value of five which is actually the value that is stored on this address here so let me show you one more thing what I want to do is I want to change the value that is stored on this PTR address so on this address that our PTR pointer is storing so how am I going to do that I’m going to say again star symbol and then PTR and this here means please access this memory location so whatever I’m going to assign here is going to be stored on that memory location and what I want to assign is for example value of 10 like this so now if I copy this line of code and then paste it here and if I run my program again we have this value of 10 stored on this address here and one interesting thing as well is that if we try to write out the value that our n holds now like this if I run my program as you can see our n holds the value of 10 as well even though uh we have never said that n has the value of 10 but we have stored the address of our n in our pointer and then we have D referenced our pointer meaning we have changed the value on that address here in this 11th line of code here so that’s why our n holds value of 10 now one very important thing to keep in mind is that your pointer has to be of the same type like the variable that it is pointing to so let’s close this so as I said this integer pointer is pointing to an integer variable so our n is of int type and the same way would be if we created a float pointer it would have to point to a float variable and then chart pointer can point only to a Char variable double bull um and so on so pointer and the variable that that pointer is pointing to have to be of the same type okay so that means that if I try to change the type of this n variable to float for example like this so if I say float immediately we get an error here and the error says a value of type float pointer cannot be used to initialize an entity of type end pointer meaning you cannot assign address of a float type to pointer of int type okay so that is the error that we have here and I’m I’m going to return this to int okay and as you can see that error has disappeared because now our pointer is pointing to the same type so to the variable of the same type that that pointer has so int pointer pointing to an INT variable one thing that beginners very often try to do is the following so they say okay let’s create a pointer so int let’s give it a name ptr2 so pointer like this so I have created a pointer right so let’s dreference this pointer and assign it a value so I’m going to say please D reference PTR 2 and assign it a value of 7 for example now this code here has a problem problem and if I run it if I say build solution actually as you can see it says uninitialized local variable ptr2 used this means that this ptr2 so this pointer to does not have an address so where should it store this value of seven if it does not have an address and the easiest way right now to solve this problem is going to be to create a variable and to give the address of that variable to our ptr2 pointer so I’m going to say int V this variable has to be of the same type as our pointer as we already said and then I’m going to say here our ptr2 is going to hold hold the address of our V variable like this and now we shouldn’t have this problem anymore so if I build my code again as you can see one succeeded so the problem has disappeared and let me write out the value of this V variable so I’m going to say V is equal to and then let’s write out the value of this V and if I run my program as you can see it says that V has the value of seven because we have assigned it here by D referencing our pointer that is holding the address of our V so you may say now okay Salina I understand this but this seems a little bit too much why would I create a pointer just to assign a value to a variable and the answer to that question is you don’t this is not the type of problem for which pointers were created in C++ so this there are different problems that pointers solve in C++ and this here is just a demonstration of what are Pointers and how can you create your own pointers now examples of some of these other problems that pointers solve is you can use pointers in order to pass values by a reference to a function and then you can use them to return multiple values from a function which is an interesting one uh you can use pointers in combination with arrays as well uh you can use them for dynamic memory allocation and then if you’re familiar with oop objectoriented programming you can use a pointer of a base class in order to access a object of derived class and then there is this concept of smart pointers which we are going to cover in the future so I believe as well that I have mentioned some of these things in some of my videos but I plan to make um dedicated series to pointers where we are going to talk only about pointers so stay tuned subscribe to my channel um hit that Bell icon as well so that you are notified when I publish my next video and if you like this one give it a thumbs up thank you for watching and I’m going to see you in my next video where we will be talking about pointers bye hi everyone welcome to my channel in this video I want to talk about void pointers now in the previous video of this playlist I introduced you to pointers in general so I explained what are Pointers and how you can create your own pointers and I said that a pointer is a special variable that can hold an address and I said as well that a pointer can hold only the address of a variable that is of the same type as that pointer which means an integer pointer can hold the address of an integer variable and then a character pointer holds the address of a character variable float pointer of a float variable and so on but there is an exception to that rule and that is a void pointer and a void pointer is a special type of pointer that can hold the address of a variable of any other data type so a void pointer can hold the address of a float variable or an INT variable or a Char bull struct whichever one you pass to that void pointer but a void pointer has a limitation as well and that limitation is that you cannot directly D reference a void point so in order to demonstrate this I’m going to create an example with the knowledge that we already have so using only integer and character and Float pointers that we are already familiar with and then I want to introduce a void pointer in order to solve that problem so that you can see how void pointers are used so let’s jump into our Vis Visual Studio here I want to create a variable um and let’s make it of int data type so I’m going to say int and let’s call it number like this and I’m going to assign it a value of five for example now what I want to do with this number variable is I just want to write it out and because we are working with pointers let’s play with pointers some more so I want to write out this variable using a function that receives a pointer let’s do that so that function is going to be of return type void and let’s call it print number like this and then here this function is going to receive an integer pointer and we are going to call it uh number PTR so number pointer okay now what this function here should do is it it should just print out the value that this number pointer is storing so actually the value that is stored on the address that this number pointer is holding so in order to do that we have to the reference this pointer so I’m going to say star symbol and then put the name of this number pointer and let’s add an end line as well now this function is finished and it is writing out the value that is stored on the address that we passed to it and we have to pass it the address of an integer variable so if I invoke this print number function let’s say print number as I said since it is receiving a pointer we have to pass it an address and that is going to be the address of this number variable so let’s say m perent number so this here is going to give us the address of this number variable okay and now if I run this program as you can see this function here has printed out value of five which is the value of our number okay now what happens if I want to create a k character variable and then print out a character variable as well so let’s do that I’m going to say Char let’s call it letter and let’s assign it a value of letter A okay and now in order to print out this character using this same approach I will have to create another function because this one here receives an integer pointer and in order to print out a character we would have to create a function that receives a character pointer so let’s copy this function here and I’m going to call it print letter like this and then this function is going to receive a character pointer okay and that pointer is going to be called Char PTR and then here I just want to write out the referen value of that Char PTR so let’s invoke this print letter function let’s let’s pass it the address of this letter variable that we have created here and now if I run this program as you can see we have five for this first function and then a for this second function which means that these two functions are doing its job but what happens if I want to print out a floating Point number and then a double and then a Boolean value and a stct value in order to do each one of those I would have to to create a function that receives that specific data type pointer but we can solve that same problem using a void pointer because as we already said a void pointer can point to a variable of any data type so let’s do that let’s create a function that is going to print out any data type so that function is going to be of return type void and let’s give it a name print because it is generic it is going to work with all data types I’m going to give it name of print and as we said that function is going to receive void pointer and let’s call that pointer PTR okay now because this void pointer can point to a variable of any data type we will have to keep track of which data type we have passed inside this void pointer and in order to do that I’m going to use another parameter here and that parameter is going to be of type R and I’m going to call it type so how this type parameter is going to be used well in a situation where we pass an integer pointer to this print function here this type parameter is going to have a value of I in a situation when we pass a character pointer to this void pointer here in this type parameter we are going to pass a value of c and then if we pass a float pointer here here we are going to store an F and then if we pass a double pointer here this type variable should store a letter D so that we can know how we are going to dreference this PTR pointer so the first thing that we have to do is we have to check what this type variable is holding so in order to do that I’m going to use switch case like this you can use if else as well if you want so here I’m going to switch on this type variable like this and what I want to do here is I want to say in the case that this type variable is holding the value of I that means that this here is an integer pointer so here I’m just going to leave a comment for now so I’m going to say handle int pointer like this and then the other situation is going to be in the case that this type variable is holding the value of C that means that here we have passed a character pointer so I’m going to leave another comment as well so here I I’m going to say handle Char pointer okay now let’s explain how we are going to handle this integer pointer and then this character pointer as well well as we already said since a void pointer cannot be dereferenced directly but we first have to cast it into a specific specific data type so into an integer pointer or into a character pointer in this particular situation I’m going to do that so here I’m going to say in the case that our user has passed an integer pointer here I will have to cast this void pointer into an integer pointer first so how do you do that well you say please this pointer here so this PTR cast that into an integer pointer so this expression here is going to cast this void pointer into an integer pointer so in these parentheses here we are going to have an integer pointer and in order to access to the address that an integer pointer is storing and write out the value that is stored on that address we dreference that pointer and for that we use this star symbol so now here we have the value that is stored on this integer pointer and I’m going to write out that value like this and let’s add endline as well so this here writes out the value of an integer pointer so let’s copy that and then paste it here and here what I want to do in this C situation is I want to cast this void pointer into a character pointer so Char pointer and then after I have character pointer here I ID reference that and then this line is going to write out the value of our character okay so now if I invoke this print function I should get the value of this number and then of this letter as well using only this print function so let’s comment these two for now and let’s invoke our print function so I’m going to say print and then the first thing that it receives is going to be DX address and in this particular situation considering that this is a void pointer we can pass it the address of any data type so we can pass it the address of a integer variable or of a character variable or of a floating Point variable and in this situation we have integer variable and then character variable so let’s use these two so let’s pass here the address of our number that oh that is going to be the first parameter and then the second parameter is going to be the type and considering that we have passed the address of an integer let’s pass the value of i as our second parameter here okay and then I’m going to copy this function one more time and this time I want to pass it the address of our letter variable and considering that that is a character here as a second parameter I’m going to pass C now I’ve just not notice that something is missing from our switch case and before I say what it is please write your idea write your answer in the comments down below so what we are missing in these two cases is break at the end so here I’m missing a break like this and then here as well okay so now we have completed our switch case and if I run my program now as you can see for this first line we get the value of five which is our integer so this print function has printed the value of our integer and then in this second line we have printed out the value of a character and we have done that using this print function which receives a void pointer and then it receives a Char uh variable which is called type and we use that Char variable in order to differentiate which data type we are storing inside this void pointer because as I already said a void pointer can point to a variable of any data type but it cannot be de referenced directly so for that we are using this type parameter to check which data type is stored inside this pointer here so now you have an idea on how to use void pointers but I would actually advise you to be very careful when using void pointers why because your compiler in this particular situation does not have a way to tell you hey you are trying to cast a void pointer which is actually an in pointer into a character or vice versa if you’re doing that your compiler in this situation here does not have a way to know if you’re are making an error or not because inside a void pointer you can store the address of any variable so of a variable of any data type and then you can try to cast that void pointer into a pointer of any other data type which means for example in this particular situation here we are passing an integer so this number here is an integer which means that here for this line of code we are receiving an integer pointer okay and then in this case here we are trying to cast that into an integer pointer and that is okay but if we accidentally make a mistake here and we try to cast that into a character pointer for example like this our compiler Now does not have a way to know that we are making a mistake here and if we run our program look at this we get this undefined symbol here which is what is this you know this is not the expected behavior of our program and we did not get a compile time error which means that this here is is completely valid so your compiler is not going to tell you that this here is an error but it actually is a logical error and those type of Errors so logical errors are much harder to find inside programs and this here is a small program and it is kind of easy to find an error in a program that has 20 lines of code but if you had a logical error in a program that has 20,000 L lines of code for example you would have a very very big problem so again I strongly advise you to be very careful when you are using void pointers because as you see compiler is not uh reporting an error for this situation here but but this actually is an error so I’m going to return this to an integer pointer okay so I hope that you enjoyed this video and uh that you learned something new and if you did give it a thumbs up subscribe to my channel and thank you for watching I’m going see you in my next video bye hi everyone welcome to my channel in this video I want to talk about pointers and arrays and if you watched the first video of this playlist you remember that I said that one of the common uses of pointers is using them with arrays so in this video I want to demonstrate how that works so let’s jump straight to our visual studio and let’s create an array of type in and I’m going to call that array lucky numbers like this and let’s say that I have five lucky numbers for example and those are 2 3 5 7 9 oh that’s five numbers already okay so these here are my lucky numbers and now I want to show you one thing so what is going to happen if I say for example see out lucky numbers and let’s add and l so what is going to be written out if I write out just the name of my array let’s check that if I run this program as you can see we get an address but what is this address this address here so the name of our array is actually the address of the first element of that array and in order to prove that let’s write out the address of the first element of this array to check if these two are going to be the same so here I want to write out the address of the first element which is the element with index zero so now if I’m telling the truth these two should be the same so if I run my program as you can see indeed we get the same address which means again the name of the array is the address of the first element of that array now that means that this lucky number’s name behaves as a pointer and these square brackets here are behaving as a operator for D referencing so if I say for example see out lucky numbers of two I’m going to get the element that has index two so this here is going to be the first address and then it is going to add two more addresses to that element and we are going to get the value that is stored there so lucky numers name is the address of the first element it is going to add two more addresses to that so one two so this line of code here is going to give us the value of five let’s add endline and run our program in order to demonstrate that and as you can see indeed we get the value of five now there is another way to do this same thing here and that is going to be the following way so I can say see out please write out this lucky numbers which as we already demonstrated is the address of this first element so I’m going to put that here and then I can use arithmetic operators on this so I can say please add to this lucky numbers two more address spaces and then the reference this so we use star symbol for that and then I’m going to add end line and these two lines of code so this one here and this one here should behave the same so if I run my program now as you can see we get the same value so these two lines of code are doing the same thing which means that this line of code here is using these square brackets in order to the referentie the element with that index which is index 2 and we know that the indexing starts with zero so 0 1 2 and we get the value of five and then this second line of code here uses this star symbol in order to differentiate this expression here and what this expression here says it says lucky numbers which is the address of the first element as we demonstrated here so please use the address of the first element and then add two more address spaces to that so the address of the first element if I add two more to that it’s going to be the address of one two so the address of this one here and then if I dierentiate that I get this value which is stored on that address so these two lines of code are writing out the same thing so let me very quickly show you how you can enter the values for this array and then write them out because here we have hard hardcoded value so I’m going to delete this and um I’m going to comment these two lines of code as well no actually I’m going to comment all of these because we don’t need them anymore but I’m going to leave them for you so that you can um see how these are used in order to deeri um your array so as I said we want to enter values for our array now so in order to do that I’m going to use for Loop so I will say four and then our for Loop starts uh with I equal to Zer because that is the index of our first element and then considering that our array has five elements and the last one has index of four we are going to run our four Loop while I is less than or equal to four and in each iteration we are going to increment the value of our I okay and in order to enter values for our array let’s write out a message so I’m going to say see out number so please enter a number that is going to be the message for our user and then I’m going to input a value from my console into lucky numbers of I so this here means please enter the value into our lucky numbers array to element that has the same index that our I holds in that iteration so in the first iteration index zero in the second iteration index one and then in the third iteration index two and so on so this for Loop here is going to help us to enter the values into our lucky numbers array and in order to show that I’m going to put here a break point and that means that our program is going to stop its execution when it comes to this line here so if I run my program as you can see it asks me to enter number so I’m going to say 2 5 7 9 and 13 so those were five numbers that we had to enter for our lucky numbers array and if I press enter one more time as you can see our program has been stopped in this line of code here and if I hover over my lucky numbers here we have values that we have entered for our array so value of 2 5 7 9 and 13 which are the values that we have entered here so I’m going to stop my program now please stop okay and now what I want to do is I want to use this other approach to write out the numbers that this lucky numbers array is holding so let’s use another for Loop I’m going to copy this one okay and then what this second for Loop is going to do it is just going to write out these numbers but but okay let’s use this approach for now so I’m going to just change this to see out and then use these other redirection signs and after each number I’m going to add an empty space okay like this and instead of using this approach here so instead of uh using these square brackets in order to deeri uh the element on that position let’s use this approach here so I’m going to say please use lucky numbers so which is the address of the first element and then to that I want to add the value of my I so plus I like this and this is going to move okay so this expression here is going to help us to access all of the elements of this lucky numbers array so from the one that has index zero until we come to the one that has index for and because we don’t want to write out addresses because this here is going to give us the address but we want to write out the values that are stored on those addresses we will have to dreference this expression here so for that I will use star symbol and I’m going to remove this breakpoint now and I’m going to start my program to see what is going to happen Okay and let’s enter 2 3 5 7 9 19 and as you can see we get values of 2 3 5 7 and 19 which we have entered okay let’s stop this program now and there is one more thing that I want to show you what is going to happen if I change this max value from four to five like this so what is going to happen in this particular situation let’s run our program and enter numbers again so 2 5 7 13 15 okay as you can see now here we have these five numbers that we have entered so 2 5 7 13 15 those are the ones here and then the last one is something that we do not recognize what is this this is some junk number that we definitely have not entered here so this number here is something that does not belong to our lucky numbers array and here we have accessed someone else’s memory base so we have this result because we changed max value from four to 5 here and as we already know our lucky numbers has five elements the last one having the index of four which is this one here and then here we added one more iteration so now we are trying to access the element with index five and that element with index 5 does not belong or does not exist in our lucky numbers array so so this element here this is memory location that belongs to someone else and we accessed that trying to deeri the pointer to the element that is Sixth Element so we have five elements in our lucky numbers array and then if we add one more address space to that we get this here which is memory location that belongs to someone else and if we just randomly change and access memory locations that do not belong to us we can cause very very big problems so I advise you to be very careful when iterating through your arrays and to access only memory locations that belong to that array that you are iterating so in the case that your array has five elements that those are going to be from zero to four so indexes from 0 to four in the case that your array has 10 elements for example those are going to be indexes 0 to 9 and so on so again be very very careful to not access memory locations that do not belong to you so I hope that now you have an idea on how pointers and arrays go together and as well we have seen some of the potential problems that can happen if you are not careful enough when working with pointers and arrays so again I advise you to be very careful uh when working with pointers and arrays and and not access memory locations memory addresses that do not belong to you so thank you for watching this video If you enjoyed it if you learned something new give it a thumbs up and subscribe to my channel hit that Bell icon as well you can follow me on my other social media if you want links for those are going to be in the description of this video thank you for watching and I’m going to see you in my next video bye hi everyone welcome to my channel in this video I want to explain how you can use pointers in order to return multiple values from a function so I said in the first video of this playlist that using pointers to return multiple values from a function is a very common use for pointers so in this video I want to demonstrate how that works so here we are going to create an array of numbers and then first I want to create two functions and one of those two is going to return me the smallest number in that array and then the other one is going to return me the largest number in that array and then in the second part of this video I want to show you how you can use pointers in order to get both smallest and largest number using one function only so you can return both of these numbers so Min number and Max number using only one function and that is going to be with the help of pointers so let’s start with this example as I already said uh I’m going to create an array of numbers so um those are going to be numbers of type int and let’s call this array numbers like this and it is going to be array of five numbers and let’s assign him values of for example 5 4 um – 2 29 and 6 for example so those are going to be my numbers okay and now let’s create a function that is going to return the smallest number of this array so I’m going to create that function here and let’s give it the return type of int and I’m going to call it get min like this and what I will need to pass to this function is going to be array so this array here so I’m going to say int numbers array so this is going to be the first argument and then the second one is going to be the size of this array so I’m going to say in size and we will use this second parameter here so that we know how many iterations we have to make in order to go through all of the elements of this array so let’s define this function the first thing that I want to do is I want to create a variable of type int and that variable is going to be called min so this is going to be the variable that is going to hold the minimal value so the smallest number in this array and initially I want to assign to this variable the first element of this array so I’m going to say that the smallest element initially is going to be the first one so I’m going to say numbers of zero and then we are going to iterate through all of the other elements and each time that we find an element that is smaller than the current value of our min we are going to store that value in our Min so let’s do that so I’m going to say four and since we have already stored the value of our first element in our Min we do not need to iterate through that so I’m going to say that my int initially has the value of one okay oh int I is equal to one like this and then our for Loop is going to iterate while our I is less than the size of this array so in this particular situation while our I is less than five and in each iteration we want to increment our I like this so as I already said if we find a number on a specific position which is in this current situation uh the position with the index of I if we determine that that number is smaller than our Min number like this we are going to store that number in our Min variable so I’m going to say Min is equal to numbers of I so that at the end of this for Loop in our Min variable is going to be the smallest number of this array so at the end of this for Loop here we can return our Min like this okay so now in order to test this function here I’m going to invoke it let’s say get actually I’m going to write it out I’m going to say C out Min is like this and then let’s invoke it let’s say get min like this and this get min function receives array so let’s pass it numbers array and then it receives the size of this array and this array has 1 2 3 4 five elements and you can check that here as well so we are going to pass Five here okay and if I run this program as you can see it says Min is minus 2 which really is the smallest number in our array so that is going to be the first function and then the second function that I want to create is going to be the one that is going to return the largest number in our array so let’s copy this function I’m going to copy it and then just modify it a little bit so the name of that other function is going to be get Max like this and then that function as well will receive numbers array and the size but here we are going to have variable which is called Max instead of Min and again we will assign it the value of our first element so then we iterate through all of these elements so starting with the second element because first the value of the first one we already have inside this variable so we do not need to check that but if we find in these remaining elements if we find element that is larger so bigger than our Max let’s copy this here we are going to store the value of that element in our Max variable like this and then at the end we will return our Max variable okay so now in order to test this function I will invoke it as we did with this previous one I’m I will just say Max is and then invoke get Max like this and I’m going to add two end lines here so that we have output which is formatted nicely and if I run my program now as you can see it says Min is minus 2 and then Max is 29 which is correct so let’s explain one more time the algorithm that we are using here in order to determine the biggest number of our array so here in this first line I say that the biggest number is the one with the index zero so the first one and then here we iterate through all of the remaining elements and that is from the one with index one until the last one and if we find that any of those remaining numbers is greater than our Max number then we say that our Max number is equal to that number so at the end of this for Loop we will have in our Max variable the largest number of this array here okay and we have accomplished to get our Min number and then our Max number using these two functions oh I’m sorry so using this get min and then get Max function as well so now what I want to show you is how you can use one function only to get both Min and Max number so let’s do that let’s create a function which we will name void get min and Max like this and this function is going to receive like these two previous functions our numbers array and the size of the array and then considering that we want to return two values from our function we will have to pass those two values by a reference which means we will have to pass the addresses of the two values and then this function here is going to change the value that is stored on those two addresses and this function here our main function is going to have the access to those two addresses as well so once this function here assigns the value of Min and Max numbers to those two addresses our main function is going to be able to access those values so as I said let’s pass to this function as well int Min like this and then int Max so we are receiving here pointer to A Min number and then pointer to a Max number okay so the job of this function will be very similar to the jobs of the two functions that we just created so I will just copy this and then paste it here and once more we are iterating through all of the numbers of our numbers array and then here we say if you find any number which is greater than our Max number and here we have an error which says operand types are incompatible so int and int pointer which means that here we have to D reference this pointer using the star symbol so if you find any number which is greater than our current Max please store that number inside our Max like this and then I’m going to copy this code one more time for our M number so I will say if you find any number which is smaller than our Min number like this Please assign that number to my Min number like this so this is going to be the work that this function will do and considering that this function is receiving these two so Min and Max as pointers this means that this function is directly going to modify the values that are stored on these addresses and those same addresses are going to be available in our main function which means that once this function changes the value vales that are stored on our Max and our Min address this main function here will be able to access those changed values and using pointers in that way we have achieved to return multiple values from our function so let’s invoke this get min and Max in order to test what I just said so I’m going to comment these two lines of code because we do not need them anymore and because here we have to pass Min and Max we will have to create those two variables here as well so I will say int Min and as we did before I’m going to assign to this Min the value of my first element like this okay and then I will create Max variable as well and assign to my Max variable value of my first element as well and now let’s invoke this get min and Max function so let’s say get min and Max and let’s pass it parameters that it needs so the first one is numbers array so I will say numbers and then second parameter is size of that array so five and then it expects to receive two pointers so Min and Max and we will have to pass addresses of our Min and then address of our Max as well like this and this here is called passing a parameter using a reference so that means pass an address of a variable to your function rather than passing variable itself because then if you don’t pass an address your function is going to create a copy and whatever changes your function makes with that copy you will not be able to see those changes in your main function so in this way we are passing addresses which means that this function here is operating on the original addresses which this function here is accessing as well so when this function here changes the values that are stored on those addresses this function here will know so that means that now if I try to write out oh I will need to add semic colum here at the end okay so that means now if we try to write out our Min like this Min is and then Min and then if we try to write out our Max as well Max we will get um values of Min -2 and 29 at least we expect to get those two values if this function is working correctly so if I run my program as you can see we really have values of min-2 let me just um collapse this so we really have values of Min -2 and 29 as the smallest and largest number in our array and we have achieved that using this pass by a reference so by passing addresses of our variables to our function which our function then changes the values that are stored on those addresses and since this main function as well has the access to those same addresses that means that our main function can see the changes that have been made on those addresses so I hope that now you have an idea how to return multiple values from a function using pointers so if you enjoyed this video If you learned something new give it a thumbs up and also subscribe to my channel and I will be publishing more videos related to pointers in the future so if you don’t want to miss out on those press Bell icon as well thank you for watching and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Salina and I make it and programming related videos so if that is something that is of interest to you consider subscribing to my channel and give this video a thumbs up as well and in this video I want to talk about Dynamic arrays so you should already be familiar with the idea of arrays and the concept of fixed arrays from my previous videos but for those of you that really want to understand the need for dynamic arrays I will go through some of the advantages and disadvantages of fixed arrays so that you can really understand where the need for dynamic arrays arised so we said that an array is type of collection which stores elements in continuous memory now that means that it stores elements one after the other so if I do this for example if I say let’s create an integer array of five elements and let’s call that array my array like this your computer in the background will do the following it will give to this array here five containers of type int and those containers will be one after the other in memory so that means because the array stores data in a continuous memory so one after the other accessing that data will be very very fast so because the name of the array is the address of the first element of that array when you specify the name of the array and then you give it the index of the element that you want to access it is going to be very fast to move from that first element until that position that you specified so until the element that has that index whereas if you use some other collection type like linked list for example which stores data in non-continuous memory which means in a random places in memory and then each element contains a pointer to the previous one and to the next one accessing elements in those type of collections would be much slower so if you wanted to access Fifth Element of linked list you would have to Traverse the first four elements and then get to the fifth one because they are stored in noncontinuous memory meaning they are randomly in your memory so as I already said accessing elements of an array is a very very fast operation but because your array stores data in continuous memory this has certain disadvantages as well so what happens if you want to insert or delete an element in the middle of the array for example so how can you insert or delete an element in the middle of the array without breaking that continuousness if I can say it like that so you get the point that means that the operation of inserting and deleting elements in an array is going to be much slower than inserting elements in a linked list for example and if you want me to do a video which is related to differences between linked lists and arrays for example write it in the comments down below and I will make it in the future when I find time so again if you are not familiar with the basics of arrays I recommend you to watch my first video which is related to arrays and pointers which I will link here and then you come back to this one so those were some advantages and disadvantages of arrays and it will be up to you to decide which collection you want to use however here I want to give you the biggest disadvantage or the biggest obstacle that we encountered so far when working with arrays and that is the following so the size of the array had to be constant which means that the size size of the array had to be known before we even started our program so we always kind of wanted to do this so we wanted to say int size and then we wanted to ask our user to enter the size so please enter the size for the array and then after our user enters the size for the array we wanted to create the array that is of that size that our user specified however we get the error as you can see here it says expression must have a constant value which confirms what I just said which is that the size of the array had to be known at compile time so the size of the array could not be changed once we declar it it and we had to declare it before we even started our program so this problem was without solution so far but surprise surprise if you know how to work with Dynamic arrays and dynamic memory you can very easily create an array and runtime so in C++ there are two keywords or two commands which allow you to allocate and deallocate dynamic memory which means to get yourself dynamic memory when you need it and then free that memory once you don’t so that it can be reused that is what allocate and deallocate means and those two commands are new and delete so new allocates memory as you may guess and then delete deallocates that memory or freeze it once you don’t need it so let’s see how we can use this information that I just gave you on our example so instead of doing this here I will comment it because we do not need it instead of doing this what I will do is I will create a pointer like this and let’s call that pointer my array and what I want to assign to this pointer will be the following I will say please make this pointer point to new integer array and the size of that array will be this size that our user entered like this okay and as you can see our compiler is fine with this expression here we do not have an error so this new keyword has allocated an array of this size that our user has specified and that array aray the address of the first element of that array is stored in this my array pointer so if I build this code as you can see we do not have compile time errors so what I want to show you now is how we can enter elements for this array and then how we can write them out and this is array again this is array which is created at runtime so our user will specify the size of this array which is something that we were not able to do before we learned how to use Dynamic arrays so uh in order to enter elements for this array here as we already know we will be using four Loops so I will say four please iterate from the element which has index zero until you come to the element that has the index of this size okay and then in each iteration increase our count counter by one and what I want to do is I want to write out a message for my user so I will say array and then let’s add index of the current element index that our user is entering so I like this okay and then what I want to do is I just want to enter an element on that position that we are current Curr it rating so I will say my array of I like this so using this code here we should be able to enter elements in this array here and then in order to write these elements out what I will do is I will use another for Loop so I will just copy this one and then paste it here and in this particular situation I just want to write out so I will use C out command and and then these other red Direction signs so I just want to write out all the elements of the array and I will add a few empty spaces after each element so that we have a nicely formatted output so this Loop here should write out the elements of our array and if I start this program now as you can see it asks us to enter the size of the array so let’s say five for example and now we are we are entering ele with index zero so let’s say 2 5 7 9 and let’s say 11 okay and as you can see here we have the elements of our array written out and we wouldn’t be able to do this if we didn’t know how to work with Dynamic arrays so let’s stop this program and there is one more thing that I want to mention and that is following these square brackets here are only one way to dreference your array another way to dreference the array or to to access the element on a specific position is the following so so the other way to access the element on a specific position would be using this asteris symbol and then here you say my array plus I now because the name of the array is the address of the first element here you have the address of the first element and then when you add a certain number to that it will move that many spaces in memory and then access the element on that position and when you dreference that you will get the value of that element instead of its address so here is one way to dreference your array and then here is another way to dreference your array and you can use whichever one you prefer and if I run this program again just to demonstrate that it works the same let’s say three and then one 5 10 okay we get 15 10 as the elements of our array one very important thing that I mentioned at the beginning is that we have two commands for working with dynamic memory and one of those two commands is new so we said that new is a command which will allocate memory for us when we need it and then another one is going to be delete and delete is a command which will de allocate that memory when we do not need it so that it can be reused Now using dynamic memory comes with a responsibility and that responsibility is the following each time that you allocate memory when you need it you will have to deallocate that memory once you don’t so that that memory can be reused so that means that each time that you write new command you will have to write delete command once you don’t need that memory anymore so I will demonstrate how can you deallocate the memory that we have allocated for this my array so let’s say that here in this line of code we do not need our array anymore so how can I deallocate the memory that I allocated for this array well using delete keyword and I will say please delete the memory that I allocated for my array which is called my AR array like this and you have to specify these square brackets because that is the way that you allocated memory so here you said I want new array and then here you have to say please delete that array and a good practice is to do the following as well so you can say my array is equal to null like this and let me collapse this so this here is going to assign the value of null to your array so your array will not point to anything to any address at this line of code so why do we do this because in the previous line of code we have deallocated this array here and now in this line of code here your array will point to a location in memory which does not belong to you so it does not belong to your program and because of that because we do not want to cause some accidental crashes or problems in our program by having this array pointing to memory locations which are not ours anymore it is a good practice to say that our array is now equal to null which means that it is not pointing to anything so as I already said and I repeat again when you use dynamic memory each time that you write new you will have to write the lead that corresponds to that new so here I have allocated an array and here I am deallocating that array so I hope that this video was helpful and that this video helped you understand what are Dynamic arrays and how they work and now that I have explained this it opened the whole world of new possibilities and new things that you can do with Dynamic arrays and I will leave to you to play and discover those new things until my next video of course so thank you for watching this video give it a thumbs up if you liked it subscribe to my channel click the Bell icon as well and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Alina and I make it and programming related videos so if you’re interested in those please subscribe to my channel and give this video a thumbs up and in this video I want to talk about a topic that confuses many students and that is the topic of multi-dimensional dynamic arrays and the topics like this one are the reason why many people step away from learning C+ Plus+ and they believe that learning C++ is hard but what I think is that many of those people never actually had the opportunity to have this explained in a very simple way so that is what I will try to do in this video so with the help of excel I will try to draw what are multi-dimensional Dynamic arrays and how they work and I hope that at the end of this video you will have it pretty clear and you will be able to create and work with your own multi-dimension Dynamic arrays so what a multi-dimensional array is in simple words it is array of arrays and in this video you will learn how to create two dimensional Dynamic arrays and in order to be able to understand this topic you should be familiar with the topic of dynamic arrays which is a video that I already did on my channel and I will link it here so make sure to watch that video before watching this one so I will very quickly remind us all of how Dynamic arrays work and then we will see how we can use that knowledge to understand multi-dimensional Dynamic arrays so let’s create a visual representation of a dynamic array in our Excel sheet so let’s say that it will be array of four elements like this this is our array and the indexes of these elements start with zero and then 1 2 3 so these are the indexes and then the address of this first element will be for example 000000 AA like this and this address here it is the address of the first element but it is the address of the array itself as well and you should be familiar with that so this here is our dynamic memory and then on our stack which will be here let’s create our stack so this here will be our static memory here we will have to create a pointer variable which will hold the address to this element here or actually the address to the array itself so I will create a variable let’s call it PTR 1 so this is going going to be our pointer one and as we already said that pointer will hold the address of the first element of this array let’s copy that address like this and then we will store that address in our PTR 1 now if we wanted to create more array way by the knowledge that we have so far we would have to copy this and then let’s paste it here and let’s create one more array like this so now we have three Dynamic arrays and the first element of the first array has the address of 0 0000 AA and then this one here let’s say that it has the address of BB for example and then this one here has the address of 0000 CC and to be able to access these arrays we would have to create on our stack two more pointers so let’s do that okay so our pointer 2 will hold the address of the second array which is 0000 BB and then our pointer Tre would hold the address of our third array which is 0000 CC like this now with this we have created three Dynamic arrays and in order to create those three Dynamic arrays we had to create three pointers so those are these three here and this is something that you should already be familiar with so you should already know from my previous video how to create a dynamic array now one thing that I see here and that is a problem is that if we wanted to create four or five or 100 of these Dynamic arrays we would have to create that number of pointers on our stack which means that this part here is not Dynamic only this part here is dynamic because here our user determines how many elements this Dynamic array will have but for each of these Dynamic arrays we have to manually create a pointer on our stack which will hold the address of that array okay now what happens if I want to move this part here to our dynamic memory as well so let’s do that let me cut it from here and then I will paste it here for example so now I have moved this part to dynamic memory as well and as you can see here this kind of looks like an array as well so that means that this first element if we look at this as an array the first element will have index of zero and then second will have index of one and then third will have oh this won’t move third will have index of two okay so this is an array that has three elements and the first element for example has the address of 00 0 e e e e like this so now this part here is dynamic and then this part here is dynamic and in order to be able to access this array here what we have to do is on our stack which is this part here we will have to create a pointer which will hold the address of this first element so let’s do that let me just okay now as I already said on our stack we will have to create a pointer which will hold the address of this first element so let’s call that pointer table for example and this table will hold the address of this element here which is 0 0 e e e e and this table because it holds the address it should be a pointer but one thing to keep in mind is that this variable here is not a simple variable but this here is a pointer as well which means that this table will not be a simple pointer but it will be a pointer to a pointer and you indicate that using two ASX symbols because one is just a pointer and then two mean a pointer to a pointer why because this table holds the address of the element which is this one here and that element is pointer as well okay now if we look at this here as you can see we have one array that represents the number of rows that our table will have okay so one 2 3 and then for each one of the elements of this array here we create a separate Dynamic array like this so this here will be our table that has the number of rows that are user defines and then however many columns are user defines as well and in this particular situation it is a table that has three rows so these are three rows and then four columns so let’s now translate this to our C++ code and because we said that now our user defines how many rows this table will have and then how many columns as well that is the first thing that I will do so I will create two variables let’s call them int rows and columns and then I will ask my user to enter how many rows and how many columns he wants this table to have so let’s do that see in rows o and then columns like this okay now after our user has entered how many rows and columns he wants what I want to do now is this part here so this part here this is our first step and then this here is going to be our second step and this part here will be the third step so this part here will be the following when translated to C++ code so here as I already said I create a pointer to a pointer which I will call table so I will say please create an integer pointer to a pointer because here we will store integer values you can create a character or double or float whichever one you want I want to create integer pointer to a pointer and call that table like this okay so we are done with this first part here and then what this table pointer to a pointer will point to is going to be this array here so I will say please create a new in integer array that has this many elements so the number that our user has entered for this rows variable because this rows variable holds how many elements this array here will have but as you can see here we have an error and that error is happening because this here which is this array here is not just a simple integer array but it is array of pointers so here you will have to indicate that so you will have to say please create an integer array of pointers and as you can see now our error has disappeared and with this line of code here we have created this array here now our third step is going to be that for each element of this array here we would have to create a separate Dynamic array so this first element will hold the address of this Dynamic array and then this second element holds the address of this array and then this third the address of this array here so what I will have to do is I will have to iterate through all the elements of this array and for each one I will have to create a separate Dynamic array so let’s do that so I will say for in I is equal to zero and then I is less than the number of rows that our user entered and in each iteration increment I so in this particular situation our number of rows is equal to three but you can have as many as you want so what I want to do in each of these iterations is I want to create for each one of these elements a separate Dynamic array so I will say table of I is equal to new integer array that has this many elements and that is the number that we stored in our columns variable like this and with this code here we have successfully created these Dynamic arrays now how do you access elements on a specific position of this table here for example this one or this one or this one so in order to do that let’s say for example table of Row one and then column 2 will have the value of 88 now what this could here what this line here will do is the following so this part here it says please give me the table variable which is this one here and it is a pointer to a pointer which holds this address here so 0 0 e e e e and that address is the address of this element here so the address of this array and here we say please give me the element of that array that has index one which is this element here so it is the element that stores the address of 0000 BB which is the address of this array here and this second part says please give me the element of that array that has the index of two which is this element here so here we will store the value of 88 so that is what this line of code here will do it will store number 88 to this position position here so this line of code here will store this number at the element that has row index one and column index two of this table so this here is a row with index zero this here is a row with index one and then two so we are storing at this row and then column index two is this one here so here we store number 88 one thing that we said as well in my previous video is that each time that you allocate dynamic memory you have the responsibility to deallocate that memory when you don’t need it which means each time that you use new keyword you will have to use delete keyword when you don’t need that memory anymore so now is the question how do we deallocate all of this memory that we have taken so in order to do that what I want to do is I want to show you something if for some reason we lose this address here so the value that is stored in this table pointer to a pointer we will never be able to access all of these elements that we have created in our dynamic memory so that means that we will not be deallocating this first and then if we for some reason lost this the value of our 000000 AA for example that means that we would never be able to access this array here so we would never be able to deallocate this memory so that means that this is not going to be deallocated it first as well now allocation of this memory here went as follows this was the first step so this was the first thing that we allocated and it is this code here and then we allocated this so that corresponds to this part here and then the last thing to allocate was these arrays here and that corresponds to this part of the code and the allocation of this memory will go the other way around which means that first we will deallocate this and then we will deallocate this and then this will be the last thing that we will have to worry about now in order to deallocate this the process will be almost the same as the one when we allocated it so I will copy this for Loop and then paste it here but what I want to do in this particular situation so when the allocating is instead of creating a new Dynamic array for each table of I is I want to delete that array so for each element of this array here I want to delete the corresponding Dynamic array so I want to delete these three arrays and in order to do that I will say please delete the array that my table of I is pointing to now what this code here will do is for this element here it will deallocate this array here so let’s delete that and then for this element here it will deallocate this array and then for this one it will deallocate this array here so now we have successfully freed this memory that we have taken here now after we have done this after we have successfully deallocated our first step which was here this part here will be our second thing to deallocate so here I will say please delete the array that my table variable is pointing to so this line of code here will deallocate this part here so I will say please delete that we do not need it anymore okay and then our third step which is the last one will be to null this value here why because we have just free the memory that was here so we have deallocated that now someone else might be using this memory here and we really shouldn’t keep the address of someone else’s memory so we should make this a null value like this and in order to do this in our code I will just say now my table will hold the value of null like this and this will prevent some potential crashes and problems in your application so I hope that this video helped you understand multi-dimensional Dynamic arrays a bit better and if it did please give it a thumbs up and subscribe to my channel because that helps me to reach more people and hopefully make programming a little bit easier for them as well thank you very much for watching and I’m going to see you in my next video bye hi everyone welcome to my channel my name is Alina I’m a software engineer and I make it and programming related videos here in YouTu YouTube and in this video I will be doing something that I haven’t done before and that is reviewing your code and we will see some errors that you guys have sent me so I get a lot of messages a lot of questions on my Instagram on Twitter and then here on YouTube as well in the comments where you ask me for an opinion or for advice on certain errors that happen to you in programming and I try to answer as many of those as possible but it’s just not possible to answer to all of you and I’m very very sorry but I thought that it would be a good idea to make a video related to that so that all of you can see it so here I am I’m making that video right now so in this video I will be reviewing some code and we will see some errors that can happen and that do happen very often and I’m going to do that both manually and then I want to show you a very cool tool which is called PVS Studio I’m going to leave details in the description down below so that you can check it out for yourself but what PVS studio is it is static code analyzer so it is basically a tool that reviews your code and helps you detect bugs and errors and security weaknesses in your code so in the case that you need another pair of eyes to look at your code and give you a few tips and recommendations it is a great tool to use and you can get it as a team license for teams up to nine people or you can get an Enterprise license for bigger companies bigger departments or you can even request a free license if you are a student if you are working on some private projects or on some open- Source projects and then one very cool thing is that it is not only used to analyze C++ code but you can use it for C and then C and Java as well uh and I wondering guys which team are you in are you team C++ or are you team C or Java or C um I’m very curious to read your answers so please write me in the comments down below and if you ask me I’m definitely team C++ and then team c those are just two of my favorite languages ever but I am very curious to read what you will answer to this question so um I’m going to leave all the details about PVS studio uh in the description of this video so that you can check it out for yourself and let’s jump into the coding part right now because I am very very excited to do this here I have created a new project and I have this program that does basically nothing so what I want want to do is I want to check if everything is okay with this code so far so if we have made any errors so far so I’m going to click on extensions and then PVS studio and here you have multiple options you can check your current file you can check open files or you can check your project selected items solution I’m going to select this first option so I’m going to check this current file and you get this window here and as you can see we have one error and that error is that my license will expire in 5 days okay so that is not error which is related to our code so we are just going to ignore it so we don’t have code errors so far and let me show you how you can install this tool very quickly so you click on extensions and then manage extensions okay and then here you can search for PVS Studio like this okay great it is this one here so PVS studio is a tool for detecting bugs and security weaknesses and so on okay so you will click download here and your download should begin I’m not going to do that right now because I already have it but once it is downloaded the installation is pretty simple so it’s basically next next next finish installation so that should be simple okay now let’s close this window here and as you could see we don’t have any errors with this code so far so let’s write some code and let’s create some errors now so so let’s create an array of type int and I will call it saved money and that will be array of five elements and I want to initialize those elements here so the purpose of this array is following let’s say that I want to save some money each month and I want to store that data in this array here so I’m going to save money for five months and I will store how much money I have saved each month inside this array here so for example the first month I have saved $100 or Euros or whatever and then second month I have saved 200 and then 300 and 400 and 500 fifth month okay so this is our array now what I want to do with this data here is I want to Summit after 5 months I want to know how much money I have saved in total so I’m going to create a variable of type int and I will call it total like this and I will initially give give it a value of zero and then I will iterate through this array here so I will say for INT I is equal to zero and then I is less than or equal to 5 and then I ++ and what I will do inside this for Loop is I will say that my total will hold whatever my total was previously holding plus current value of how much money I have saved in that month so saved money off I okay now after I have done this I will just write out how much money I have in total so I will say total and then this value that is stored inside this variable here okay now if I run this program let’s see what will happen well it says that in total I have minus 8 billion something very very big number so I have tried to save each month and then at the end I have minus8 billion dollar you know so that does not make any sense so here we have a logical error okay so let’s see what is this error that is happening so let’s click on extensions and PVS Studio check current file to see what kind of error PVS Studio sees and it says array over run is possible the value of I index could reach five and if I double click on that it will take me here and this part of code here is underlined and this is actually our compiler our compiler is underlying this and it says that okay it says index 5 is out of valid index range of 0 to four so this here this situation here says that this I is out of range of valid indexes that this array here can have and if you are familiar with arrays you know that indexing of an array starts zero so the first element has index of zero and then 1 2 3 and four so valid indexes for this array here are from 0o to four and here we have accidentally written that this I is less than or equal to five and that is making a problem so if I delete this part here so if I say that my I is just less than five this error should disappear so if I click on PVS 2 studio and then check current file the error should disappear okay it has disappeared now so if I run my program this program is behaving as expected so this is one thing that very often happens you can accidentally write less than or equal to and then you can get a very very big logical error another thing that can happen is if you’re typing very quickly and if you lose concentration for a moment and and here instead of incrementing you decrement this counter so you say I minus minus you should get an error as well so if I try to build this program let’s see what our compiler will tell us so here in this error list it says ill defined for Loop counts down from minimum so that’s kind of helpful um information and then another let’s read this other warning it says reading invalid data from Saved memory the readable size is 20 bytes but minus 4 bytes maybe read so this is not really help helpful for someone who’s just starting and then this third message it says index minus 2 billion something is out of valid index range 0 to 4 again this is not really helpful for someone who is just starting so uh let’s see what our PVS Studio has to say so I’m going to click extensions PVS Studio check current file okay and this first error here here says the condition I less than 5 of for Loop is always true so this is actually a helpful message and if I click on this I will be taken here and it says that this condition here I is less than five is always true but why is it always true because we have started counting at zero and then we try to decrement that value in each iteration and uh doing that we can never reach this condition here this condition here will never become false so that means that here we have created an infinite Loop and here our PVS Studio says consider inspecting this for operators so consider inspecting this for Loop here because it will not be executed at all or it can be executed incorrectly and in this particular situation it is going to be executed incorrectly so it is going to be an infinite Loop which we don’t want in this particular situation so I’m going to correct this error here so I’m going to return it to i++ but that is sort of a help that you can get from PVS studio and this here is a pretty simple program because it has just a few lines of code where all the bugs can potentially happen but imagine working on a very very big project and then something like this happens so imagine something like this happening uh on a program that a bank uses so you are trying to save money for a couple of months and then the program says that you are in depth that your balance is minus 2,000 or 20,000 or something like this so this can create very very big issues and in this particular situation if you have a bug like this you will want to have all the help that you can get and sometimes that can be uh just the help that you get from your compiler or if you have a very very big problem you will definitely benefit from tools like PVS Studio which will give you some additional information on that situation that is happening and on that problem that you you are having and then there are other situations where you can have very very big problems but your compiler will not be helpful at all because your compiler will not be able to see those potential bugs that you are creating inside your code so in order to demonstrate that let’s delete all of this code here because I will not need it anymore and what I do need is to create a function here so let’s create a function of return type void and I’m going to call it my function like this okay and what I want to do inside this function is I want to create an integer pointer PTR so if you’re not familiar with the topic of pointers make sure to watch the playlist that I will link here it is the playlist where I explain pointers in C++ because it is one of the most important topics in C++ and it is the example that I will be using here so I have created an integer pointer and I’m going to say now you will point to a new array of integers and that array will have five elements like this now what do I want to do with this array well let’s do something very simple let’s say PTR of two will hold the value okay it will hold the value of 10 like this and then let’s just write out something let’s say hi I am equal to and then PTR of two okay so this here is pretty simple example that does basically nothing important nothing useful but I will use it to demonstrate a very very big issue that can happen in this situation so I’m going to invoke this function here so I’m going to say my function like this and I have successfully invoked this function so if I run this program it says hi I am 10 so nothing unexpected happened or did it okay um if I inspect my errors list it says that I don’t have any errors so if you ask a compiler it will tell you that everything is perfectly fine so let’s now ask PVS Studio here it has already detected two potential issues but you can click PVS studio and then check current file just to be sure okay and it says visibility scope of the PTR pointer was exited without releasing the memory a memory leak is possible so what we have done here in this program is we have created memory leak where here so here we have said please give me in my dynamic memory give me array of five integers and this is going to be done in dynamic memory so once this function is finished once this function ends we are going to lose this PTR so we are going to lose the address of the first element of this array here so here if I try to do something like PTR I cannot access this variable here anymore this variable has its scope and that scope is inside this function here and once this function exits we are going to lose the address of this array here which means that we will not be able to deallocate this memory here now let’s delete this okay here as you can see this memory leak is not a big memory leak here we are leaking five integers so we are leaking an array of five integers and that is not very big problem but usually what happens in real life is that you are not creating an array of five integers usually you are creating things that require much more memory than just an integer array of five elements so here as I said we are leaking five integers but imagine a situation where you are creating an array of 5,000 or 50,000 and not integers but objects of type user like this and this user has some pretty heavy data in it for example it has some images and things like that so here you are creating 50,000 users and to make things worse imagine not invoking this function only once but imagine invoking this function in a for Loop for example so invoking it 100 times or 1,000 times or 100,000 times that is going to be a huge memory leak so what this line of code here does is it asks your operating system for dynamic memory so it says please can you give me new memory so can you give me dynamic memory that I need to store this data here and your operating system gives that memory to your program so it allocates that memory for your program and then once this function here finishes you will lose all the variables that you have created inside that function and that is this PTR variable here so that means that the address of this array here which was stored inside this PTR variable is lost now and now you will not be able to deallocate this memory here which means that you will not be able to say okay now I am done with this memory which is a lot of memory and now I’m going to return that back to my operating system because I don’t have unlimited amounts of memory and my operating system maybe would like to give that memory to some other program so once this function here ends and you don’t deallocate this memory here you are leaking that memory and that is exactly the error that you have here it says a memory leak is possible whereas your compiler here said expected a type specifier which is U error that we have here because it does not know what is this user so I’m going to return this back to int okay now that error has disappeared but your compiler does not know that you are leaking memory here whereas your PVS Studio analyzer here says that a memory leak is happening so how do you fix this error here well for each new you need to have delete again you need to watch my C++ pointers playlist in order to understand pointers if you don’t understand this already so as I said for each new you need to have delete so I’m going to say delete and let’s delete this PTR let’s see what is going to happen now so if I click extensions and then PVS Studio check current file it says the memory was allocated using new array type operator but it was released using delete operator and then it gives you a recommendation here it says use delete array pointer it set instead so here we are trying to allocate memory for an array but we are not deleting that entire array so here we are leaking memory again so here I’m going to use these square brackets in order to indicate that I want to delete this entire array that I have allocated in this line of code here so now we shouldn’t be leaking memory anymore so let’s check that out I’m going to say PVS Studio check current file and let’s see we have that error that we previously had which is your license will expire in 5 days but we do not have memory leaks anymore which is great so errors like this memory leaks are very common when you work with pointers in C++ and these memory leaks can create huge problems if you have them in your code and as you could already see in most cases you cannot really on your compiler to tell you that you are leaking memory because in most cases it will not even be able to see that so tools like PVS Studio can be very helpful in those situations you could see all those little tips and recommendations that we had from PVS Studio that helped us to fix issues that we had related to leaking memory so those were some topics that I wanted to address in this video because many people when working with pointers will come across problems that they will not be able to fix or they will not even be aware that they have those problems that they have memory leaks until it gets to the point where it is unmanageable and they have very big problem they have huge problem so um if you have any more questions that you would like me to make video on please leave those in the comments down below and if you like this type of video please give this video a thumbs up so that I can know and I will make more videos like this in the future if you want me to so thank you very much for watching this video and I am going to see you in my next video bye hi everyone [Music] well okay so hi everyone welcome to my channel welcome to another [Music] [Applause] [Music] hi everyone Hi everyone welcome to Welcome to my channel so this here is the initialization step of our initialization initialization in initialization initialization so it is the in in

By Amjad Izhar
Contact: amjad.izhar@gmail.com
https://amjadizhar.blog


Discover more from Amjad Izhar Blog

Subscribe to get the latest posts sent to your email.

Comments

Leave a comment