Sunday, 31 July 2016

HOW TO START A PROGRAM ?

               DECLARATION AND INITIALIZATION


Variable must be declared before they are used .usually it is preferred to declare them at the starting of the program,but in C++ they can be declared in the middle of program too,must be done before using them .

EXAMPLE:
  
int i;               //declared but not initialized
char c;
int i, j, k; n                   //multiple declaration

initialization means assigning value to an already declared variable,

int i;                        //declaration
i=20;                // initialization

initialization and declaration can be done in one single step also,

int i=20;                    // initialization and declaration in same step
int=20, j=56;

if a variable is declared and not initialized by default it will hold a garbage value also,if a variable is once declared and if try to declare it again ,we will get a compile time error.

int i,j;
i=20;
j=56;
int j=i+j;     // compile time error,cannot redeclare a variable in same scope


PROGRAM TO FIND THE SIZE OF VARIOUS DATA TYPES

# include<iostream.h>
using namespace std;
int main()
{
    cout<<"size of char:"<<sizeof(char)<<endl;
    cout<<"size of int:"<<sizeof(int)<<endl;
    cout<<"size of short int:"<<sizeof(short int)<<endl;
    cout<<"size of  long int:"<<sizeof(long int)<<endl;
    cout<<"size of float:"<<sizeof(float)<<endl;
    cout<<"size of double:"<<sizeof(double)<<endl;
    cout<<"size of wchar_t:"<<sizeof(wchar_t)<<endl;
    return 0;
}

In this example :
1)       endl       = endl is use to insert a new line character after every line .
2) << operator= this is being used to pass multiple values out to the screen .
3)     sizeof       = this is used to get size of various data types.

*when you execute the above program you will get the following  result.
 size of char : 1
 size of  int : 4
 size of  short int : 2
 size of  long int : 4
 size of float : 4
 size of double : 8
 size of wchar_t : 4 

Data types in c++

DATA TYPES

A data type determines the type and the operations that can be performed on the data.They are used to define types of variables and contents used . Data type define the way you use storage in the programs you write.C++ provides various data types and each data type is represented differently within the computer's memory. the various data types provided by C++ are BUILT-IN data types,DERIVED data types and USER-DEFINED data types as shown in figure .




BUILT-IN DATA TYPES


The basic (fundamental) data types provided by c++ are integral,floating point and void data type.Among these data types, the integral and floating-point data types can be preceded by several type modifiers.These modifiers(also known as type qualifiers ) are the keywords that alter either size or range or both of the data types .The various modifiers are short ,long,signed and unsigned .By default the modifier is signed.
In addition to these basic data types ,ANSI C++ has introduced two more data types namely,bool and wchar_t.
                                                                    OR
THESE ARE THE DATA TYPES WHICH ARE PREDEFINED AND ARE WIRED DIRECTLY INTO THE COMPILER.eg: int,char etc.



Integral Data Type :The integral data type is used to store integers and includes char (character) and int (integer) data types.

CHAR:Character refer to the alphabet ,numbers and other, characters (such as {,@,#,etc) defined in the ASCII character set .In C++, the char data type is also treated as an integer data type as the characters are internally stored as integers that range in value from -128 to 127. The char data type occupies 1 byte of memory (that is, it holds only one character at a time ).

The modifiers that can precede char are signed and unsigned .The various character data type with their size and range are listed in above table.

INT:Numbers without the fractional part represent integer  data . In C++ , the int data  type is used to store integers such as 4,42,5233,-55,-759. Thus,it cannot store numbers such as 4.29,-586,-65.2579.The various integer data types with their size and range are listed below



FLOATING-POINT DATA TYPE:A floating -point data type is used to store real numbers such as 3.98,12.345,0.25,-45.85.This data type includes float and double' data types. The various floating-point data types with their size and range are listed below.



VOID:The void data type is used for specifying an empty parameter list to a function and return type for a function . When void is used to specify an empty parameter list,it indicates tha a function does not take any argument and when it is used as a return type for a function , it indicates that a function does not return any value .For void, no memory is allocated and hence, it cannot be used to declare simple variables, however, it can be  used to declare generic pointers.

BOOL AND WCHA_T: The bool data type can be held only boolean values,that is;either true or false, where true represents 1 and false represents 0. It requires only one bit of storage ,however,it is also considered as an integral data type .The bool data type is mostly commonly used for expressing the result of logical operations performed on the data.It ia also used as a return type of a function indicating the success or the failure of the function.

In addition to char data type ,C++ provides another data   type wchar_pt which is used to store 16- bit wide characters.Wide characters are used to hold large character sets associated with some non-english languages.

DERIVED DATA TYPES: Data types that are derived from the built-in data types are known as derived data types .the various derived data types provided by c++ are arrays,junction,references and pointers . 
                                                                      
ARRAY: An array is a set of elements of the same data type that are referred to by the same name.All the elements in an array are stored at contiguous(one after another) memory locations and each  element is accessed by a unique index or subscript value. the subscript value indicates the position of in an array.

FUNCTION:A function is a self-contained program segment that carries out a specific well-defined task. In  C++, every program contains one or more funcyions which can be invoked from other parts of a program ,if required.

REFERENCE:A reference is an alternative name for a variable .that is ,a reference is an alias for a variable in a program .A variable and its reference can be used interchangeably in a program as both refer to the same memory locations . Hence, changes made to any of them (say,a variable) are reflected int the  other (on reference).

POINTER:A pointer is a variable that can store the memory address of another variable. pointers allow to use the memory dynamically. That is, with the help allocated of pointers , memory can be allocated or de-allocated to the variables at run-time , thus,making a program more efficient.

Saturday, 30 July 2016

Basic of C++

In this section we will cover the basic of c++,it will include the syntax ,variable,operators,loop types,pointers,references and information about other requirements of a c++ program.you will come across lot of terms that you have already studied in c language.

syntax and structure of c++ program

here we will discuss one simple and basic c++ program to print "HELLO " and  its structure in parts with details and uses .

first c++ program

#include<iostream.h>
using namespace std;
int main ()
{
cout<<"HELLO";

}  

HEADER FILES are included at the beginning just like in c program.A header file is a file with extension .h which contain c function declaration and macro definition to be shared between several source files.here iostream is a header file which provide us with input and output streams. Header files contained predeclared function libraries, which can be used by user for their ease.

USING NAMESPACE STD,tells the compiler to use standard namespace.Namespace collects identifier used for class,object and variables .Namespace can be used by two ways in a program,either by the use of using statement at the beginning , like we did in above mentioned program or by using name of namespace as prefix before the identifier with scope resolution (::) operator.

EXAMPLE- STD::COUT<<"A";

MAIN(),function is the entry point of execution in c++.its where the execution begins .it has got two argument like argc and argv which are command line argument accepted by the final exe.Basically main() is the function which holds the executing part of program its return type is  int.

COUT<<, is used to print anything on screen ,same as printf in c language .cin and cout are same as scanf and printf,only difference is that  you do not need to mention format specifiers like %d for int etc,in cout & cin