Search This Blog

Pages

Friday, January 21, 2011

Initial of Lab Task 1

----------------------------------------------------------
Written by Sagar Giri

----------------------------------------------------------


OBJECT: BASIC PROGRAMMING ON C++

THEORY:

Output operator
cout<<

            The identifier cout is a predefined object that represents the standard output stream in C++. The operator << is called the insertion or put to operator. In inserts (or sends) the contents of the variable on its right to the object on its left. If string represents a string variable then the following statement will display its contents.

            cout<<string;

E.g.:


#include<iostream.h>

void main()

{

            char msg[]="Assignment 1";

            cout<<msg;

Output:

Assignment 1

}

Input Operator
cin>>

            The identifier cin is a predefined object in C++ that is correspond to the statement input stream. The operator >> is known as extraction or get from operator. It extracts (or takes) the value from the keyboard it to the variable on its right.

cin>>number1;

E.g.:

#include<iostream.h>

int main()

{

            cout << "Enter two numbers: "<<endl;

            int a,b;

            cin >>a>>b;

            int sum=a+b;

            cout <<"The sum is: "<<sum;

            return 0;

}

Output:

Enter two numbers: 5 7

The sum is: 12

endl;
It is an identifier which is predefined object in C++ that is correspond to run the program to the next line.

E.g.:

#include<iostream.h>

int main()

{

            int sum=12;

            cout <<"The final result is: "<<endl;

            cout <<sum;

            return 0;

}

Output:

The final result is:

12

Here the statement "The final result is: " displayed in first line and the sum 12 is displayed in next line in the screen.

New Operator:
The new operator offers dynamic memory allocation similar to the standard library function malloc() or calloc(). The new operator obtains memory at runtime from the memory heap from the operating system and returns the address of the obtained memory. The new operator is used as follows:

            data_type *data_type_ptr;

            data_type_ptr=newdata_type; //allocates single variable

            data_type_ptr=newdata_type[size]; //allocates an array

For example:

            int  *iptr;

            iptr=new int; //allocates space for single integer

iptr=new int[n]; //allocates space for an array of integer with a elements

Delete Operator
The delete operator is used to release the memory allocated by the new operator back to the operating system memory heap. Memory release by the use of delete operator will be refused by other parts of the program. The delete operator destroy the dynamic variable created at the run time. The delete operator is used as follows:

delete data_type ptr; //release a single dynamic variable

delete[] data_type ptr; //release dynamically created array.

For e.g.:

int *ptr;

iptr=new int[5];

------------------

delete iptr;

Enumeration
An enumerated data type is another user defined data type which provides a way for attaching names to numbers, thereby increasing comprehensibility of the code. It provides a means of naming a finite set and a declaring identifiers as elements of the set. The format for defining a enumeration type is:

            enum_day{sun,mon,tue,wed,thu,fri,sat};

E.g.:

enum shape{circle,square,rectangle};

int main()

{

            cout <<"Enter shape code: ";

            cin >>int code;

            while(code>=circle && code<=rectangle)

            {

                        switch(code)

                        {

                                    case circle:

                                    { ……………

                                    break;}
                                    case square:

                                    { ……………

                                    break;}
                                    case rectangle:

                                    { ……………

                                    break;}

                        }

            }

return 0;

}

#define (pre-processer)
#define is a preprocessor which assign a constant value to a variable so that the value of variable will always same throughout the program.

E.g.:

#include<iostream.h>

#define PI 3.14

int main()

{

            cout <<"Enter the radius of the circle"<<endl;

            cin >>int r;

            int area=PI*r*r;

            cout <<"The area of the circle is: "<<endl;

            cout <<area;

return 0;

}


----------------------------------------------------------
By Bishal Jamarkattel
----------------------------------------------------------


Symbolic constant:

            Symbolic constant is a name that substitutes for a sequence of characters. A symbolic constant allows a name to appear in place of a numeric constant, a character constant or a string.

            There are three ways of creating symbolic constant.
  • Preprocessor directives
The line beginning with hash (#) sign are called preprocessor directives. Pre-processor directive are instruction to complier.

E.g.: #define PI 3.14
  • Using the qualifier constant
Qualifiers are keyword which are used to modify or extend data types.
  • By defining a se of integer constant using enum keyword.

Manipulator:

      Manipulators are instruction to I/O stream that modify the output in various ways. The header file <iomanip.h> must be included to use standard manipulator.

Commonly used manipulators are:

setw(int w): Set the field width to w.

setprecision(int d): Set the floating point precision to d.

setfill(int c): Set the fill character to c.

setiosflags(longf): Set the format flag f.

resetiosflags(longf): Clear the flag specified by f.

endl: Insert new line and flush stream.
Dynamic Memory Allocation (DMA):

      A technique of allocating memory during runtime on demands is known as DMA. The memory management function such as malloc() and free() in C have been improved and evolved in C++ as new and delete operator to accomplish dynamic memory allocation and de-allocation respectively.
new operator: The new operator obtains memory at runtime from the memory heap form the operating system and returns the address of obtained memory.

new operator is used as follows:

data_type *data_type_ptr;

data_type_ptr=new data_type; //allocates single variable.

data_type_ptr=new datatype[size]; //allocates an array.
delete operator:

      The delete operator is used to release the memory allocated by the new operator back to the operating system memory heap. Memory released by the use of delete operator will be reused by other part of the program.

delete data_type_ptr; //release a single dynamic variable.

delete [] data_type_ptr; //release dynamically created variable.

No comments:

Post a Comment