Variables in C++ (Integers and Strings

In this teaching, I will discuss how to declare variables and output them in a simple C++ Program.

Number Variables

  • You can declare the variables in one line and set their value in a second line.
  • Better Yet: You can declare the variables and set their values in the same line. The less code you have to get the job done the more easier it is for others to read your code.

Example of declaring a variable:

int balance = 12;

 

Example of variable output:

cout << balance;

 

Example of variable output with text:

cout << “The balance is “ << balance;

 

String Variables

  • You can set a string variable by using the string object which requires you to use the include statement for string at the beginning of your cpp file.
  • To declare and set a string value you type string, then the variable name you desire, and then set the value.

Example of String class include statement:

#include <string>

 

Example of declaring a string statement:

string myName = “Zach Murphy”

Leave a Reply

Your email address will not be published. Required fields are marked *