Thursday, 3 March 2011

56. Difference between pointer and reference


POINTER
1.   Its not necessary to initialize the pointer at the time of declaration. Like
      Code:
     int a=10;
     int *p=&a; //it is not necessary
Another way is:
   Code:
   int a=10;
   int *p;
   p=&a;
2.   You can create the array of pointer.
3.   You can assign NULL to the pointer like
   Code:
   int *p=NULL; //valid
4.   You can use pointer to pointer.

REFERENCE:

1.   Its necessary to initialize the Reference at the time of declaration. Like
      Code:
      int &a=10
      int &a; //Error here but not in case of pointer.
2.   You can not create the Array of reference.
3.   You can not assign NULL to the reference like.
    Code:
    int &a=NULL; //Error
4.   You can not use reference to reference.

55. Difference between C and C++

Ø  In case of C, the data is not secured while the data is secured(hidden) in C++
Ø  C is a low-level language while C++ is a middle-level language
Ø  C is function-driven while C++ is object-driven
Ø  C++ supports function overloading while C does not
Ø  We can use functions inside structures in C++ but not in C.
Ø  The NAMESPACE feature in C++ is absent in case of C
Ø  The standard input & output functions differ in the two languages
Ø  C++ allows the use of reference variables while C does not

54.Difference between class and structure

1.   Classes are usually used for large amounts of data, whereas structs are usually used for smaller amount of data.
2.   Classes could be inherited whereas structures no
3.   A structure couldn't be Null like a class.
4.   A structure couldn't have a destructor such as class.
5.   A structure can't be abstract, a class can.
6.   You can't use sizeof with classes but you can with structures.
7.   The structure can't contain a volatile field wheatears the class does.
8.   classes support polymorphism, whereas structures do not.

Sunday, 13 February 2011

51. Inline function

Inline functions are an imperative feature of C++ and are frequently used with classes. Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program. In a program when we call a function the program jumps to the address of the function and when it reaches the end of the function it comes back. This jumping actually involves much more and takes time. But when an inline function is called the compiler will replace the call with the function code. So in reality in that place there will be no function call, only the code of the function. No jumping needed to different addresses. The general format of inline function is as follows:

inline datatype function_name(arguments)

Example:
The following code creates and calls an inline function:
#include<iostream.h>
inline int average(int a, int b)
{
   return (a + b) / 2;
}
void main()

{
   int result = average(12, 14);

   cout << "The average of number 12, 14 is " << result << "\n";
   getch();

}
It is most important to know that the inline specifies is a request, not a command, to the compiler. If, for various reasons, the compiler is unable to fulfill the request, the function is compiled as a normal function.

Thursday, 10 February 2011

50. What is macros in C

A macro is a fragment of code which has been given a name. When we use the name of macro in program, it is replaced by the contents of the macro. You may define any valid identifier as a macro, even if it is a c keyword. There are two types of macros. One is object-like macros and another is function-like macros. Object-like macros do not take parameters; function-like macros do. The generic syntax for declaring an identifier as a macro of each type is:
For object-like macros:
 #define <identifier> <replacement token list>
For example:
#include<stdio.h>
#include<conio.h>
#define sum a+b
void main()
{
Clrscr();
int a=5,b=10,c;
c=sum; //if we give the semicolon (;)when we define the macro; then we cannot give the semicolon when we write the name of identifier. If we give the semicolon when we define the macro, then we give the semicolon when we write the name of identifier in program
printf(“The sum of a+b is: %d”,c);
getch();}
For function-like macros:
#define <identifier> (<parameter list>) <replacement token list>
For example:   
#include<stdio.h>
#include<conio.h>
#define square(x) x*x;
Void main()
{
Clrscr();
int i=2,j;
j=square(i) //Here is no semicolon because I give the semicolon when I define the macros.
printf(“The value of j is: %d”,j);
getch();
}
 The #undef directive removes the definition of a macro.
Written by arnob;