C++ represents a powerful, widely-used programming language that balances efficiency with abstraction capabilities. This chapter introduces the fundamentals of C++ programming, including program structure, basic syntax, and the compilation process that transforms source code into executable programs. It covers essential programming concepts such as identifiers, keywords, comments, and basic input/output operations, providing students with their first practical experience in translating algorithms into functional code that a computer can execute.
Operators
Arithmetic Operators
Perform mathematical operations on numeric values.
- Addition
+
: Adds two values - Subtraction
-
: Subtracts the right operand from the left operand - Multiplication
*
: Multiplies two values - Division
/
: Divides the left operand by the right operand - Modulus
%
: Returns the remainder after division - Increment
++
: Increases the value by 1 - Decrement
--
: Decreases the value by 1
Example:
int a = 10, b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1
a++; // a becomes 11
b--; // b becomes 2
Relational Operators
Compare values and return boolean results.
- Equal to
==
: Returns true if operands are equal - Not equal to
!=
: Returns true if operands are not equal - Greater than
>
: Returns true if left operand is greater than right operand - Less than
<
: Returns true if left operand is less than right operand - Greater than or equal to
>=
: Returns true if left operand is greater than or equal to right operand - Less than or equal to
<=
: Returns true if left operand is less than or equal to right operand
Example:
int x = 10, y = 20;
bool isEqual = (x == y); // false
bool isNotEqual = (x != y); // true
bool isGreater = (x > y); // false
bool isLess = (x < y); // true
bool isGreaterEqual = (x >= y); // false
bool isLessEqual = (x <= y); // true
Logical Operators
Perform boolean operations.
- Logical AND
&&
: Returns true if both operands are true - Logical OR
||
: Returns true if at least one operand is true - Logical NOT
!
: Returns the opposite of the operand’s value
Example:
bool a = true, b = false;
bool andResult = a && b; // false
bool orResult = a || b; // true
bool notResult = !a; // false
Bitwise Operators
Perform operations at the bit level.
- Bitwise AND
&
: Sets each bit to 1 if both bits are 1 - Bitwise OR
|
: Sets each bit to 1 if at least one bit is 1 - Bitwise XOR
^
: Sets each bit to 1 if only one of the bits is 1 - Bitwise NOT
~
: Inverts all bits - Left shift
<<
: Shifts bits left, filling with zeros - Right shift
>>
: Shifts bits right, filling with sign bit or zero
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int bitwiseAnd = a & b; // 0001 = 1
int bitwiseOr = a | b; // 0111 = 7
int bitwiseXor = a ^ b; // 0110 = 6
int bitwiseNot = ~a; // 1010 = -6 (in two's complement)
int leftShift = a << 1; // 1010 = 10
int rightShift = a >> 1; // 0010 = 2
Assignment Operators
Assign values to variables.
- Simple assignment
=
: Assigns value - Addition assignment
+=
: Adds and assigns - Subtraction assignment
-=
: Subtracts and assigns - Multiplication assignment
*=
: Multiplies and assigns - Division assignment
/=
: Divides and assigns - Modulus assignment
%=
: Takes modulus and assigns - Bitwise assignment
&=
,|=
,^=
,<<=
,>>=
: Perform bitwise operation and assign
Example:
int x = 10;
x += 5; // Same as x = x + 5, x becomes 15
x -= 3; // Same as x = x - 3, x becomes 12
x *= 2; // Same as x = x * 2, x becomes 24
x /= 4; // Same as x = x / 4, x becomes 6
x %= 4; // Same as x = x % 4, x becomes 2
Other Operators
- Conditional/Ternary Operator
? :
: Three-operand operator that evaluates a condition and returns one of two valuescppint max = (a > b) ? a : b; // If a > b, returns a, otherwise returns b
- sizeof Operator: Returns the size of a variable or type in bytes
cpp
int size = sizeof(int); // Typically returns 4
- Comma Operator
,
: Evaluates multiple expressions and returns the value of the rightmost expressioncppint x = (a = 5, a + 2); // a becomes 5, x becomes 7
- Scope Resolution Operator
::
: Accesses identifiers in different scopescppstd::cout << "Using namespace explicitly";
- Member Access Operators
.
and->
: Access members of objects or pointers to objectscppstudent.name = "Priya"; // Using dot operator studentPtr->name = "Raj"; // Using arrow operator
Operator Precedence and Associativity
Operators have different precedence levels that determine the order of operations. When operators have the same precedence, associativity determines the order.
High to low precedence (partial list):
- Scope resolution
::
- Member access
.
,->
,[]
, function calls()
- Unary operators
++
,--
,~
,!
, unary+
and-
,sizeof
- Multiplication, division, modulus
*
,/
,%
- Addition, subtraction
+
,-
- Shift operators
<<
,>>
- Relational operators
<
,<=
,>
,>=
- Equality operators
==
,!=
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Logical AND
&&
- Logical OR
||
- Conditional operator
?:
- Assignment operators
=
,+=
,-=
, etc. - Comma operator
,
Example of precedence in action:
int result = 5 + 3 * 2; // Multiplication happens first, result is 11
int result2 = (5 + 3) * 2; // Parentheses override precedence, result is 16
Understanding data types and operators is fundamental to programming in C++. These concepts form the building blocks for more complex operations and algorithms that you’ll explore in future chapters.
Complete Chapter-wise Hsslive Plus One Computer Science Notes
Our HSSLive Plus One Computer Science Notes cover all chapters with key focus areas to help you organize your study effectively:
- Chapter 1 The Discipline of Computing
- Chapter 2 Data Representation and Boolean Algebra
- Chapter 3 Components of the Computer System
- Chapter 4 Principles of Programming and Problem Solving
- Chapter 5 Introduction to C++ Programming
- Chapter 6 Data Types and Operators
- Chapter 7 Control Statements
- Chapter 8 Arrays
- Chapter 9 String Handling and I/O Functions
- Chapter 10 Functions
- Chapter 11 Computer Networks
- Chapter 12 Internet and Mobile Computing