Thursday, May 9, 2019

Array

Arrays

 An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

 for example:

we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier.
Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.

For example: an array to contain 5 integer values of type int called billy could be represented like this:
   
 Image7

where each blank panel represents an element of the array, that in this case are integer values of type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length.
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [elements];
where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array has to contain. Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:
   int billy [5];
NOTE: The elements field within brackets [] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.

Initializing arrays:

When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros.
In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }.

For example:

int billy [5] = { 16, 77, 40, 120,71 }; 


 double billy[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}


 declaration

lets see how we declare the arrays 

 



Friday, May 3, 2019

Types of function


Types of function

We have two types of function in C++:
1) Built-in functions
2) User-defined functions

1) Build-it functions

Built-in functions are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath etc. We can directly call them when we need.

Example: C++ built-in function example

Here we are using built-in function pow(x,y) which is x to the power y. This function is declared in cmath header file so we have included the file in our program using #include directive.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
    /* Calling the built-in function 
     * pow(x, y) which is x to the power y
     * We are directly calling this function
     */
    cout<<pow(3,2);
    return 0;
}

Output:
9

2) User-defined functions


We have already seen user-defined functions, the example we have given at the beginning of this tutorial is an example of user-defined function. The functions that we declare and write in our programs are user-defined functions. Lets see another example of user-defined functions.

User-defined functions

#include <iostream>
#include <cmath>
using namespace std;
//Declaring the function sum
int sum(int,int);

int main(){
   int x, y;
   cout<<"enter first number: ";
   cin>> 

Types of function

We have two types of function in C++:
1) Built-in functions
2) User-defined functions

1) Build-it functions

Built-in functions are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath etc. We can directly call them when we need.

Example: C++ built-in function example

Here we are using built-in function pow(x,y) which is x to the power y. This function is declared in cmath header file so we have included the file in our program using #include directive.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
    /* Calling the built-in function 
     * pow(x, y) which is x to the power y
     * We are directly calling this function
     */
    cout<<pow(2,3);
    return 0;
}

Output:
8

2) User-defined functions


We have already seen user-defined functions, the example we have given at the beginning of this tutorial is an example of user-defined function. The functions that we declare and write in our programs are user-defined functions. Lets see another example of user-defined functions.

User-defined functions

#include <iostream>
#include <cmath>
using namespace std;
//Declaring the function sum
int sum(int,int);

