Docstoc

C++ Complete ease reference with ease examples part_4_

Document Sample
C++ Complete ease reference with ease examples part_4_ Powered By Docstoc
					                      Type Conversion
1. Converting by assignment operator
2. Using cast operator     #include<iostream.h>
Syntax:                    #include<conio.h>
(cast_type) expression;    void main()
or                         {
                           char ch='A';
cast_type (expression);
                           int x;
                           x=(int)ch;
                           cout<<x;
                           getch();
                           }

1   VIT - SITE                                    8/26/2010
                       Enum Types
 The enum keyword automatically enumerates a list of words
    by assigning them values 0,1,2,3,4, and so on.

 The general form of enum is:


enum variable_name{ list of constants separated by commas };

e.g:
  enum day{sun,mon,tue,wed,thu,fri,sat};




2      VIT - SITE                                    8/26/2010
# include<iostream.h>
enum day{sun,mon,tue,wed,thu,fri,sat};
void main( )
{
  day d1, d2,d3;
  d1 = mon;
  d2 = fri;
  int diff = d2 – d1;
  cout<<"days between = "<<diff<<endl;
  if(d1<d2)
  cout<< "day1 comes before day2\n";
}

3   VIT - SITE                           8/26/2010
                      Functions
 A function groups a number of program statements into a
  unit and gives it a name.
 This unit can then be invoked from other parts of the
  program.
 It is used to reduce program size.
 The main advantages of using a function are:
   1. Easy to write a correct small function
   2. Easy to read, write and debug a function
   3. Easier to maintain or modify such a function
   4. It can be called any number of times in any place with
       different parameters



4   VIT - SITE                                         8/26/2010
     The Function Declaration
     The Function Definition
     Function Calling




5   VIT - SITE                   8/26/2010
                    Return statement
     The keyword return is used to terminate function and return a
      value to its caller.
     The return statement may also be used to exit a function
      without returning a value.
     The general form of the return statement is.
              return;
              return ( expression);




6      VIT - SITE                                        8/26/2010
//using multiple return                       if(a>b)
   statements in a function                   {
# include<iostream.h>                                   if(a>c)
void main()                                             return (a);
{                                                       else
   float maximum( float, float, float);                 return (c);
                                              }
   float x,y,z,max;                           else
   cout<<"Enter three numbers";               {
   cin>>x>>y>>z;                                        if(b>c)
   max = maximum(x,y,z);                                return (b);
   cout<<"maximum"<<max;                                else
   }                                                    return (c);
                                              }
float maximum(float a,float b,float c)    }
{
7    VIT - SITE                                            8/26/2010
                 Types of functions

 A function is invoked without passing any formal
  argument does not return any value to the calling
  portion.
 A function is invoked with formal argument and does
  not return any value to the calling portion.
 A function is invoked with formal argument and
  returns back a value to the calling environment.




8   VIT - SITE                                8/26/2010
                  Function Arguments
 The arguments can be classified into two groups:


          1. Actual argument
          2. Formal argument

 Local and Global variable




9    VIT - SITE                                      8/26/2010
                         Recursive function
A function which calls itself directly or indirectly again and again is
  known as the recursive function.
 # include<iostream.h>                  int sum(int n)
 void main()                            {
 {                                      int value=0;
 int sum(int);                          if(n= = 0)
 int n,temp;                            return(value);
 cout<<"Enter any integer               else
 number"<<endl;                         value = n+sum(n-1);
 cin>>n;                                return(value);
 temp = sum(n);                         }
 cout<<"value = "<<n<<"and its
 sum ="<< temp;
 }

 10    VIT - SITE                                              8/26/2010
                       Default arguments
E.x: void sum(int a,int b,int c=6,int d=10);
 // default argument declaration           void sum(int a1,int
 # include<iostream.h>                     a2,int a3,int a4)
 void sum(int a,int b,int c=6,int d=10);   {
         //default argument                int temp;
                initialization             temp = a1+a2+a3+a4;
 void main()                               cout<<"sum="<<temp;
 {                                         }
 int a,b,c,d;
 cout<<"enter any two
 numbers"<<endl;
 cin>>a>>b;
 sum(a,b);      //sum of default values
 }

 11    VIT - SITE                                       8/26/2010
                   Function Overloading


int add(int a,int b);            cout<<add(5,10);
int add(int a,int b,int c);      cout<<add(15,10.0);
double add(double x,double y);   cout<<add(12.5,7.5);
double add(int p,double q);      cout<<add(5.10,15);
double add(double p,int q);      cout<<add(0.75,5);




12   VIT - SITE                                     8/26/2010
                             Function Overloading
#include<iostream.h>
int volume(int);             double volume(double r,int h)
double volume(double,int);   {
long volume(long,int,int);   return(3.14519*r*r*h);
main()                       }
{                            long volume(long l,int b,int h)
cout<<volume(10)<< endl;     {
cout<<volume(2.5,8)<<endl;   return (1*b*h);
cout<<volume(100L,75,15);    }
getch();
return 0;
}
int volume(int s)
{
return (s*s*s);
}      VIT - SITE
13                                                  8/26/2010

				
DOCUMENT INFO
Shared By:
Stats:
views:16
posted:8/26/2010
language:English
pages:13
Description: C complete and easy reference with easy examples. If u learn this u r finished with C