C++ language

FAQ
Previous Home Next

What is Polymorphism?

Polymorphism is phenomena by which we can make make a method for reuse. Polymorphism is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions. In other words, polymorphism means taking more than one form. Polymorphism leads to two important aspects in Object Oriented terminology - First one is function Overloading and second is function Overriding.

Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. Overriding refers to the modifications made in the sub class to the inherited methods from the base class to change their behavior.

What is Operator overloading?

When an operator is overloaded, it takes on an additional meaning relative to a certain class. But it can still retain all of its old meanings.

Examples:

  • The operators >> and << may be used for I/O operations because in the header, they are overloaded
  • In a stack class it is possible to overload the + operator so that it appends the contents of one stack to the contents of another. But the + operator still retains its original meaning relative to other types of data.

What are Templates ?

C++ Templates allow u to generate families of functions or classes that can operate on a variety of different data types, freeing you from the need to create a separate function or class for each type. Using templates, you have the convenience of writing a single generic function or class definition, which the compiler automatically translates into a specific version of the function or class,

for each of the different data types that your program actually uses. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that.

What is the difference between run time binding and compile time binding?

Dynamic Binding: This is also known as "Late Binding".The address of the functions are determined at runtime rather than at compile time.

Static Binding: This is also known as "Early Binding" .The address of the functions are determined at compile time rather than at run time.

What is Difference Between C and C++

C C++
.In C passing value to a function is "Call by Value" in C++ its "Call by Reference"
C does not have a class/object concept. C++ provides data abstraction, data encapsulation, Inheritance and Polymorphism
In C structures can not have contain functions declarations. In C++ structures are like classes, so declaring functions is legal and allowed.
C compiler can not ! C++ supports all C syntax.
File extension is c in C while .cpp in C++.(C++ compiler compiles the files with .c extension but C compiler can not!)
in c the closest you can get to an User defined data type is struct and union.

What will be the output of the following code?

void main (){
 int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
} 

The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.

Why doesn't the following code give the desired result?


 int x = 3000, y = 2000 ;
  long int z = x * y ;

Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below:

long int z = ( long int ) x * y ;
  Note that ( long int )( x * y )
would not give the desired effect.

Why doesn't the following statement work?

char str[ ] = "Hello" ; strcat ( str, '!' ) ; 

The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character.

To make the above statement work writes the statement as shown below :

 strcat ( str, "!" ) ;

How do I know how many elements an array can hold?

 main( ){
int i[32767] ;
float f[16383] ;
char s[65535] ;
} 

How do I write code that reads data at memory location specified by segment and offset?

Use peekb( ) function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek( ) function.

#include <stdio.h>
#include <dos.h>
main( ){
char far *scr = 0xB8000000 ;
FILE *fp ;
int offset ;
char ch ;
if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL )
{
printf ( "\nUnable to open file" ) ;
exit( ) ;
}
for ( offset = 0 ; offset < 160 ; offset++ )
fprintf ( fp, "%c", peekb ( scr, offset ) ) ;
fclose ( fp ) ;
if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL )
{
printf ( "\nUnable to open file" ) ;
exit( ) ;
}
for ( offset = 0 ; offset < 160 ; offset++ )
{
fscanf ( fp, "%c", &ch ) ;
printf ( "%c", ch ) ;
}
fclose ( fp ) ;
} 

Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?

There is nothing like Virtual Constructor. The Constructor cant be virtual as the constructor is a code which is responsible for creating a instance of a class and it cant be delegated to any other object by virtual keyword means.

What about Virtual Destructor?

Yes, there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object baller is balling to , proper destructor will be called.

What is Pure Virtual Function? Why and when it is used ?

The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error.

This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

What is problem with Runtime type identification?

The run time type identification comes at a cost of performance penalty. Compiler maintains the class.

How Virtual functions call up is maintained?

Through Look up tables added by the compile to every class image. This also leads to performance penalty.

Can inline functions have a recursion?

No, Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.

Previous Home Next