Saturday, 1 August 2015

Basics of C++

         Revision C++
                                            By
                          Mr.Harmandeep Singh
                                        &
                               Miss.Ankita Pabbi

          Introduction to C++

Developed at AT & T Bell Lab
By Bjarne Stroustrup  


Keywords
Reserved word or system defined words. e.g.: int, long, do, while, static, public, for, if, new, delete, friend, const, auto, void etc

Identifiers
An identifier is the name given by user for a unit of the program. E.g.: variable name, array name, function name, class name etc.



               Literals
 Data items that never change during the execution of program.

Types of literals:
       Integer : e.g. : 12, 8, 78
       Float : 2.5, 6.9
       Character: ‘a’ , ‘c’
       String: “anku”, “a”
       Back Slash constant: \n \t \b \a


                Punctuators  

{ } [ ] ( ) , ; : * = # 


                 Operators

       Arithmetic operators: +  * / %
       Relation operators: < > <= >=  == !=
       Increment / Decrements: ++ --
       Logical Operators: &&, ||,! (and, or, not)
       Conditional: ? :
       Shorthand: +=, -=, /=, *=
       Ex- a=a+b; can be write as a+=b;



           Data Types

       Fundamental data types
       Derived data types



              Fundamental Data types
       int
       char
       float
       double
                  
                      Data type modifiers
       signed
       unsigned
       long
       short


           Derived data type

       Arrays
       Function
       Pointer
       Reference etc.

     User Defined derived data type

       Class
       Structure
       Union
       Enumeration

              Variable
       A name of storage location is called variable.

         Declaration of Variables

int  x, y, z;
int  a=10;
float  p,q,r;
float  z=3.5;
char a, ans=‘y’;
char name[20];
double f;


         Declaration of constants

const int MAX = 100;
const double PI = 3.14;
const char A = ‘n’;
const char name[] = “Ashok”;



          Symbolic constant
#define PI 3.14
#define MAX 100

                 
                Header Files
       iostream.h -> cin, cout
       stdio.h -> printf(), scanf()
       conio.h -> getch(), clrscr()
       string.h -> strcpy(), strcat(), strlen(), strcmp(), strlwr(), strupr()
       ctype.h -> isdigit(), isalpha(), ….
       math.h -> pow(), sqrt()

           
               I/O statements
       cout -> output
       cin   -> input
Ex-
int x;
cout<<“Enter x:”<<endl;
cin>>x;
cout<<“You entered:”<<x;



                  Character I/O
       getchar()
       putchar()
       getch()
       getche()
       get()
       put()
       getline()
       read()
       write()



Single statement vs compound
Statement
       Single statement write simple manner
       Compound statement is enclosed in { }

        Control Structure
       Sequence
       Selection
       looping


         SELECTION
       simple if
       if else
       if else if ladder
       switch
               if statement
Syntax:
 if (condition )
     expression;
Ex-
if (age<21)
cout<<“You are not eligible for marriage”;

              if else
Syntax:-
if( condition )
     expression1;
else
     expression2;

Ex:-
if( age<=21 )
     cout<<“ You are eligible for marriage”;
else
      cout<<“ You are not eligible for marriage”;

       if else if ladder statement
if(condition)
     Expression1;
else if(condition)
     Expression2;
else if(condition)
     Expression3;
…….
else
     Default Expression;
if(per>=75)
     cout<<“honor”;
else if(per>=60)
      cout<<“First”;
else if(per>=45)
      cout<<“Second”;
else if(per>=33)
      cout<<“Pass”;
else
     cout<<“Fail”;

         Switch Statement
switch(index)
{
case1:statement1; break;
case2:statement2; break;
case3:statement3; break;
…........
default;
 default statement;
}

                  Switch contd.
switch(choice)
{
case 1: c=a+b; break;
case2: c=a-b; break;
case3: c=a*b; break;
case4: c=a/b; break;
default:
  cout<<”invalid choice”;
}


   Looping Statements
       while        -> Entry control
       for            -> Entry control
       do ….. while -> Exit Control


                 While
syntax:
Initialization;
while(condition)
{
//body of loop;
      //inc/dec
           }
     Ex:
int c=1;
while(c<=100)
{
cout<<c<<emdl;
c=c+1;
}

                do while

syntax:
Initialization;
do
{
//body of loop
//inc/dec
}while(condition);

Ex:
int c=1;
do
{
cout<<c<<endl;
c=c+1;
}while(c<=100);

                for

Syntax:
for(Initialization;condition;increment)
{
//body of loop;
}
Ex:
for(c=1;c<=100;c++)
{
cout<<c<<endl;
}


           Nesting of Loop
       When we use one loop structure into another loop structure it is called nesting of loop.




                     Array
An array is a collection of variables of the same type that are referenced by a common name.
Types of an array:-
       One dimensional
       Two dimensional

             Declaration of an array
Single dimensional:
data_type    arrayname[size];
Ex-
int  a[10];
float q[20];
char name[10];
        Two dimensional array:
data_type   arrayname[rowsize][colsize];
Ex:-
int   mat[3][3];
char  name[20][10];

                   Reading of an array
for(i=0;i<n;i++)
{
cin>>a[i];
}
   Display of an array element on screen
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
          Reading 2D array
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<mat[i][j]<<“ “;
}
cout<<endl;
}
      
          Display 2D array on screen

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<mat[i][j]<<“ “;
}
cout<<endl;
}


               Thanks…….










No comments:

Post a Comment