Fundamental Concepts of C++: Introduction to Variables

Variable Declaration

Declaring a variable reserves a space in memory to store the value of that variable. The compiler requires that you specify the type of the variable when declaring it.

C++ provides a rich set of built-in variable types and allows for user-defined variable types.

For example, int is a built-in type that represents an integer value. Use the keyword int to define an integer.

C++ requires you to specify the type and identifier for each defined variable.

An identifier is the name of a variable, function, class, module, or any other user-defined type. Identifiers must start with a letter (A-Z or a-z) or an underscore (_), followed by letters, underscores, and digits (0-9).

For example, to define a variable named python to hold an integer value:

int python = 10; 

Variables of the same type may occupy different amounts of memory on different operating systems.

Variable Assignment

Now, let’s assign a value to the variable and print it:

#include <iostream>using namespace std;int main(){    int myVariable = 10;    cout << myVariable;    return 0;}

The C++ programming language is case-sensitive, so myVariable and myvariable are two different variable names.

For example:

Suppose you have a variable named var, type the code to print its value:

cout << var;

In C++, cout is the standard output stream object used to output data to the console. By using the << operator, you can output the value of the variable var to the console. Thus, using cout allows you to print the value of a variable to the console.

Variable Assignment

Before using a variable, you must first declare (define) the variable name and type.

If you have multiple variables of the same type, you can define them in a single declaration, separated by commas, like this:

int a, b;// Define two variables a and b of the same type

Variables can be assigned values and used to perform operations in code.

For example, we define a sum variable and assign it the sum of two other variables:

int a = 30; int b = 15; int sum = a + b;// After execution, sum's value is 45

Using the + operator adds the two numbers together.

For example:

Declare a variable sum and assign it the sum of variables a and b:

int sum = a + b;

Adding Variables

Now, let’s write a program to calculate the sum of two integers and print it:

#include <iostream>using namespace std;int main(){    int a = 30;    int b = 12;    int sum = a + b;    // Print sum    cout << sum;     return 0;}

Remember, all variables must have a defined name and type before use.

For example:

David (aquariumDavid) and Alex (aquariumAlex) each have an aquarium. David’s tank has 8 rainbow fish, and Alex’s tank has 11 angelfish. Help them swap their fish. Complete the code to swap the values between aquariumDavid and aquariumAlex. You will need a third empty tank to temporarily hold the fish from one of the tanks to facilitate the swap.

#include <iostream>using namespace std;int main() {    int aquariumDavid = 8;    int aquariumAlex = 11;    int temp;        temp = aquariumDavid;    aquariumDavid = aquariumAlex;    aquariumAlex = temp;        cout << "David's aquarium: " << aquariumDavid << endl;    cout << "Alex's aquarium: " << aquariumAlex;    return 0;}

Leave a Comment