Wednesday, September 1, 2010

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

A virtual function that is initialized to zero (0) is
referred to as pure virtual function.It has no body and
hence also known as do-nothing or the dummy function.
Example: virtual void show()=0;

A class containing one or more pure virtual functions is
called an Abstract class, which means an instance of such
class can't be created (but pointer to that class can be
created).We should use pure virtual function if we do not
want to instantiate a class but make it act as a base class
for all the classes that derive from it.An important thing
to note about pure virtual functions is that these
functions must be overridden in all the derived classes
otherwise the compile would flag out an error.

Sample program:

class alpha
{
public:virtual void show()=0; //pure virtual function
};
class beta:public alpha
{
public:void show() //overriding
{
cout<<"OOP in C++"; } }; void main() { alpha *p; beta b; p=&b; p->show();
}

Output: OOP in C++

What will happen if when say delete this ?

There are two scenarios:

1. Using "delete this;" in destructor

Here the call will be a recursive call to the destructor
infinitely. So the program hangs here.

2. Using "delete this;" in other members of a class
including constructor.

In this case, the call is a sucide call since the object
tries to delete itself which is nothing but sitting in a
space and destroy that space itself. That is definitely a
memory crash.



Using delete this in destructor will lead to recursive loop
which will lead to Stack overflow...so avoid it over
here...however there are few times where your code with
delete this will just work fine..like in the usage of
garbage colletors in C++.Chk the below code...which works
with no issues:
class temp
{
public:
temp(){std::cout<<"Constructor"<destroy();
return 0;
}

How much is size of struct having 1 char & 1 integer?

The size of the stucture depends on the following factors.

1.Depends on the size of the datatype, allocated by compiler
2.Depends on the allignment of the variables in the
structure.

If variables of different datatypes are misalligned, then
badding bytes will be added by the compiler to falitate to
speed up the memory access by the processor.So that the size
of the structure will increase

why v use c++ even we have microprocessor?

c++ is completely object orientedlanguage. Its major
concepts like data hiding, encapsukation and polymorphism
are very effective... hence v use c++....

What is the difference between reference type and pointers.

Difference 1>
Reference must point to valid objects at the time of
declaration where pointer need not point to valid objects
at the time of declaration means
int nvalue=5;
int &rnvalue; //This is invalid.
int &rnvalue=nvalue; //This is valid.

But
int *rnvalue; //This is valid.
rnvalue=&nvalue;
Difference 2>
Pointer is a variable which holds the address of another
variable.
But Reference is another name of the same variable.





In addition to the previous answer given in Answer #1,
namely References must point to valid objects at the time
of declaration, references also has the following
limitation.

Once a reference is assigned, there's no way you can modify
the reference. However for a pointer type, variable
assignment is legal.

e.g.,

int i, j;

int *pi, *pj;

pi = &i; // pointer to i
pj = &j; // pointer to j

int &k = i; // reference to i

pi = pj; // pi no longer points to i, instead
// it is now pointing to j
k = j; // The reference k is still with i, it is only
// the value of i that is now modified. i is
// assigned the value of j

When is the last time you coded in C/C++? What is the most lines of original C/C++ code you have personally written in one project? How confident are you in your ability to write C or C++ without a reference?

Yes this is a right question, but its answer depends on
individual, there is no direct associated answer.

Actually Interviewee want to judge candidate 's coding skills,
If He didn't write any code since past few months, or may be
since past few years.

based on the above interviewee can get the idea about the
coding confidence of candidate.

Write a String class which has: 1) default constructor 2) copy constructor 3) destructor 4) equality operator similar to strcmp 5) constructor which takes a character array parameter 6) stream << operator

#include
#include


using namespace std;

class ownStrcmp
{
public:
ownStrcmp(){}
ownStrcmp(ownStrcmp& rhs);
ownStrcmp(char* instring){ _string = instring;}
void setString(char* instring){ _string = instring;}
char* getString(){return _string ;}
~ownStrcmp(){}
int operator == ( ownStrcmp &rhs);

private:
char* _string;
bool _ret;

};

