C++ Modifier Types:
C++ allows the char, int, and double data types to have modifiers preceding them. A modifier is used to alter the meaning of the base type so that it more precisely fits the needs of various situations.
The data type modifiers are listed here:
- signed
- unsigned
- long
- short
The modifiers signed, unsigned, long, and short can be applied to integer base types. In addition,signed and unsigned can be applied to char, and long can be applied to double.
The modifiers signed and unsigned can also be used as prefix to long or short modifiers. For exampleunsigned long int.
for example unsigned x;
unsigned int y;
Storage Classes in C++:
A storage class defines the scope (visibility) and life time of variables and/or functions within a C++ Program. These specifiers precede the type that they modify. There are following storage classes which can be used in a C++ Program
- auto
- register
- static
- extern
- mutable
C++ Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides following type of operators:
- Arithmetic Operators ( +, -, \, *, ++, --)
- Relational Operators (==, !=, >. <, >=, <=)
- Logical Operators (&&, ||, ! )
- Bitwise Operators (& |, ^, ~, <<, >>)
- Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
- Misc Operators ( sizeof, & cast, comma, conditional etc.)
C++ Loop Types:
C++ programming language provides the following types of loops to handle looping requirements. Click the following links to check their detail.
while loop | Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. syntax - while(condition) { body } |
for loop | Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. syntax - for(intialization;test condition;increment/decrement) { body } |
do...while loop | Like a while statement, except that it tests the condition at the end of the loop body syntax - do { body }while(condition); |
nested loops | You can use one or more loop inside any another while, for or do..while loop. syntax - for(intialization;test condition;increment/decrement) { for(intialization;test condition;increment/decrement) { body } } |
C++ Decision Making:
C++ programming language provides following types of decision making statements. Click the following links to check their detail.
Statement | Description |
---|---|
if statement | An if statement consists of a boolean expression followed by one or more statements. syntax - if(condition) { body } |
if...else statement | An if statement can be followed by an optional else statement, which executes when the boolean expression is false. syntax - if(condition) { body } else { body } |
switch statement | A switch statement allows a variable to be tested for equality against a list of values. syntax- switch(condition) { case1: { body }; break; case2: {body };break; default: { body } } |
nested if statements | You can use one if or else if statement inside another if or else if statement(s). syntax - if(condition) { if(condition) { body } } |
nested switch statements | You can use one swicth statement inside another switch statement(s). syntax- predict yourself |
C++ Functions:
A C++ function definition consists of a function header and a function body. Here are all the parts of a function:
- Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
- Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.
- Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
- Function Body: The function body contains a collection of statements that define what the function does.
Numbers in C++:
Following a simple example to show few of the mathematical operations on C++ numbers:
#include <iostream> #include <cmath> using namespace std; int main () { // number definition: short s = 45; int i = -100; long l = 100000; float f = 114.47; double d = 340.374; // mathematical operations; cout << "sin(d) :" << sin(d) << endl; cout << "abs(i) :" << abs(i) << endl; cout << "floor(d) :" << floor(d) << endl; cout << "sqrt(f) :" << sqrt(f) << endl; cout << "pow( d, 2) :" << pow(d, 2) << endl; return 0; }
sin is use to measure the sine value of the number
abs is the absolute value of the number
floor is the largest integer value less than or equal to the number
sqrt is the square root of the given number
pow is used to put the power onto the function(here d is the value and 2 is the power of it)
No comments:
Post a Comment