Thursday, September 5, 2013

Constructor Distructor

Example 1:

//Constructor Destructor

#include <iostream>
using namespace std;
class Addition
{
    int a, b;
    public:
    Addition (int, int);
    ~Addition();
    int add()
    {
     return (a+b);
    }
};

Addition::Addition (int x, int y)
{
  a = x;
  b = y;
}
Addition::~Addition()
{
cout << "Memory Deallocation\n";
}
int main ()
{
  Addition obj (3,4);
  cout <<"Sum is " <<obj.add() <<"\n";
  return 0;
}


Example 2:

#include <iostream>
using namespace std;
class Subtraction
{
public:  
    int a, b;
    int sub(int a, int b)
    {
     return (a-b);
    }
    Subtraction ();

};
Subtraction::Subtraction()
{
}
int main ()
{
  int x;
  Subtraction s;
  x=s.sub(8,4);
  cout <<"Difference is "<<x<<"\n";
  return 0;
}


Abstract Class

Example 1:

//Abstract Class

#include<iostream>
using namespace std;

class abstractinterface
 {
   public:
   virtual void numbers()=0;
   void input();
   int a, b;
 };

void abstractinterface::input()
  {
    cout<< "Enter the numbers\n";
    cin>>a>>b;
  }

class add : public abstractinterface
 {
  public:
  void numbers()
   {
     int sum;
     sum=a+b;
     cout<<"sum is "<<sum<<"\n";
   }
 };

class sub : public abstractinterface
 {
   public:
   void numbers()
    {
     int diff;
     diff=a-b;
     cout<<"diff is "<<diff<<"\n";
    }
 };

int main()
 {
  add obj1;
  obj1.input();
  obj1.numbers();
  sub obj2;
  obj2.input();
  obj2.numbers();
return 0;
 }





Tuesday, September 3, 2013

Structure

Example 1:

//Structure
#include <stdio.h>
struct student
{
  int eng;
  int maths;
  int science;
};
int main()
{
 int total;
 struct student stud;
 stud.eng = 75;
 stud.maths = 70;
 stud.science = 65;
 total = stud.eng + stud.maths +stud.science;
 printf("Total is %d\n",total);
 return 0;
}

//Video Tutorial that how to make a structure in C++






Example 2:

#include <iostream>
using namespace std;
struct student
{
  int eng;
  int maths;
  int science;
};
int main()
{
 int total;
 struct student stud;
 stud.eng = 75;
 stud.maths = 70;
 stud.science = 65;
 total = stud.eng + stud.maths +stud.science;
 cout<<"Total is "<<total<<"\n";
 return 0;
}




Monday, September 2, 2013

Two-dimensional arays

Example 1:

//Two-dimensional arays in C

#include <stdio.h>
int main()
{
   int i,j;
   int num1[3][4],num2[3][4];
   printf("Enter the elememts of 3X4 array num1\n");
   for(i=0; i<3; i++)
   for(j=0; j<4; j++)
   scanf("%d", &num1[i][j]);

   printf("Enter the elememts of 3X4 array num2\n");
   for(i=0; i<3; i++)
   for(j=0; j<4; j++)
   scanf("%d", &num2[i][j]);

   printf("The 3X4 array num1 is\n");
   for(i=0; i<3; i++)
    {
      for(j=0; j<4; j++)
      printf("%3d ", num1[i][j]);
      printf("\n");
    }
   printf("The 3X4 array num2 is\n");
   for(i=0; i<3; i++)
    {
      for(j=0; j<4; j++)
      printf("%5d ", num2[i][j]);
      printf("\n");
    }
   printf("The sum of num1 and num2 is\n");
   for(i=0; i<3; i++)
    {
      for(j=0; j<4; j++)
      printf("%3d ", (num1[i][j] + num2[i][j]));
      printf("\n");
    }
   return 0;
}

//Video Tutorial for how to run an Two Dimensional Arrays




Example 2:

//Two-dimensional arays in C++
#include <iostream>
using namespace std;
int main()
{
   int i,j;
   int num1[3][4],num2[3][4];
   cout <<"Enter the elememts of 3X4 array num1\n"; 
   for(i=0; i<3; i++)
   for(j=0; j<4; j++)
   cin>>num1[i][j];
   
   cout <<"Enter the elememts of 3X4 array num2\n"; 
   for(i=0; i<3; i++)
   for(j=0; j<4; j++)
   cin>>num2[i][j];
   
   cout <<"The 3X4 array num1 is\n"; 
   for(i=0; i<3; i++)
    {
      for(j=0; j<4; j++)
      cout<<num1[i][j] <<"\t";
      cout<<"\n";
    }

   cout<<"The 3X4 array num2 is\n"; 
   for(i=0; i<3; i++)
    {
      for(j=0; j<4; j++)
      cout <<num2[i][j]<<"\t";
      cout <<"\n";
    }

   cout<<"The sum of num1 and num2 is\n";
   for(i=0; i<3; i++)
    {
      for(j=0; j<4; j++)
      cout<<(num1[i][j] + num2[i][j])<<"\t";
      cout<<"\n";
    }
     return 0;
}

Sunday, September 1, 2013

Arrays

Example 1:

// Arrays

#include <stdio.h>
int main()
 {
  int star[3]={4,5,6};
  int sum;
  sum = star[0] + star[1] + star[2];
  printf("The sum is %d\n",sum);
  return 0;
 }

//Video Tutorial for How to run an Array





Example 2:

#include <iostream>
using namespace std;
int main()
 {
  int star[3]={4,5,6};
  int sum;
  sum = star[0] + star[1] + star[2];
  cout<<"The sum is "<<sum<<"\n";
  return 0;
 }


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;
}