Showing posts with label C Plus Plus. Show all posts
Showing posts with label C Plus Plus. Show all posts

Sunday, 9 March 2014

What is Stream in C++?

Streams in C++


Streams:


A stream is an interface between the user and I/O devices. Streams flow the data between the user and I/O devices in bytes in sequence. Streams are divided into two types i.e. input streams and output streams. The input stream is also called source stream and the output stream is called destination stream. The input stream sends data from the input devices to computer system while the output stream sends data from computer system to the output devices. C++ provides various stream classes in which different member functions and  member data are declared for different input/output operations on the stream. The object cin of class istream is used for console input operations while the object cout of class ostream is used for console output operations on the stream. The out stream class ostream also provides two other objects clog and cerr. These objects control the error messages. The object sin is usually associated with the monitor. The I/O stream classes are declared in the header file iostream.h. So you must include his header file while using the I/O stream classes.



The Stream Extraction and Insertion Operators:


The stream extraction means reading data from the input devices while the stream insertion means sending data to output devices. The extraction operator >> is used for the stream extraction and the insertion operator << is used for the stream insertion. The stream extraction operation >> is implemented as a set of overloaded members of the istream class to support reading basic types of any input stream, while the stream insertion operator << is implemented as a set of overloaded members of the ostream class to support writing basic types of data from any any output stream. These operators read and write the data and ignore the white space character. A white space character is always a delimiter between values when using the stream extraction and insertion operators. The stream extraction operator when reads the string and during reading when it founds a white space character then it stops the reading process and ignores the next after the white space character. The stream extraction and insertion operators are used with the istream and ostream objects.


The cin Stream object:


This object is used for input. Whenever you want to send data to the variables from the keyboard then this object is a source through which you can send data to the variables.This object takes input values from the keyboard and sends them to the variables. The processor then processes the input of cin object and produces output. Following is the general syntax of cin stream object:

cin >> variable1 >> variable2 >> variable3 >> ------------------->>variable-n;

When you want to enter values to more variables using a single cin stream object then these variables should be distinguished from each other using the stream etraction operator >> . You can also use a separate cin stream object for each variable.


The cout stream object:


This object is used for output. Whenever you want to receive the output from the variables or to display your own message then for such purposes this object is used. This objects takes output values from the variables and send them to the standard output devices.
Following are the general syntaxes of cout stream object:

cout << variable1 << variable2 << variable3 << ------------------- << variable-n;
                                               or
cout << "message1" << "message2" << "message3" << ------------------- << "message-n";

Whenever you want to display a message through cout stream object then the message should be enclosed with double quotes and when you want to display value of variables then in such case the using of double quotes should be ignored because double quotes instruct cout stream object to display only this message enclosed with me. When you write the name of variable without double quotes then cout stream object displays the contents of that variable.

                                        Program # 1
Write a C++ Program to read two integer values from the keyboard and display them on the screen.


                                         Program
#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
cout<<”Enter the value for x and y =”;
cin>>x>>y;
cout<<”your values are =”;
cout<<x<<y;
getch();
}

Since for each variable we can also use a separate cin and cout stream objects. So the above program can also be written as:


#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
cout<<”Enter the value for x and y =”;
cin>>x;
ccin>>y;
cout<<”your values are =”;
cout<<x;
cout<<y;
getch();
}




Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

Friday, 7 March 2014

What is Expressions, Header files and Input and Output Operations

Expressions, Header files, Include Header files and Input and Output Operations



Expressions:

When operands and operators are combined with each other then they form an expression. An expression is the combination of different type of operators and operands. For Example


  •      x + y + z
  •      x * y / 2 + 6 - 1
  •      4 + 5 / 2 - 6 * 4
  •      ( 4 + 5 ) / 2 + 1 + ( 6 - 1 )
  •      C = x + y + z




In expression operators have their own priority. In an expression Brace has the first priority, so that part of an expression is first solved which are enclosed in the braces. The second priority is the Division or the Multiplication operators. These two operators have the same priority but the first coming will be solved first in an expression. The third and the last priority is the + and - operators. These two operators also have the same priority but the first coming operator will be solved first in an expression.

Header File:


Every programming language contains thousands of built-in functions. A function is a tool that is designed to perform a specific task. The functions are categorized into different files according to their similar nature and properties. For Example: string functions, input / output functions, math functions, graphic function, memory allocation and deallocation functions, container classes functions, disk input and output functions, data conversion function etc. These files are called header files. The functions in each header file have same nature and properties. The header files permanently stored in the built-in library  of programming languages. In Turbo C++ each header file is stored with .h extension while in ANSI standard of C++ they are stored without the .h extension. When a programmer uses any built-in function in the program then he/she must include the header file of that function at the top of the program or before the using of that function.

Include Header Files:


To include header files in the program a keyword include is used with the # hash operator. The hash # operator is used before the keyword include. The header file that is to be included is enclosed within < > operators or enclosed within double quotes. Following are general syntaxes to include header files in the program:

                              #include < Header file name > or #include" Header file name "

The keyword include is called preprocessor directive. This directive will be covered in detail later.

Input and Output Operations:


When we give some instructions ( data ) to computer system then it is called an input and when computer responds to us and gives some messages or information then it is called an output. The input operation is perform through input devices i.e. keyboard, mouse etc. while the output operation is perform through the output devices i.e. monitor, printer etc. So to control such operations every programming language provide a set of built-in Input/Output (I/O) functions. The built-in I/O functions work as streams and provide an interface between the user and Input/Output devices.

Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

