Friday, 9 December 2011

Concept of Polymorphism

/*Polymorphism: a greek term means "ability to take more than one form", i.e.. any operation may exhibit different behaviours in different instances.
Behaviour depends on the following:
i)  types of parameter passed
ii) number of parameter passed
iii)order of the parameter passed

Different types of polymorphism are:

a)COMPILE TIME Polymorphism(Static polymorphism)->
  Example:
  1)Function overloading(same function name performs different tasks)
  2)Operator overloading(mechanism of assigning special meanings to operator,(Sizeof)(.)(.*)(::)(?:)cannot be overloaded)

b)RUN TIME Polymorphism(Dynamic polymorphism)->    eg. Virtual function                */

/* Program to implement compile time polymorphism*/
#include<iostream.h>
#include<conio.h>
class Add
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display(void);

//Declaration(prototypes)
int sum(int,int,int);                                //function sum is overloaded
double sum(double,int,double);           //function sum is overloaded

//Declaration(no prototype)
void operator-();                                     //overload unary minus
};

void Add::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}

//Function Definations
int sum(int a,int b,int c){
return(a+b+c);
}
double sum(double a,int b, double c){
return(a+b+c);
}

void Add::display(void)
{
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
//operator definationvoid Add::operator-()
{
x=-x;
y=-y;
z=-z;
}

void main()
{
clrscr();
Add a1;
a1.getdata(2,-3,-4);
cout<<"Input:";
a1.display();
-a1;                                                     //activates operator-() function
cout<<"operator -() Output:";
a1.display();
cout<<"Function output sum(2,3,4): "<<sum(2,3,4)<<"\n";
cout<<"Function output sum(1.5,2,2.2): "<<sum(1.5,2,2.2);

getch();
}

OUTPUT:

Sunday, 4 December 2011

C, C++, C#, VB.net, Java is not a big deal, key lies in logic

/* Basic C Program (covers pgm. to find factorial, fibnacci, prime, armstrong, even, leap, pyramid, reverse number, sum of digits of a number )- concentrate in logic */

#include<stdio.h>
#include<conio.h>

void factorial(int a);
void fibnacci(int b);
void prime(int c);
void armstrong(int d);
void even(int e);
void leap(int f);
void pyramid(int g);
void revnum(int h);
void sumofdigits(int i);


void main(){
int num;
clrscr();
printf("\n Enter the number: ");
scanf("%d",&num);
factorial(num);
fibnacci(num);
prime(num);
armstrong(num);
even(num);
leap(num);
pyramid(num);
revnum(num);
sumofdigits(num);
getch();
}

void factorial(int a)
{
int i,fact=1;
for(i=1;i<=a;i++)
  {
fact=fact*i;
  }
printf("\n The factorial of %d is: %d",a,fact);
 }

void fibnacci(int b)
{
int x=0,y=1,z,i;
for(i=0;i<b;i++)
{
z=x+y;
printf("\n %d",x);
x=y;
y=z;
}
}

void prime(int c)
{
int i,s=0;
for(i=1;i<=c;i++)
{
if((c%i)==0)
s=s+1;
}
if(s==2)
{
printf("\n %d is a prime number",c);
}
else
printf("\n %d is not a prime number",c);
}

void armstrong(int d)
{
int r,sum=0,t;
t=d;
while(d!=0)
{
r=d%10;
sum=sum+(r*r*r);
d=d/10;
}
if(t==sum)
{
printf("\n %d is armstrong number",t);
}
else
printf("\n %d is not an armstrong number",t);
}

void even(int e)
{
if((e%2)==0){
printf("\n %d is even number",e);
}
else
printf("\n %d is not an even number",e);
}

void leap(int f)
{
if((f%4)==0 && (f%100)!=0||(f%400)==0){
printf("\n %d is a leap year",f);
}
else
printf("\n %d is not a leap year",f);
}

void pyramid(int g)
{
int i,j;
printf("\n The pyramid for %d is:\n",g);
for(i=1;i<=g;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
for(i=g;i>1;i--)
{
for(j=1;j<i;j++)
{
printf("%d",j);
}
printf("\n");
}}

void revnum(int h)
{
int r,sum=0,t;
t=h;
while(h!=0)
{
r=h%10;
sum=(sum*10)+r;
h=h/10;
}
printf("\n The reverse of %d is: %d",t,sum);
}

void sumofdigits(int i)
{
int r,sum=0,t;
t=i;
while(i!=0)
{
r=i%10;
sum=sum+r;
i=i/10;
}
printf("\n The sum of digits of %d is: %d",t,sum);
}

OUTPUT
Here number= 12(say)