A data type constrains the values that an expression, such as a variable or a function, might take. They define the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored.
download this file and complete the form:
There are several types of data in programming:
- Numeric data
- Integer
- Float
- Character
- All characters (a to z, A to Z)
- Boolean
- This data type returns one of two possible values –
true
orfalse
- This data type returns one of two possible values –
It is declared with the keyword bool and returns the values true = 1
and false = 0
.
See the example below:
bool isCodingFun = true; bool isFishTasty = false; cout << isCodingFun // => 1 (true) cout << isFishTasty // => 0 (false)
C++ data types can be put into one of two categories:
Primitive
Data types that specify the size and type of variable values
These are the basic data types available in C++. Also called ‘built-in data types’. There are 7 basic data types:
- Boolean
- Character
- Integer
- Floating point
- Double floating point
- Valueless
- Wide character
This repl include a range of variable and datatypes declared at the top of the main.cpp file. Click Run to see its output.
Create a fork of this project to experiment with different values for the variables.
Non-primitive
Derived data types and user defined data types. These refer to objects and call methods to perform certain functions.
Keywords for user-defined data types include struct, enum, union, class and typedef
For Derived data types include array, linked list, pointers and reference. These data types will be discussed later in this module and in CS103 Integrated Studio I
Explore Further
For some further explanation of data types, review Educba (2020) Introduction to C++ Data Types. C++ Programming Tutorial.
Tutorialspoint (n.d.) C++ Data Types also provides further explanation of data types, variable sizes and range, and the modification of data types, along with examples of coding using data types.
Coding challenge
login to your replit team and complete the Functions Project. A link to join the team is available in the forum.
https://replit.com/teams/join/bsuuctlewesfaxgzpromcqobmajgxcpg-dip-software-one
A variable is a named location in memory that is used to hold a value that may be modified by the program. Each variable in C++ has specific characteristics that determine the size and layout of the variable’s memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. C++ is case sensitive therefore upper and lowercase letters are distinct. All variables must be declared before they can be used.
- The general form of a declaration is
type variable_list;
type
must be a valid C++ data type (including char
, w_char
, int
, float
, double
, bool
or any user-defined object), and variable_list
may consist of one or more identifier names separated by commas.
Some valid declarations are shown here:
Examples of variable declarations:
int i, j;
short int si;
unsigned int ui;
double balance, profit, loss;
Variable Rules
When you write variables you need to adhere to the following rules to avoid errors in your code.
- Variable names must begin with a letter or underscore (_) or dollar sign ($);
- Variable names are case sensitive.
- No special characters can be used except “_” and “$” sign;
- No blank spaces
- C++ keywords cannot be used as variable names.
In this video the presenter passionately talks about variables and demonstrates the use of variables in coding.
A data type constrains the values that an expression, such as a variable or a function, might take. They define the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored.
There are several types of data in programming:
- Numeric data
- Integer
- Float
- Character
- All characters (a to z, A to Z)
- Boolean
- This data type returns one of two possible values –
true
orfalse
- This data type returns one of two possible values –
It is declared with the keyword bool and returns the values true = 1
and false = 0
.
See the example below:
bool isCodingFun = true; bool isFishTasty = false; cout << isCodingFun // => 1 (true) cout << isFishTasty // => 0 (false)
C++ data types can be put into one of two categories:
Primitive
Data types that specify the size and type of variable values
These are the basic data types available in C++. Also called ‘built-in data types’. There are 7 basic data types:
- Boolean
- Character
- Integer
- Floating point
- Double floating point
- Valueless
- Wide character
This repl include a range of variable and datatypes declared at the top of the main.cpp file. Click Run to see its output.
Create a fork of this project to experiment with different values for the variables.
Non-primitive
Derived data types and user defined data types. These refer to objects and call methods to perform certain functions.
Keywords for user-defined data types include struct, enum, union, class and typedef
For Derived data types include array, linked list, pointers and reference. These data types will be discussed later in this module and in CS103 Integrated Studio I
Explore Further
For some further explanation of data types, review Educba (2020) Introduction to C++ Data Types. C++ Programming Tutorial.
Tutorialspoint (n.d.) C++ Data Types also provides further explanation of data types, variable sizes and range, and the modification of data types, along with examples of coding using data types.
Coding challenge
login to your replit team and complete the Functions Project. A link to join the team is available in the forum.
https://replit.com/teams/join/bsuuctlewesfaxgzpromcqobmajgxcpg-dip-software-one
Operators
Operators are the symbols used by the compiler to perform mathematical operations. There are various types of operators in C++, each perform specific functions.
The different types of operators and their functions/purpose are listed below:
- Arithmetic Operators are used to perform mathematical operations. There are two types of arithmetic operators – Unary and Binary. Unary operators work on single operators (
++
,--
), and binary operators work with two operands (+
,-
,*
,/
) - Relational or Comparison Operators are used to compare the values of two operands. For example, is the value of one operand greater than the other?
- Logical Operators are used for combining two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either
true
orfalse
. - Assignment Operators are used to assign values to another according to specific conditions.
For a deeper consideration of operators, the range of operators and their associated symbols, along with examples of the syntax of writing operators check out the following links:
Putting it all together
Run the following repl to see its results. Looking at the main.cpp file, a range of variables of different datatypes are declared at the top.
cout is used with the + operator to output the sum of two integers. cout is also used to display a message that includes the contents of the string variables, and lastly an if statement is used to present a message based on which letter is stored in the char variable.
Try doing each of the following, running the between each change:
- Change the string variables to reflect your name
- change the values of the integers
- change the operator used in the output to *
- change the letter stored in the char variable to a grade you would like to receive
- change the integer variables to float, and assign decimal values.
Introduction to Pointers to arrays
There is a close relationship between pointers and arrays.
Here’s a quick example to illustrate this:
char str[80], *p1;
p1 = str
Value of pointer “p1” has been set to the address of the first array element in str.
If you wanted to access the fifth element in str, you could do it two ways, through the array or through the pointer:
Through the array: str[4]
Through the pointer: *(p1+4)
(We used 4 because the first element in an array always starts at position zero).
Array names can be used as pointer constants, and pointers can be used as array names. A pointer constant is a pointer where the value cannot be changed.
As we learned earlier in the multidimensional arrays section, array elements are stored together in memory.
In this example, ‘numbers’ is the address of numbers[0] – the first short element in the array called numbers. Each short is 2 bytes, so we are creating an array called ‘numbers’ and allocating 20 bytes of memory, 2 bytes for each of the 10 elements.
short numbers[] = {10, 20, 30, 40, 50}
The expression *numbers or *(numbers+0) would retrieve the value of the first element in the array, which is 10.
And so it follows that retrieving the fourth element of the array ‘numbers’ would go like this: *numbers[3] or *(numbers+3) and the output would be 40.
When you add a value to a pointer, such as *(numbers+2) you are actually adding the value stated times the size of the data type of the pointer. In this case, the pointer type is short, which is 2 bytes. So, if you add one to numbers, you are actually adding 1 * sizeof (short) to numbers. This means:
Have a look at the following example. Read the comments to help you follow what is happening.
To prepare you for this example, let’s have a quick look at for loops.
For loop
The for loop contains three statements inside the brackets and a code block that follows.
Statement 1: runs one time before the code block
Statement 2: the condition for running the code block
Statement 3: is run every time after the code block has been executed.
For example:
Result:
Example 1
Notes for this example:
cin stands for character input, and this is where the program requires an input
Running this program will result in the following.
Array names are pointer constants
Firstly, what is a pointer constant? A pointer constant is a pointer where the value is fixed, you cannot change it after declaration. So, an array name is a pointer to the first element in an array and this address cannot be changed.
These statements are legal:
But these statements are illegal:
Pointer arithmetic
You cannot use arithmetic on arrays, only on pointers. Pointers can be manipulated by the following operators:
++ | Access the next value from the array |
-- | Access the previous value from the array |
< | Less than |
> | More than |
<= | Less than or equal to |
>= | More than or equal too |
Read through C++ pointers vs arrays (Tutorials Point, n.d.) to understand the difference in an example.
Referencing and dereferencing
You will have noticed this in our code already. Referencing is when you are asking for the address of what your pointer is pointing to, however dereferencing is when you are asking for the value of what the pointer is pointing to.
There are three different ways the * is used in our code:
* is used for multiplication
* is used to declare a pointer
* is a deference operator, it is used to request the value of what a pointer is pointing to.
See C++ Dereference (W3Schools, n.d.) for an example of this.
Introduction to Pointers to arrays
There is a close relationship between pointers and arrays.
Here’s a quick example to illustrate this:
char str[80], *p1;
p1 = str
Value of pointer “p1” has been set to the address of the first array element in str.
If you wanted to access the fifth element in str, you could do it two ways, through the array or through the pointer:
Through the array: str[4]
Through the pointer: *(p1+4)
(We used 4 because the first element in an array always starts at position zero).
Array names can be used as pointer constants, and pointers can be used as array names. A pointer constant is a pointer where the value cannot be changed.
As we learned earlier in the multidimensional arrays section, array elements are stored together in memory.
In this example, ‘numbers’ is the address of numbers[0] – the first short element in the array called numbers. Each short is 2 bytes, so we are creating an array called ‘numbers’ and allocating 20 bytes of memory, 2 bytes for each of the 10 elements.
short numbers[] = {10, 20, 30, 40, 50}
The expression *numbers or *(numbers+0) would retrieve the value of the first element in the array, which is 10.
And so it follows that retrieving the fourth element of the array ‘numbers’ would go like this: *numbers[3] or *(numbers+3) and the output would be 40.
When you add a value to a pointer, such as *(numbers+2) you are actually adding the value stated times the size of the data type of the pointer. In this case, the pointer type is short, which is 2 bytes. So, if you add one to numbers, you are actually adding 1 * sizeof (short) to numbers. This means:
Have a look at the following example. Read the comments to help you follow what is happening.
To prepare you for this example, let’s have a quick look at for loops.
For loop
The for loop contains three statements inside the brackets and a code block that follows.
Statement 1: runs one time before the code block
Statement 2: the condition for running the code block
Statement 3: is run every time after the code block has been executed.
For example:
Result:
Example 1
Notes for this example:
This program uses cin for character input.
Before running the program, try to predict what the output will be.
Run the program and follow the instructions to see the result.
Try changing the valve of the SIZE. What effect does it have on the number of values stored?
Array names are pointer constants
Firstly, what is a pointer constant? A pointer constant is a pointer where the value is fixed, you cannot change it after declaration. So, an array name is a pointer to the first element in an array and this address cannot be changed.
These statements are legal:
But these statements are illegal:
Pointer arithmetic
You cannot use arithmetic on arrays, only on pointers. Pointers can be manipulated by the following operators:
++ | Access the next value from the array |
-- | Access the previous value from the array |
< | Less than |
> | More than |
<= | Less than or equal to |
>= | More than or equal too |
Read through C++ pointers vs arrays (Tutorials Point, n.d.) to understand the difference in an example.
Referencing and dereferencing
You will have noticed this in our code already. Referencing is when you are asking for the address of what your pointer is pointing to, however dereferencing is when you are asking for the value of what the pointer is pointing to.
There are three different ways the * is used in our code:
* is used for multiplication
* is used to declare a pointer
* is a deference operator, it is used to request the value of what a pointer is pointing to.
See C++ Dereference (W3Schools, n.d.) for an example of this.