Tuesday, 2 January 2018

C++

C++ Tutorial

 

C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.

C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features.

C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983.

C++ is a superset of C, and that virtually any legal C program is a legal C++ program.

C++ Compiler:

This is actual C++ compiler, which will be used to compile your source code into final executable program.

Most C++ compilers don't care what extension you give your source code, but if you don't specify otherwise, many will use .cpp by default

Most frequently used and free available compiler is GNU C/C++ compiler, otherwise you can have compilers either from HP or Solaris if you have respective Operating Systems.

C++ Program Structure:

Let us look at a simple code that would print the words Hello World.

#include <iostream> using namespace std; // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; }

Comments in C++

C++ supports single line and multi-line comments. All characters available inside any comment are ignored by C++ compiler.

C++ comments start with /* and end with */. For example:

/* This is a comment */ /* C++ comments can also * span multiple lines */

A comment can also start with //, extending to the end of the line. For example:

#include <iostream> using namespace std; main() { cout << "Hello World"; // prints Hello World return 0; }

C++ Primitive Built-in Types:

C++ offer the programmer a rich assortment of built-in as well as user-defined data types. Following table list down seven basic C++ data types:

TypeKeywordBooleanboolCharactercharIntegerintFloating pointfloatDouble floating pointdoubleValuelessvoidWide characterwchar_t

Variable Definition & Initialization in C++:

Some examples are:

extern int d, f // declaration of d and f int d = 3, f = 5; // definition and initializing d and f. byte z = 22; // definition and initializes z. char x = 'x'; // the variable x has the value 'x'.

C++ Variable Scope:

A scope is a region of the program and broadly speaking there are three places where variables can be declared:

Inside a function or a block which is called local variables,

In the definition of function parameters which is called formal parameters.

Outside of all functions which is called global variables.

C++ Constants/Literals:

Constants refer to fixed values that the program may not alter and they are called literals.

Constants can be of any of the basic data types and can be divided in Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Again, constants are treated just like regular variables except that their values cannot be modified after their definition.

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 example unsigned long int.

C++ allows a shorthand notation for declaring unsigned, short, or longintegers. You can simply use the word unsigned, short, or long, without the int. The int is implied. For example, the following two statements both declare unsigned integer variables.

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.

Loop TypeDescriptionwhile loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.for loopExecute a sequence of statements multiple times and abbreviates the code that manages the loop variable.do...while loopLike a while statement, except that it tests the condition at the end of the loop bodynested loopsYou can use one or more loop inside any another while, for or do..while loop.

C++ Decision Making:

C++ programming language provides following types of decision making statements. Click the following links to check their detail.

StatementDescriptionif statementAn if statement consists of a boolean expression followed by one or more statements.if...else statementAn if statement can be followed by an optional else statement, which executes when the boolean expression is false.switch statementA switch statement allows a variable to be tested for equality against a list of values.nested if statementsYou can use one if or else if statement inside another if or else if statement(s).nested switch statementsYou can use one switch statement inside another switch statement(s).

C++ Functions:

The general form of a C++ function definition is as follows:

return_type function_name( parameter list ) { body of the function }

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 = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.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; }

C++ Arrays:

Following is an example, which will show array declaration, assignment and accessing arrays in C++:

#include <iostream> using namespace std; #include <iomanip> using std::

No comments:

पोक्सो (POCSO)

चर्चा का कारण हाल ही में राज्यसभा ने यौन अपराधों से बच्चों का संरक्षण (संशोधन) विधेयक, 2019 {POCSO (Amendment) Bill, 2019} को मंजूरी प्रदान ...