OOPS: NOTES
1.OPERATORS IN C++
An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. In C++, we have built-in operators to provide the required functionality.
An operator operates the operands. For example,
int c = a + b;
Here, ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands that are being ‘added’.
Operators in C++ can be classified into 6 types:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
1) Arithmetic Operators
These operators are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘-‘ is used for subtraction ‘*’ is used for multiplication, etc.
Arithmetic Operators can be classified into 2 Types:
A) Unary Operators: These operators operate or work with a single operand. For example: Increment(++) and Decrement(–) Operators.
Name | Symbol | Description | Example |
---|---|---|---|
Increment Operator | ++ | Increases the integer value of the variable by one | int a = 5; a++; // returns 6 |
Decrement Operator | — | Decreases the integer value of the variable by one | int a = 5; a–; // returns 4 |
Example:
- C++
a++ is 10
++a is 12
b-- is 15
--b is 13
Time Complexity: O(1)
Auxiliary Space : O(1)
Note: ++a and a++, both are increment operators, however, both are slightly different.
In ++a, the value of the variable is incremented first and then It is used in the program. In a++, the value of the variable is assigned first and then It is incremented. Similarly happens for the decrement operator.
B) Binary Operators: These operators operate or work with two operands. For example: Addition(+), Subtraction(-), etc.
Name | Symbol | Description | Example |
---|---|---|---|
Addition | + | Adds two operands | int a = 3, b = 6; int c = a+b; // c = 9 |
Subtraction | – | Subtracts second operand from the first | int a = 9, b = 6; int c = a-b; // c = 3 |
Multiplication | * | Multiplies two operands | int a = 3, b = 6; int c = a*b; // c = 18 |
Division | / | Divides first operand by the second operand | int a = 12, b = 6; int c = a/b; // c = 2 |
Modulo Operation | % | Returns the remainder an integer division | int a = 8, b = 6; int c = a%b; // c = 2 |
Note: The Modulo operator(%) operator should only be used with integers.
Example:
- C++
a + b = 11
a - b = 5
a * b = 24
a / b = 2
a % b = 2
Time Complexity: O(1)
Auxiliary Space : O(1)
2) Relational Operators
These operators are used for the comparison of the values of two operands. For example, ‘>’ checks if one operand is greater than the other operand or not, etc. The result returns a Boolean value, i.e., true or false.
Name | Symbol | Description | Example |
---|---|---|---|
Is Equal To | == | Checks if both operands are equal | int a = 3, b = 6; a==b; // returns false |
Greater Than | > | Checks if first operand is greater than the second operand | int a = 3, b = 6; a>b; // returns false |
Greater Than or Equal To | >= | Checks if first operand is greater than or equal to the second operand | int a = 3, b = 6; a>=b; // returns false |
Less Than | < | Checks if first operand is lesser than the second operand | int a = 3, b = 6; a<b; // returns true |
Less Than or Equal To | <= | Checks if first operand is lesser than or equal to the second operand | int a = 3, b = 6; a<=b; // returns true |
Not Equal To | != | Checks if both operands are not equal | int a = 3, b = 6; a!=b; // returns true |
Example:
- C++
a == b is 0
a > b is 1
a >= b is 1
a < b is 0
a <= b is 0
a != b is 1
Time Complexity: O(1)
Auxiliary Space : O(1)
Here, 0 denotes false and 1 denotes true. To read more about this, please refer to the article – Relational Operators.
3) Logical Operators
These operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false.
Name | Symbol | Description | Example |
---|---|---|---|
Logical AND | && | Returns true only if all the operands are true or non-zero | int a = 3, b = 6; a&&b; // returns true |
Logical OR | || | Returns true if either of the operands is true or non-zero | int a = 3, b = 6; a||b; // returns true |
Logical NOT | ! | Returns true if the operand is false or zero | int a = 3; !a; // returns false |
Example:
- C++
a && b is 1
a ! b is 1
!b is 0
Time Complexity: O(1)
Auxiliary Space : O(1)
Here, 0 denotes false and 1 denotes true. To read more about this, please refer to the article – Logical Operators.
4) Bitwise Operators
These operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.
Name | Symbol | Description | Example |
---|---|---|---|
Binary AND | & | Copies a bit to the evaluated result if it exists in both operands | int a = 2, b = 3; (a & b); //returns 2 |
Binary OR | | | Copies a bit to the evaluated result if it exists in any of the operand | int a = 2, b = 3; (a | b); //returns 3 |
Binary XOR | ^ | Copies the bit to the evaluated result if it is present in either of the operands but not both | int a = 2, b = 3; (a ^ b); //returns 1 |
Left Shift | << | Shifts the value to left by the number of bits specified by the right operand. | int a = 2, b = 3; (a << 1); //returns 4 |
Right Shift | >> | Shifts the value to right by the number of bits specified by the right operand. | int a = 2, b = 3; (a >> 1); //returns 1 |
One’s Complement | ~ | Changes binary digits 1 to 0 and 0 to 1 | int b = 3; (~b); //returns -4 |
Note: Only char and int data types can be used with Bitwise Operators.
Example:
- C++
a & b is 4
a | b is 6
a ^ b is 2
a>>1 is 3
a<<1 is 12
~(a) is -7
Time Complexity: O(1)
Auxiliary Space : O(1)
To read more about this, please refer to the article – Bitwise Operators.
5) Assignment Operators
These operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.
Name | Symbol | Description | Example |
---|---|---|---|
Assignment Operator | = | Assigns the value on the right to the variable on the left | int a = 2; // a = 2 |
Add and Assignment Operator | += | First adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left | int a = 2, b = 4; a+=b; // a = 6 |
Subtract and Assignment Operator | -= | First subtracts the value on the right from the current value of the variable on left and then assign the result to the variable on the left | int a = 2, b = 4; a-=b; // a = -2 |
Multiply and Assignment Operator | *= | First multiplies the current value of the variable on left to the value on the right and then assign the result to the variable on the left | int a = 2, b = 4; a*=b; // a = 8 |
Divide and Assignment Operator | /= | First divides the current value of the variable on left by the value on the right and then assign the result to the variable on the left | int a = 4, b = 2; a /=b; // a = 2 |
Example:
- C++
a = 6
a += b is 10
a -= b is 6
a *= b is 24
a /= b is 6
Time Complexity: O(1)
Auxiliary Space : O(1)
6) Ternary or Conditional Operators(?:)
This operator returns the value based on the condition.
Expression1? Expression2: Expression3
The ternary operator ? determines the answer on the basis of the evaluation of Expression1. If it is true, then Expression2 gets evaluated and is used as the answer for the expression. If Expression1 is false, then Expression3 gets evaluated and is used as the answer for the expression.
This operator takes three operands, therefore it is known as a Ternary Operator.
Example:
- C++
The greatest number is 4
Time Complexity: O(1)
Auxiliary Space : O(1)
7) There are some other common operators available in C++ besides the operators discussed above. Following is a list of these operators discussed in detail:
A) sizeof Operator: This unary operator is used to compute the size of its operand or variable.
sizeof(char); // returns 1
B) Comma Operator(,): This binary operator (represented by the token) is used to evaluate its first operand and discards the result, it then evaluates the second operand and returns this value (and type). It is used to combine various expressions together.
int a = 6; int b = (a+1, a-2, a+5); // b = 10
C) -> Operator: This operator is used to access the variables of classes or structures.
cout<<emp->first_name;
D) Cast Operator: This unary operator is used to convert one data type into another.
float a = 11.567; int c = (int) a; // returns 11
E) Dot Operator(.): This operator is used to access members of structure variables or class objects in C++.
cout<<emp.first_name;
F) & Operator: This is a pointer operator and is used to represent the memory address of an operand.
G) * Operator: This is an Indirection Operator
- C++
a = 6 b = 0x7ffe8e8681bc c = 6
H) << Operator: This is the output operator. It prints the output value.
I) >> Operator: This is the input operator. It gets the input value.
int a; cin>>a; cout<<a;
Time Complexity: O(1)
Auxiliary Space : O(1)
Operator Precedence Chart
Precedence | Operator | Description | Associativity |
---|---|---|---|
1. | () | Parentheses (function call) | left-to-right |
[] | Brackets (array subscript) | ||
. | Member selection via object name | ||
-> | Member selection via a pointer | ||
++/– | Postfix increment/decrement | ||
2. | ++/– | Prefix increment/decrement | right-to-left |
+/- | Unary plus/minus | ||
!~ | Logical negation/bitwise complement | ||
(type) | Cast (convert value to temporary value of type) | ||
* | Dereference | ||
& | Address (of operand) | ||
sizeof | Determine size in bytes on this implementation | ||
3. | *,/,% | Multiplication/division/modulus | left-to-right |
4. | +/- | Addition/subtraction | left-to-right |
5. | << , >> | Bitwise shift left, Bitwise shift right | left-to-right |
6. | < , <= | Relational less than/less than or equal to | left-to-right |
> , >= | Relational greater than/greater than or equal to | left-to-right | |
7. | == , != | Relational is equal to/is not equal to | left-to-right |
8. | & | Bitwise AND | left-to-right |
9. | ^ | Bitwise exclusive OR | left-to-right |
10. | | | Bitwise inclusive OR | left-to-right |
11. | && | Logical AND | left-to-right |
12. | || | Logical OR | left-to-right |
13. | ?: | Ternary conditional | right-to-left |
14. | = | Assignment | right-to-left |
+= , -= | Addition/subtraction assignment | ||
*= , /= | Multiplication/division assignment | ||
%= , &= | Modulus/bitwise AND assignment | ||
^= , |= | Bitwise exclusive/inclusive OR assignment | ||
<>= | Bitwise shift left/right assignment | ||
15. | , | expression separator | left-to-right |
2.DATA TYPES
All variables use data type during declaration to restrict the type of data to be stored.
- Data types are used to tell the variables the type of data they can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared.
- Every data type requires a different amount of memory.
- Data types specify the size and types of values to be stored.
C++ supports the following data types:
- Primary or Built-in or Fundamental data type
- Derived data types
- User-defined data types
Data Types in C++ are Mainly Divided into 3 Types:
1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool, etc. Primitive data types available in C++ are:
- Integer
- Character
- Boolean
- Floating Point
- Double Floating Point
- Valueless or Void
- Wide Character
2. Derived Data Types: Derived data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely:
- Function
- Array
- Pointer
- Reference
3. Abstract or User-Defined Data Types: Abstract or User-Defined data types are defined by the user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes:
- Class
- Structure
- Union
- Enumeration
- Typedef defined Datatype
Primitive Data Types
- Integer: The keyword used for integer data types is int. Integers typically require 4 bytes of memory space and range from -2147483648 to 2147483647.
- Character: Character data type is used for storing characters. The keyword used for the character data type is char. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255.
- Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can store either true or false. The keyword used for the Boolean data type is bool.
- Floating Point: Floating Point data type is used for storing single-precision floating-point values or decimal values. The keyword used for the floating-point data type is float. Float variables typically require 4 bytes of memory space.
- Double Floating Point: Double Floating Point data type is used for storing double-precision floating-point values or decimal values. The keyword used for the double floating-point data type is double. Double variables typically require 8 bytes of memory space.
- void: Void means without any value. void data type represents a valueless entity. A void data type is used for those function which does not return a value.
- Wide Character: Wide character data type is also a character data type but this data type has a size greater than the normal 8-bit data type. Represented by wchar_t. It is generally 2 or 4 bytes long.
- sizeof() operator: sizeof() operator is used to find the number of bytes occupied by a variable/data type in computer memory.
Example:
int m , x[50];
cout<<sizeof(m); //returns 4 which is the number of bytes occupied by the integer variable “m”.
cout<<sizeof(x); //returns 200 which is the number of bytes occupied by the integer array variable “x”.
The size of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
- C++
Size of char : 1
Size of int : 4
Size of long : 8
Size of float : 4
Size of double : 8
Time Complexity: O(1)
Space Complexity: O(1)
Comments