Thursday, 6 March 2014

Constant Declaration and Memory representation of Data Types.

Constant Declaration and Memory representation of Data Types


Constant and its Declaration:


The value given to the variable is called constant. A constant value does not change during the program execution. A variable that remains hold its value during the program execution is called constant variable. In many programming numerical constants, and scientific formula constants are used, e.g. the value of Ï€ (pi) is always equal to 3.14, the value of gravitational acceleration "g" is always equal to 9.81, and the total number of days in a non leap year is always 365 etc. They are called constant values. A variable that holds a constant value is called constant variable. Constant variables can be declared by using a keyboard Const precede the data type of the variables and they should be initialised with values. Without initialisation a constant variable cannot be declared Following is the general syntax of constant variable declaration:


               const Data-type-name <Variable-name = value>;

In the above syntax Data-type-name is the data type of the variable you want to declare, Variable-name is the name of Variable. and the value is an expression involving only constant quantities that gives to the variable. Following are some examples of constant variables:

                                     const float Pi = 3.14;
                                     const int days_in_year = 365;
                                     const int month_in_year = 12;
                                     const char ch = 'c';
                                     const float  = 10.5;

The above variables are constant variables because they have initialised with constant values.







Limitation of variables ( data type ranges ) in C++:



Each variable has a maximum and minimum range for value. When a variable is declared then memory space is allocated to it in the form of bits. A bit is a memory unit representing a single binary digit 0 or 1. The variable minimum and maximum ranges are shown in the following table.

Data Type Name
Bytes
Minimum Value
Maximum Value
Char
1
-128
127
Unsigned char
1
0
255
Int
2
-32768
32767
Unsigned int
2
0
65535
Unsigned short int
2
0
65535
Long int
4
-2147483648
2147483647
Unsigned long int
4
0
2147483647
Float
4
-3.4E+38
3.4E+38
Double
8
-1.7E+308
1.7E+308
Long double
10
-1.7E+308
1.7E+308
Bool
1
False 0
True 1
Wchar_t
2

255 or greater

Memory Representation of Data Types:


The general formula that represents the maximum range of variable is 2"-1 while the general formula that represents the range of variable is -2"-1. Here n represent the total number of bits allocated in memory for  a declared variable.


  • Memory Representation of Integer Variables:

When you declared a variable as an integer then it takes 2 bytes ( 16 bits ) space in memory. The following table shows memory representation of integer variable.

       1    2   3    4     5     6    7    8    9    10   11  12  13  14   15  16 
Sign
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
Since the value may either positive or negative. The first bit holds the information about the entered value either the value is negative or positive. If the entered value is positive then this bit stores positive sign (+) and when the entered value is negative then this bit stores negative sign (-). The remaining 15 bits from 2 to 16 represent the magnitude of the data. the maximum and minimum ranges of an integer variable can be calculated by using the above two formula as.

Total Number of bits for Data
Maximum range of integer
Minimum range of integer
N = 15
215-1 = 32767
-215-1 = -32768
This memory representation shows that an integer variable can hold maximum value from 0 to 32767 and minimum value -1 to -32767. If you want to store data out of these ranges then in such case int data type will fail. For this purpose you will declare long int variable.

  • Memory Representation of Long Integer Variables:


The long integer variable takes 4 bytes ( 32 its ) space in memory. The following table shows the memory representation of long integer variable.
        
   1      2 3 4 5 6 7  8 9 10 --------------------------------26 27 28 29 30 31
Sign
/




















/
/
/
/
/
/
/
/
Here the first bits represent sign while the remaining 31 bits from 2  to 31 represent the magnitude of the data. The maximum and minimum ranges of long integer variable can be calculated by using the following formula.

Total number of bits for data
Maximum range of integer
Minimum range of integer
N = 31
231 -1 = 2147483647
-231 -1 =- 2147483648
The above memory representation shows that a long integer variable can hold the maximum value from 0 to 2147483647 and minimum value from -1 to -2147483648.

  • Memory Representation of Character Variables:


Character variable takes 1 byte ( 8 bytes ) space in memory. The following table shows the memory representation of a character variable.

           1 2  3 4  5 6  7 8 
Sign
/
/
/
/
/
/
/
/

The first bit represent sign while the remaining 7 bits from 2 to 8 represent the magnitude of the data. The maximum and minimum ranges of a character variable can be calculated by using the following formula.

Number of
bits
Maximum range of
 unsigned int
Minimum range of
unsigned int

    N = 1
   27 -1 = 127
     -27 -1 = -128

The above memory representation shows that a character variable can hold the maximum range of value up to 127 and minimum range value up to -128.

  • Memory Representation of Unsigned Integer Variables:


Unsigned means no sign for value. When you declared a variable as an unsigned then that variable will always hold a positive value. When you give the negative value to an unsigned variable then unsigned variable ignores the negative value and hold another default value. The entire bits of unsigned variable represent the magnitude of the data i.e. no bit will be allocated for sign purpose.
 1   2   3    4  5    6   7    8   9  10  11 12 13141516
/
/
/
/
/
/
/
/
/
/
/

/
/
/
/

Since in unsigned integer variable the entire 16 bits represent the magnitude of the data then the maximum and minimum ranges of unsigned integer can be calculated as:

Number of bits
Maximum range of
unsigned int
Minimum range of
unsigned int
     N = 16
       216 -1 = 65535
     0








Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com