Getting Started with Data

C++ considers data to be the focal point of the problem-solving process. We stated above that C++ supports the object-oriented programming paradigm. In C++, as well as in any other object-oriented programming language, we define a class to be a description of what the data looks like (the state) and what the data can do (the behavior). Classes are analogous to abstract data types because a user of a class only sees the state and behavior of a data item. Data items are called objects in the object-oriented paradigm. An object is an instance of a class.

Built-in Atomic Data Types

C++ requires users to specify the data type of each variable before it is used. The primary C++ built-in atomic data types are: integer (int), floating point (float), double precision floating point (double), Boolean (bool), and character (char). There is also a special type which holds a memory location called pointer. C++ also has collection or compound data types, which will be discussed in a future chapter.

Numeric Data

Numeric C++ data types include int for integer, float for floating point, double for double precision floating point.

The standard arithmetic operations, +, -, *, and / are used with optional parentheses to force the order of operations away from normal operator precedence.

When two integers are divided in C++, the integer portion of the quotient is returned and the fractional portion is removed. i.e. When two integers are divided, integer division is used. To get the whole quotient, declaring one of the numbers as a float will convert the entire result into floating point.

Exponentiation in C++ is done using pow() from the cmath library and the remainder (modulo) operator is done with %.

When declaring numeric variables in C++, modifiers like short, long, and unsigned can optionally be used to help to ensure space is used as efficiently as possible.

Run the following code to see that you understand each result.

//outputs results from various math expressions
#include <iostream>
#include <cmath>
using namespace std;

int main(){

    cout << (2+3*4) << endl;
    cout << (2+3)*4 << endl;
    cout << pow(2, 10) << endl;
    cout << float(6)/3 << endl;
    cout << float(7)/3 << endl;
    cout << 7/3 << endl; //In C++ this is integer division
    cout << 7%3 << endl;
    cout << float(3)/6 << endl;
    cout << 3/6 << endl;
    cout << 3%6 << endl;
    cout << pow(2, 100) << endl;

    return 0;

Boolean Data

Boolean data types are named after George Boole who was an English mathematician, so the word “Boolean” should be capitalized. However, the Boolean data type, in C++ uses the keyword bool which is not capitalized. The possible state values for a C++ Boolean are lower case true and false.

C++ uses the standard Boolean operators, “and” is given by && , “or” is given by ||, and “not” is given by !. Note that the internally stored values representing true and false are actually 1 and 0 respectively. Hence, we see this in output as well.

//outputs boolean results to the command line
#include <iostream>
using namespace std;

int main() {
  cout << true << endl;
  cout << false << endl;
  cout << (true || false) << endl;
  cout << (true && false) << endl;
  return 0;

Boolean data objects are also used as results for comparison operators such as equality (==) and greater than (>). In addition, relational operators and logical operators can be combined together to form complex logical questions. The table below shows the relational and logical operators with examples shown in the session that follows.

Operation NameOperatorExplanation
less than<Less than operator
greater than>Greater than operator
less than or equal<=Less than or equal to operator
greater than or equal> =Greater than or equal to operator
equal==Equality operator
not equal!=Not equal operator
logical and&&Both operands true for result to be true
logical or||One or the other operand is true for the result to be true
logical not!Negates the truth value, false becomes true, true becomes false
//outputs the results from the relational and logical operators
#include <iostream>
using namespace std;

int main(){

    cout << (5 == 10) << endl;
    cout << (10 > 5) << endl;
    cout << ((5 >= 1) && (5 <= 10)) << endl;

    return 0;

When a C++ variable is declared space in memory is set aside to hold this type of value. A C++ variable can optionally be initialized in the declaration by using a combination of a declaration and an assignment statement.

Consider the following session:

//showcases how variables work in C++
#include <iostream>
using namespace std;

int main(){

    int theSum = 4;
    cout << theSum << endl;

    theSum = theSum + 1;
    cout << theSum << endl;

    bool theBool = true;
    cout << theBool << endl;

    theBool = 4;
    cout << theBool << endl;

    return 0;

The declaration int theSum = 0; creates a variable called theSum and initializes it to hold the data value of 0. The right-hand side of each assignment statement is evaluated and the resulting data value is “assigned” to the variable named on the left-hand side. Here the type of the variable is integer.

In C++, the data type cannot change. This is a characteristic of C++'s static typing. A variable can hold ever only one type of data. Pitfall: C++ will often simply try to do the assignment you requested without complaining. Note what happened in the code above in the final output.

Character Data

In C++ single quotes are used for the character (char) data type, and double quotes are used for the string data type.

Consider the following code.

//outputs the boolean results to show how strings and chars differ in C++
#include <iostream>
#include <string>
using namespace std;

int main(){

    string strvar = "b";
    char charvar = 'b';

    cout << ('b' == charvar) << endl;
    cout << ("b" == strvar) << endl;
    //cout << ('a' == "a") << endl; // will error!

    return 0;

Knowledge Check

  1. What makes a data type categorized as an atomic data type? (hint: arrays and vectors are not atomic)

    • A. It must be mutable so that it can change at any time.
    • B. Its memory size cannot be too big, relating to how atoms are naturally very small.
    • C. It must be statically allocated so it cannot change in size.
    • D. It cannot be broken down any smaller while still being easily operable.
  2. What is the result of dividing 3/2 in C++?

    • A. 1
    • B. 1.5
    • C. 2
    • D. A run-time error will occur.
    • E. none of the above
  3. How do I raise 4 to 5th power in C++?

    • A. 4**5
    • B. 5**4
    • C. 4^5
    • D. pow(4, 5)
  4. In the theSum example above in the Boolean section, why did theBool output a value of 1 after being set to 4?

    • A. Setting theBool to anything other than true or false is ignored.
    • B. Setting theBool to anything > 0 will be true and false otherwise.
    • C. false == 0 and true = !false i.e. anything that is not zero and can be converted to a Boolean is not false, thus it must be true.
    • D. None of the above.
  5. If I want to create a string in C++, what set of symbols may be used?

    • A. ' '
    • B. " "
    • C. ' ' or " " may be used
    • D. It depends upon the implementation.
    • E. none of the above