Scroll Progress Bar

Operators

C++ provides a wide range of operators that allow to perform various operations on variables and values. These operators can be classified into several categories based on their functionality. Here are some of the most commonly used operators in C++:

Arithmetic Operators:
  • + (Addition): Adds two operands.
  • - (Subtraction): Subtracts the right operand from the left operand.
  • * (Multiplication): Multiplies two operands.
  • / (Division): Divides the left operand by the right operand.
  • % (Modulus): Computes the remainder of the division of the left operand by the right operand.
Assignment Operators:
  • = (Assignment): Assigns the value of the right operand to the left operand.
  • +=, -= , *= , /= , %=: Perform the specified operation and assign the result to the left operand.
Increment and Decrement Operators:
  • ++ (Increment): Increases the value of a variable by 1.
  • -- (Decrement): Decreases the value of a variable by 1.

These can be used as prefix (++x, --x) or postfix (x++, x--) operators, and their behavior differs slightly in each case.

Comparison Operators:
  • == (Equal to): Checks if two operands are equal.
  • != (Not equal to): Checks if two operands are not equal.
  • < (Less than): Checks if the left operand is less than the right operand.
  • > (Greater than): Checks if the left operand is greater than the right operand.
  • <= (Less than or equal to): Checks if the left operand is less than or equal to the right operand.
  • >= (Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.
Logical Operators:
  • && (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 truth value.
Bitwise Operators:
  • & (Bitwise AND): Performs a bitwise AND operation.
  • | (Bitwise OR): Performs a bitwise OR operation.
  • ^ (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.
  • ~ (Bitwise NOT): Inverts the bits of the operand.
Conditional (Ternary) Operator:

? : (Conditional Operator): Provides a concise way to write an if-else statement.

Program:

int x = (a > b) ? a : b;
Member Access Operators:
  • . (Dot Operator): Accesses members of an object or structure.
  • -> (Arrow Operator): Accesses members of an object through a pointer.
Sizeof Operator:
  • sizeof: Returns the size (in bytes) of a data type or object.
Program:

int size = sizeof(int);
Comma Operator:
  • , (Comma Operator): Allows multiple expressions to be evaluated sequentially, with the value of the last expression as the result.
Program:

int x = 5, y = 10, z;
z = (x++, y++);
These are some of the fundamental operators in C++. C++ also supports operator overloading, which allows to define custom behaviors for operators in own classes. Additionally, the standard library provides various overloaded operators for standard data types and containers, enhancing the language's flexibility and expressiveness.

question


answer

question2


answer2