Tuesday, 23 October 2012

Abstract Class Vs Interface

FeatureInterfaceAbstract class
Multiple inheritanceclass may inherit several  interface .class may inherit only one  abstract class .
Default implementationAn interface cannot provide any code, just the signature.An abstract class  can provide complete, default code and/or just the details that have to be overridden.
Access ModfiersAn  interface  cannot have access modifiers for the subs, functions, properties etc everything is assumed as publicAn  abstract class  can contain access modifiers for the subs, functions, properties
Core VS Peripheralinterfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interfaceAn  abstract class  defines the core identity of a class and there it is used for objects of the same type.
HomogeneityIf various implementations only share method signatures then it is better to use interfaceIf various implementations are of the same kind and use common behaviour or status then abstract class  is better to use.
SpeedRequires more time to find the actual method in the corresponding classes.Fast
Adding functionality (Versioning)If we add a new method to an  interface  then we have to track down all the implementations of the interface  and define implementation for the new method.If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and ConstantsNo fields can be defined in interfacesAn  abstract class can have fields and constrants defined.


Monday, 22 October 2012

Pascal Triangle in c


Program:
#include<stdio.h>
 
long factorial(int);
 
main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");
 
      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for( c = 1 ; c <= n ; c++ )
         result = result*c;
 
   return ( result );
}

Output:

Monday, 15 October 2012

Program to find odd or even number using C

#include<stdio.h>
void main ()
{
int number;
clrscr ();
printf ("Enter the value of number: ");
scanf("%d",&number);
if (number%2==0)
{
printf ("\nNumber is Even");
}
else
{
printf("\nNumber is Odd");
}
getch ();
}

Output:








Tuesday, 9 October 2012

Difference between C++ and Java



C++ was mainly designed for systems programming and Java was created initially to support network computing.
But today java is provides support for all purpose internet,distributed system etc.

1.C++ supports pointers whereas Java does not pointers.
Although Java supports pointers in a way, that Object reference are used to pass between functions, but pointer arithematical is not possible.
2.At compilation time Java Source code converts into byte code.
The interpreter execute  this byte code at run time and gives output.
Java is interpreted for the most part and hence platform independent.
C++ run and compile using compiler which converts source code into machine level languages so       c++ is plate from dependents 
3.Java uses compiler and interpreter both and in c++ their is only compiler.
4.C++ supports operator overloading,multiple inheritance but java does not.Java supports method overloading.
5.C++ is more nearer to hardware then Java
6.Everything is an object in Java (Single root hierarchy as everything gets derived from java.lang.Object). 
7.Java is pure object oriented programming language, while C++ supports both procedural and object oriented approach.
8.Thread support is built-in Java but not in C++. 
9.Internet support is built-in Java but not in C++.
10.Java does not support header file, include library files just like C++ .Java use import to include  different Classes and methods.
11.Java does not support default arguments like C++.
12.There is no scope resolution operator " :: "in Java.
13.There is no "goto " statement in Java.
14.Java support call by value only.
15.Java does not support unsigned integer.
16. No preprocessor is required in Java, while C++ rely on preprocessor.
17.Java doesn’t support Templates, while C++ supports Templates for parameterized type.
18.In Java, there is bound checking on the arrays, while in C++ there is no bound checking.
19.Java doesn’t supports forward referencing, while C++ supports explicit forward referencing
20.Java supports interfaces which is missing in C++
21.Java also supports network protocols such as TCP/IP and HTTP,while C++ lacks those support.
22.Java supports distributed applications such as RMI and sockets, while C++ lacks this functionality.
23. In Java, array lengths in multidimensional can vary from one element to the next within one dimension, while in C++ multidimensional arrays lengths are of same sizes.
24.Java supports labels on break and continue statements, while in C++ there is no label functionality.
25.  In Java, Strings are objects, while in C++, Strings are null-terminated character arrays.

Sunday, 7 October 2012

Difference between C and C++


1.C is Procedure Oriented Programming Language (POP).
   C++ is Object Oriented Programming Language (OOP).

2.C is mostly used to develop system software.
   C++ is mostly used to modal real life problem to program and use to develop application programs.  

3.C program has extension .C
   C++ program has extension .cpp

4.C uses the top-down approach.
    while C++ uses the bottom-up approach.

5.C is function-driven.
   while C++ is object-driven.

6.C supports GTK tool.
   C++ supports Qt tools for GUI programming.

7.C does not have different visibility mode to store data. It stores data in a default mode irrespective      of the data type.
   C++ provides different data visibility modes : Public, Private, and Protected.

8. C language was developed in AT & T bell laboratories. 
    While C++ is the higher version of C with GUI features developed by B. Stroutstrup.

Friday, 5 October 2012

Conversion of decimal number to a binary number in C++

#include<iostream.h>

#include<math.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int dec_num;
int bin_digits;
int *bi;
int index,i;
int repeat;

clrscr();

do
{
cout<<"\n"<<"Enter the decimal number ";
cin>>dec_num;
bin_digits=(log10(dec_num)/log10(2))+1;

bi=new int[bin_digits];
index=0;

while(dec_num > 0)
{
bi[index]=dec_num % 2;
index++;
dec_num=dec_num / 2;
}

for(i=bin_digits-1;i>=0;i--)
cout<<bi[i];

delete[] bi;
cout<<"\n"<<"Repeat? Press 1 ";
cin>>repeat;
}while(repeat == 1);

exit(0);
}

OUTPUT:


Monday, 1 October 2012

Program to find Armstrong Number


#include <stdio.h>
 
main()
{
   int number, sum = 0, temp, remainder;
 
   printf("Enter a number\n");      
   scanf("%d",&number);
 
   temp = number;
 
   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10; 
   }
 
   if ( number == sum )
      printf("Entered number is an armstrong number.");
   else
      printf("Entered number is not an armstrong number.");         
 
   return 0;
}

output:

Program to generate and print Armstrong Numbers using C


#include<stdio.h>
#include<conio.h>
 
main()
{
   int r;
   long numb = 0, c, sum = 0, temp;
 
   printf("Enter the maximum range upto which you want to find armstrong numbers ");
   scanf("%ld",&numb);
 
   printf("Following armstrong numbers are found from 1 to %ld\n",numb);
 
   for( c = 1 ; c <= numb ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
      if ( c == sum )
         printf("%ld\n", c);
      sum = 0;
   }
 
   getch();
   return 0;
}

Output:
Enter the maximum range up to which you want to find armstrong    numbers 1000
Following armstrong numbers are found from 1 to 1000
1
153
370
371
407


Related Posts Plugin for WordPress, Blogger...