Effective programming requires understanding how data is represented and manipulated. This chapter examines C++’s fundamental data types (integers, floating-point numbers, characters, and booleans), type conversion, and variable declarations. Students will learn about arithmetic, relational, logical, and bitwise operators, operator precedence, and expressions. The chapter also introduces compound data types like arrays and structures for organizing related data.
Chapter 5: Data Types and Operators
Basic Data Types in C++
Fundamental Data Types
- Integer Types:
- int: Standard integer (-2,147,483,648 to 2,147,483,647)
- short: Smaller integer (-32,768 to 32,767)
- long: Larger integer (at least ±2,147,483,647)
- long long: Even larger integer (at least ±9,223,372,036,854,775,807)
- Floating-Point Types:
- float: Single precision (7 decimal digits precision)
- double: Double precision (15 decimal digits precision)
- long double: Extended precision (varies by implementation)
- Character Types:
- char: Single character (‘A’, ‘1’, ‘*’)
- wchar_t: Wide character (for international characters)
- char16_t/char32_t: Unicode characters
- Boolean Type:
- bool: Logical value (true or false)
- Void Type:
- void: Represents absence of type
Type Modifiers
- signed: Can represent both positive and negative values (default)
- unsigned: Represents only positive values
- short: Reduces the size of the data type
- long: Increases the size of the data type
Derived Data Types
- Arrays: Collection of elements of the same type
cpp
int numbers[5] = {1, 2, 3, 4, 5};
- Pointers: Store memory addresses
cpp
int num = 10; int* ptr = #
- References: Alias for another variable
cpp
int num = 10; int& ref = num;
User-Defined Data Types
- struct: Groups different data types under one name
cpp
struct Student { string name; int age; float gpa; };
- enum: User-defined type consisting of integral constants
cpp
enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
- class: Blueprint for objects (covered in object-oriented programming)
cpp
class Rectangle { private: int length, width; public: void setDimensions(int l, int w) { length = l; width = w; } int area() { return length * width; } };
Type Conversion
- Implicit Conversion: Automatic conversion
cpp
int x = 10; double y = x; // int to double
- Explicit Conversion (Casting):
- C-style casting:
(type) expression
- Function-style casting:
type(expression)
- C++ casting operators:
static_cast
,dynamic_cast
,const_cast
,reinterpret_cast
- C-style casting:
Operators in C++
Arithmetic Operators
- Addition (+):
a + b
- Subtraction (-):
a - b
- Multiplication (*):
a * b
- Division (/):
a / b
- Modulus (%):
a % b
(remainder after division) - Increment (++):
a++
or++a
- Decrement (–):
a--
or--a
Relational Operators
- Equal to (==):
a == b
- Not equal to (!=):
a != b
- Greater than (>):
a > b
- Less than (<):
a < b
- Greater than or equal to (>=):
a >= b
- Less than or equal to (<=):
a <= b
Logical Operators
- Logical AND (&&):
a && b
- Logical OR (||):
a || b
- Logical NOT (!):
!a
Assignment Operators
- Simple assignment (=):
a = b
- Add and assign (+=):
a += b
equivalent toa = a + b
- Subtract and assign (-=):
a -= b
- Multiply and assign (*=):
a *= b
- Divide and assign (/=):
a /= b
- Modulus and assign (%=):
a %= b
Bitwise Operators
- Bitwise AND (&):
a & b
- Bitwise OR (|):
a | b
- Bitwise XOR (^):
a ^ b
- Bitwise NOT (~):
~a
- Left shift (<<):
a << b
- Right shift (>>):
a >> b
Other Operators
- Conditional/Ternary (?:):
condition ? expr1 : expr2
- sizeof: Returns size of variable/type
- Comma (,): Evaluates multiple expressions
- Pointer operators (*, &): Dereference and address-of
Operator Precedence and Associativity
Operators have different precedence levels determining the order of evaluation:
- Parentheses have highest precedence
- Postfix operators (++, –)
- Prefix operators (++, –, +, -, !, ~)
- Multiplication, division, modulus
- Addition, subtraction
- Shift operators
- Relational operators
- Equality operators
- Bitwise operators
- Logical operators
- Conditional operator
- Assignment operators
- Comma operator
Understanding data types and operators is essential for writing effective C++ programs that manipulate data correctly and efficiently.
Complete Chapter-wise Hsslive Plus One Computer Application Notes
Our HSSLive Plus One Computer Application Notes cover all chapters with key focus areas to help you organize your study effectively:
- Chapter 1 Fundamentals of Computer
- Chapter 2 Components of the Computer System
- Chapter 3 Principles of Programming and Problem Solving
- Chapter 4 Getting Started with C++
- Chapter 5 Data Types and Operators
- Chapter 6 Introduction to Programming
- Chapter 7 Control Statements
- Chapter 8 Computer Networks
- Chapter 9 Internet
- Chapter 10 IT Applications