Types and Expressions

COS2614 - Programming: Contemporary Concepts · C++ Language Reference

Types and Expressions

This topic covers the basic types and expressions in C++. Understanding types and expressions is crucial because they form the foundation of programming in C++. Types determine what kind of data you can work with, while expressions allow you to manipulate that data.

Key idea: After studying this topic, you should be able to:

  • Identify and use different data types in C++.
  • Understand the concept of expressions and how they are evaluated.
  • Write simple C++ programs that utilize various types and expressions.

Data Types in C++

C++ has several built-in data types. These types can be broadly classified into three categories: fundamental types, derived types, and user-defined types.

Fundamental Types

Fundamental types are the basic building blocks of data in C++. They include:

  • int: Represents integer values (e.g., 1, -42, 0).
  • float: Represents floating-point numbers (e.g., 3.14, -0.001).
  • double: Represents double-precision floating-point numbers (e.g., 2.718281828459).
  • char: Represents single characters (e.g., 'a', 'Z').
  • bool: Represents boolean values (true or false).

For example, the following code declares variables of different fundamental types:

int age = 25;  // An integer type
float height = 1.75;  // A float type
double pi = 3.14159;  // A double type
char initial = 'J';  // A char type
bool isStudent = true;  // A bool type

Derived Types

Derived types are built from fundamental types. The most common derived types are:

  • Arrays: A collection of elements of the same type. For example, int numbers[5] = {1, 2, 3, 4, 5}; declares an array of five integers.
  • Pointers: Variables that store the address of another variable. For example, int* ptr = &age; creates a pointer to the variable age.
  • References: An alias for another variable. For example, int& ref = age; creates a reference to age.

User-Defined Types

User-defined types allow you to create your own data types using classes and structures. For example:

class Person {
public:
    int age;
    float height;
};

This code defines a Person class with two attributes: age and height.

Expressions in C++

Expressions are combinations of variables, constants, and operators that evaluate to a value. C++ supports various types of expressions:

Arithmetic Expressions

These expressions perform mathematical operations. Common operators include:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus

For example:

int sum = 5 + 10;
float average = (sum / 2.0);

Relational Expressions

These expressions compare values and return a boolean result (true or false). Common relational operators include:

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

For example:

bool isEqual = (age == 25);

Logical Expressions

These expressions combine boolean values using logical operators:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

For example:

bool canVote = (age >= 18 && isStudent == false);

Common Mistakes

Watch out: A common mistake is mixing data types in expressions. For example, trying to divide an integer by a float without proper casting can lead to unexpected results.

Summary

  • C++ has several fundamental data types such as int, float, double, char, and bool.
  • Derived types include arrays, pointers, and references.
  • User-defined types can be created using classes and structures.
  • Expressions are combinations of variables and operators that evaluate to a value.
  • Common types of expressions include arithmetic, relational, and logical expressions.

Check your understanding

  1. What are the fundamental data types in C++?
  2. How do you declare an array in C++?
  3. What is the difference between a pointer and a reference?
  4. What will the following expression evaluate to: (5 > 3) && (2 < 1)?