ownStrcmp::ownStrcmp(ownStrcmp& rhs)
{
_string = rhs._string;
}
int ownStrcmp::operator == ( ownStrcmp &rhs)
{
_ret = true;
if(this == &rhs)
{
return _ret;
}
int i = 0;
while( _string[i] != NULL){ ++i;}
int stringLength = i;
for (int j=0;j {
if(_string[j]!=rhs._string[j]) _ret=false;
}
return _ret;
}


int main()
{
ownStrcmp string1("hello world");
ownStrcmp string2("hello world");
if (string1 == string2)
cout<<"result is true"< else
cout<<"result is false"< return 0;
}

What are the advantages of C++ programming compared to C programming?

c++ provides security to the data,c doesn't provide any
security to the data this is main advantage of c++ over than
c and c++ supports all concepts of c and it introduces
object oriented concepts like
objects,inheritance,polymorphism..etc.c++ programs are
useful for real-time purpose but c doesn't support.c++
introduces class concept.by using classes we can do
real-time projects.
c++ follows bottom-up approach but c follows top-down approach.

What is the difference between a copy constructor and an overloaded assignment operator?

A copy constructor constructs a new object by using the
content of the argument object. An overloaded assignment
operator assigns the contents of an existing object to
another existing object of the same class.

Second main difference:
A copy constructor has no return type, whereas an
overloaded assignment operator has one.

Third difference (trivial):
Name of the copy constructor is the same as that of the
class in which it is defined

why all c++ program must have default constructor?

a defaulft constructor is created that takes no parameters
and does nothing.that takes no arguments but sets up your
object as required.

A defaulft constructor ,but by no convention so is any
constructor that takes parameters ,this is can be a bit confusing ,but is usually clear from context which one is meant.


A default constructor is not made by the compiler.so if u want a constructor that takes no parameters ,and u,ve created any others constructors ,u must make the default constructor urself.

"that if u make any constructor at all."

a number is perfect if it is equal to the sum of its proper divisor.. 6 is perfect number coz its proper divisors are 1,2 and three.. and 1+2+3=6... a number is deficient if the sum of its proper divisor is less than the number.. sample: 8 is deficient, coz its proper divisors are 1,2 and 4, and 1+2+4=7. abundant number, if the sum of its proper divisor is greater than the number.. sample..12 is abundant coz 1+2+3+4+6=16 which is geater than 12. now write a program that prompts the user for a number, then determines whether the number is perfect,deficient and abundant.

#include
#include
int f(int x)
{ int i,c,a=0;
for(i=1;ix)
printf("\nthis number is abundant");
if(f(x) printf("\nthis number is deficient");
getch();
}

Write a program that takes a 5 digit number and calculates 2 power that number and prints it?

answer:

that question ask the exponential.
In the compute clause we use the ** for exponential
so write a program using this.

example:

IDENTIFICATION DIVISION.
PROGRAM-ID. SIVARAJAN.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(10) VALUE 2.
01 B PIC 9(10) VALUE 12345.
01 EXP PIC 9(25).
PROCEDURE DIVISION.
START-PARA.
COMPUTE EXP=A**B.
DISPLAY EXP.
STOP RUN.

What is OOPS and How it is different from Procedural Programming ?

OOPS means Object Oriented Programming Languages and
Systems and is different from the Structural programming in
the fact that in OOPS programming you take advantage of
Polymorphism, Multiple inheritance and Abstraction and
Encapsulation of the data by using Private and this helps
in Security of Data while giving you the levarage to
program your software system with the maximum flexibility.

In Procedural Programming language the execution of
application can be step by step. That means the data can be
executed ina sequential manner but in Object oriented
languages the application executed according to the order
we wrote in the program.
that means there is no need to follow the order of
execution on OOP.
it depends on the object.

What is the expansion of OOPS?

Object Orientated Programming Structure.



programing interview question part1

JAVA is FULLY OBJECT ORIENTED PROGRAMING LANGUAGE?

answer:Yes, the JAVA is the fully object oriented, bcoz in java we
can create object for main function also, bcoz main function
written in class

Beauty of Love: this place is make for lovers

Beauty of Love: this place is make for lovers