int main(){
   int a, b;
   cout<<"enter first number: ";
   cin>> a;

   cout<<"enter second number: ";
   cin>>b;

   cout<<"Sum of these two :"<<sum(a,b);
   return 0;
}
//Defining the function sum
int sum(int x, int y) {
   int c = x+y;
   return c;
Output:
enter first number: 22
enter second number: 19
Sum of these two :41

Thursday, May 2, 2019

Operators in 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 provide the following types of operators:
• Arithmetic Operators
• Relational Operators 
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Arithmetic Operators:

Operations of addition, subtraction, multiplication and division literally correspond with their respective mathematical operators. The only one that you might not be so used to see is modulo; whose operator is the percentage sign (%). Modulo is the operation that gives the remainder of a division of two values. For example, if we write
a = 11 % 3; 
the variable a will contain the value 2, since 2 is the remainder from dividing
 11 between 3.
 Ther are following arithmetic operators supported by c++: 


Picture

Relational operater:-

 Inorder to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result.




 
Operator

Use  

Description
>a > bReturns true if a is greater than b.
>=a>=b Returns true if a is greater than or equal to b.
<a < bReturns true if a is less than b.
<=a <=bReturns true if a. is less than or equal to b.
==a==bReturns true if a and b are equal.
!=a!=bReturns true if a and b are not equal



Logical operators ( !, &&, || )

 The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.

Example of Logical Operators

#include <iostream>
using namespace std;
int main(){
   bool b1= true;
   bool b2= false;
   cout<<"b1 && b2: "<<(b1&&b2)<<endl;
   cout<<"b1 || b2: "<<(b1||b2)<<endl;
   cout<<"!(b1 && b2): "<<!(b1&&b2);
   return 0;
}
Output:
b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1


Bitwise Operators ( &, |, ^, ~, <<, >> ) 

Bitwise operators modify variables considering the bit patterns that represent

Picture


Assignment (=)

The assignment operator assigns a value to a variable. a = 5;
This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these.

The most important rule when assigning is the right-to-left rule: The assignment operation always takes place from right to left, and never the other way:
a = b;
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value that was stored until this moment in a is not considered at all in this operation, and in fact that value is lost.

Miscellaneous Operators

There are few other operators in C++ such as Comma operator and sizeof operator. We will cover them in detail in a separate tutorial.

Operator Precedence in C++

This determines which operator needs to be evaluated first if an expression has more than one operator. Operator with higher precedence at the top and lower precedence at the bottom.
OicPictu


Data types in c++

Data types in c++

When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.



User-defined data types

We have three types of user-defined data types in C++
1. struct
2. union
3. enum
Click here to complete toturial for user defined data types.



Derived data types in C++

We have three types of derived-defined data types in C++
1. Array
2. Function
3. Pointer
Click here to complete toturial for Derived data types.

Variable in c++

Variable in C++

 Variable is define as a name which is associated with a value that can be changed. Variable are used to store the data into it.  

 For example:

Let us think that I ask you to retain the number 5 in your mental memory, and then I ask you to memorial also the number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. Values that we could now for example subtract and obtain 4 as result.
The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in C++ with the following instruction set:
                      a = 5;              
                      b = 2;              
                     a = a + 1; .       
                      result = a - b;



Syntax of declaring a variable in C++

data_type variable1_name = value1, variable2_name = value2;
For example:
                int a=5, b=2
We can also write it like this:
int a,b;
a=5;
b=2;

Types of variables


Variables can be categorised based on their data type. For example, in the above example we have seen integer types variables.

Following are the types of variable  available in C++.
• int type variable
• float type variable
•double type variable
•char type variable
•string type variable


Types of variables based on their scope

Before going in details of data type lets discuss what is scope first. When we discussed the Hello. World Program, we have seen the curly braces in the program like this:-
#include<iostream>

using namespace std;

int main()
{
   
   cout<<"Hello World!";

   return 0;
}


Any variable declared inside these curly braces have scope limited within these curly braces, if you declare a variable in main() function and try to use that variable outside main() function then you will get compilation error.

There are two types of variables based on scope:


Local variable: Local variable is one declared within the body of a function or a block.







Global variable:   global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a program.





Hello world:-the first program of c++

First program of c++

//My first program
#include<iostream.h>
 using namespace std;
int main() 
{
cout << "Hello World"; 
return 0;
}
Output:
Hello world 

// my first program in C++ 


This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.

#include <iostream> 

Line beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

using namespace std; 


All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.

Int main()

This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed

cout << "Hello World!"; 

This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. 
cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (which usually is the screen).
cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.
Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).

return 0; 

The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Difference between procedural programming and object oriented programming

Traditional Procedural Programming


In traditional, procedural programming, data and functions (subroutines, procedures) are
kept separate from the data they process. This has a significant effect on the way a pro-
gram handles data:
■ the programmer must ensure that data are initialized with suitable values before
use and that suitable data are passed to a function when it is called
■ if the data representation is changed, e.g. if a record is extended, the correspon-
ding functions must also be modified.
Both of these points can lead to errors and neither support low program maintenance
requirements.


Objects-oriented programming 


Object-oriented programming shifts the focus of attention to the objects, that is, to the
aspects on which the problem is centered. A program designed to maintain bank
accounts would work with data such as balances, credit limits, transfers, interest calcula-
tions, and so on. An object representing an account in a program will have properties
and capacities that are important for account management.
OOP objects combine data (properties) and functions (capacities). A class defines a
certain object type by defining both the properties and the capacities of the objects of
that type. Objects communicate by sending each other “messages,” which in turn acti-
vate another object’s capacities.

Advantages of OOP

Object-oriented programming offers several major advantages to software development:
■ reduced susceptibility to errors: an object controls access to its own data. More
specifically, an object can reject erroneous access attempts
■ easy re-use: objects maintain themselves and can therefore be used as building
blocks for other programs
■ low maintenance requirement: an object type can modify its own internal data
representation without requiring changes to the application.                                                               


      previous                                                      Next          

Array

Arrays  An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by ad...