Saturday, August 31, 2013

Function

// How to make Function

#include <stdio.h>
int add(int a, int b)
{
int c = a + b;
printf("Sum of a and b is %d\n",c);
}
int main()
{
int sum;
sum = add(5,4);
return 0;
}


//Video Tutorial for How to Declare Functions



Example 2:

#include <iostream>
using namespace std;
int add(int a, int b)
{
int c = a + b;
cout<<"Sum of a and b is "<<c<<"\n";
}
int main()
{
int sum;
sum = add(5,4);
return 0;
}


Friday, August 30, 2013

Function Override

Example 1:

// Function Override

#include <iostream>
using namespace std;

class arithmetic
{
  protected:
    int a, b, sum, sub, mul, div;
  public:
    void values (int x, int y)
      {
a=x, b=y;
      }
    virtual int operations ()
      {
sum= a + b;
cout<< "Addition of two numbers is "<< sum<<"\n";
      }
};

class Subtract: public arithmetic
{
  public:
    int operations ()
      {
sub= a - b;
cout<< "Difference of two numbers is "<<sub <<"\n";
      }
};

class Multiply: public arithmetic
{
  public:
    int operations ()
      {
mul = a * b;
cout<< "Product of two numbers is "<< mul<<"\n";
      }
};

class Divide: public arithmetic
{
  public:
    int operations ()
      {
div = a / b;
cout<< "Division of two numbers is "<< div<<"\n";
      }
};


int main()
{
    arithmetic *arith, p;
    Subtract subt;
    Multiply mult;
    Divide divd;

    arith=&p;
    arith->values(30,12);
    arith->operations();

    arith=&subt;
    arith->values(42,5);
    arith->operations();

    arith=&mult;
    arith->values(6,5);
    arith->operations();

    arith=&divd;
    arith->values(6,3);
    arith->operations();

return 0;
}

Thursday, August 29, 2013

Function Overload

//Function Overload

#include <iostream>
using namespace std;

int add(int a, int b, int c)
{
   return(a + b + c);
}

float add(float d, float e)
{
   return (d + e);
}

int main()
{
int add(int, int, int);
float add(float, float);
int a,b,c;
float d,e,sum;

   cout << "Enter three integers\n";
   cin >> a >> b >>c;
   sum = add(a, b, c);
   cout << "Sum of integers is " << sum << "\n";

   cout << "Enter two floating point numbers\n";
   cin >> d >> e;
   sum = add(d, e);
   cout << "Sum of floating point numbers is " << sum << "\n";
   return 0;
}


Wednesday, August 28, 2013

Call by Reference


//Call by Reference

#include<stdio.h>
int swap(int *a, int *b)
{
  int t;
  t=*a;
  *a=*b;
  *b=t;
}

int main()
{
  int i, j;

  printf("Enter the values ");
 scanf("%d%d",&i,&j);

  printf("Before swapping %d and  %d\n",i,j);

  swap(&i,&j);

  printf("After swapping %d and %d\n",i,j);

return 0;
}

Call by Value

//Call by Value

#include<stdio.h>
int cube(int x)
{
  x=x*x*x;
  return(x);
}
int main()
{
 int n=8;
 printf("Cube of %d is %d\n",n,cube(n));
return 0;
}


Tuesday, August 27, 2013

Friend Function

Example 1:

// Friend Function

#include <iostream>
using namespace std;
class frnd
{
private:
   int a,b;
public:
   void input()
    {
     cout<<"Enter the value of a and b\n";
     cin>>a>>b;
    }

  friend int compute(frnd f1);
};

int compute(frnd f1)
{
  int x=f1.a+f1.b;
  return x;
}

int main()
{
  frnd f;
  f.input();
  cout <<"The result is: "<< compute(f)<<"\n";
return 0;
}

Classes Objects

Example 1:

//Classes Objects

#include <iostream>
using namespace std;
class square
{
    int x;
    public:
    int area (int);
};
int square :: area (int a)
{
  x = a;
 return x*x;
}
int main()
{
  square sqr;

  cout << "Area of the square is " << sqr.area(4)<<"\n";
  return 0;
}