Sie sind auf Seite 1von 9

1) Difference between Decleration & Definition.

extern float p1;


// this is decleration. No memory is allocated as such.
Corresponding definiton will be provided in some other translational unit and
memory for that variable is allocated there.
float p2;

//this is definition (memory is allocated to p2)

2) Difference between
float *p1[100] ; // p1 is an array of 100 pointers (each pointing to a float
variable)
float (*p2)[100] // p2 is a pointer to an array which contains 100 floats.

Due to Associativity.

3) How function overloading is implemented in C++


with Name Mangling.
using c++filt tool we can get normal name from a mangled/decorated name

4) Difference between Macros and Inline functions


macros copied during preprrocessing.
inline func copied (object code) after compilation and during linking.
no type checking for macros.
side effects with macros

5) Header Gaurds
#ifndef _ABC_H_

#define _ABC_H_
...

...
endif
6) What is initialization list and when it is mandatory
class ABC {
public:
ABC(v1,v2,v3): mem_a(v1),mem_b(v2) {
mem_c=v3;
}
}
to initialze constant and reference member variables of a class, initiallization
lists are mandatory. They offer better performnace by eliminating temporary
vars.

7) Abstract Class. Class with pure virtual functions is called abstract class.
Can not be instantiated.
class ABC {
virtual method_1() = 0; //pure viirtual function
virtual method_2();
}

8) How virtual functions are implemented?


Each object of a virtual class will have one extra pointer (VPTR) inserted by
compiler .
For each virtual class there will be one function pointers table (array of
function pointers)

VPTR of the object will be pointing to the aabove virtual table

During runtime, depending upon the type of the object pointed to by the base
pointer, the corresponding method will be called.
class Base {
public:
virtual int f1();
}
class Der_1: public Base {
public:
int f1();
}

class Der_2:public Base {


public:
int f1();
}

Der_1 *pd1 = new Der_1()


Der_2 *pd2= new Der_2()
Base *bp = new Base()
bp->f1() // Base f1 will be called
bp=pd1
bp->f1() // Der_1 f1 will be called as bp is now pointing to a Der_1 object
bp=pd2
bp->f1() // Der_2 f1 willl be callled now

If in the Base decleration, the ''virtual" keyward is not present before the f1()
decleration, bp->f1() willl always call Base f1() iirrrespective of to which
object this bp is now pointing to, as the decision of func call willl be taken
during compile time rather than at rruntime due to this compiler cannot
decide to which objct bp is pointing to .

9) long a=5;
int b=sizeof(a++)
what is the value of a?
'a' value willl be stilll be 5, as 'sizeof' is an operrator and its arguments are
evaluated for type and the actual evaluation does not happen. type of 'a++'
expression is an int, so b will be assigned either 4 (for 32bit builds) or 8 (for
64bit builds)

10) what do you know abut 'this' pointer


every of object willl have 'this' pointer that referes to the object itself.
11) How to you prevent object creation, object copy or assignment
By making the constructor, copy constructor , assignment operrator 'private'
How to do you limit the number of objects of a class to 1 (singleton pattern)
1) make constructorr provate
2) provide a public method to getInstance
class Single {
private:
Single(){...}
public:
Single *getInstance() {
if (p_instance == NULL) {
p_instance=new Single();
}

return p_instance;
}
}
How about deletion of this single instance?
11) alloca ?
Usually dynamic memory allocated by mallloc, calloc, realloc, new are
allocated in the Heap.
but 'alloca' function is used to allocate dynamiic memory in the stack instead
of in the Heap. This alllocated memory will be freed automaticallly when the
currently active function returns

12) Difference between

const int *p1 = &a;


int const * p2 = &b;
int * const p3 = &c;
p1 and p2 are same. ' pointer to a constant value' . they are like read
handles, they can not be used to change the value they are pointing to .
However they are allowed to point to a different object during their lifetime at
different points;
p1= &another_int; is allowed. But
*p1 = 36; is not alllowed as we are trying to change the value of 'a' indirectly
through 'p1' . As 'p1 is read handlle this is not allowed'

p3 is a constant pointer. It can act as both read and write handle. It can read
and change the value it is pointing to. But once after this pointer is initialized
to point to an object, this cannot be changed to point to a different object
during its entire life time. From its inception to its end, it willl be pointing to
the same object as it is initialized with.
*p3 = 84; is allowed. It will change the value of 'c' to 84. But
p3= &another_int

is not allowed.

p3++ is also not alllowed as this iis trying too point to a new address.
however (*p3)++ is alowed, this willl increment the value of 'c'

13) Castiing Operator


const_cast : to remove or add constantness to a pointer or reference
variable.
const int *p1 = &a;
int *p2 = const_cast<int *>(p1) // remmoved the constantness of p1
const int *p3 = const_cast<const int *>(p2) // added constantness

int a; // a is simple integer


const int &b = &a ; // b is a reference variable referencing a
int &c = const_cast<int &> (b)
const int &d = const_cast<const int &> (c)

reinterpret_cast : to cast between totally unrellated types.


ClassABC *abc;
ClassDEF *d = reinterpret_cast<ClassDEF *> (abc)

dynamic_cast : to downcast. To cast a base poiinter to a derived pointer.


class Base;
class Der_1:public Base;
class Der_2:publiic Base;
Der_1 *dp1 = new Der_1()
Base *bp1=dp1 /// Allowed withoout cast as a Derived poointer can be
assigned to a Base pointer

Der_1 *dp2 = dynamic_cast<Der_1 *> (bp1) // dynamic_cast is needed as


we are doing a downcast (from Base pointer to Derived Pointer) . As bp1 is
already pointing to a Der_1 object this cast would be successfull.
Der_2 *dp3 = dynamic_cast<Der_2 *>(bp1) // this willl return NULL to dp3
as bp1 iis pointing to a Der_1 object and we are forrcing to cast to Der_2
object. this is illlegal . so a NULLL willl be returned by the dynamic_cast
operator.
So after dynamic_cast, we need to check thee pointer before using. like
if( dp3 != NULL) {
....
} else {
printf(" Failure Message. Try to do some recovery or Throw if you cannot
recover to a satisfiable state")
}

static_cast is llike normal c cast. Done during compile time.

14) Conversion Operators


class Complex {
public:
operator int() {
return real + imag;
}
operator char*() {
char *s=new(100);
sprintf(s,"%d + i%d",real,imag);
return s;
}
}

Complex c1(3,7)
int x=c1 ; // 'operator int' conversion function is called and 3+7 is returned
to 'x' .
char *ptr=c1 // 'operator char*' is called and '3+i7' string is returned to ptr
withoout the above conversion operators, these assignments are illegal and
willl resullt n comppillation failures

15) Object Slicing


If you assign a Derived Object to a Base Object (unlike from Derived Pointer
to a Base pointer which is SAFE), Only Base portin of the Derived Object is
assigned to Base Object and the other part of the Derived object is
lost/sliced_out .
Derived D1;
Base B1=D1 ; / wrong resullts in object slicing
Base *bp1= &D1 ; / SAFE as we are assigning pointers rather than objects

16) Diamond Problem


class Base;
class Der_1:public Base;
class Der_2:public Base;
class Der_3: public Der_1, public Der_2;
In the object of 'Der_3' there willl be two copes of the Base object. Too fix
thiis problem, Der_1 and Der_2 need to be derived from Base with 'virtual
iinheritance'
class Der_1: virtual public Base;
class Der_2:virtual publc Base;;
Here Base is callled 'Virtual Base Class' . Responsibility of initializing 'Virtual
Base classes' lies wiith the deepmost derived object (in this case with Der_3
constructor)
17) count number of unique characters in a string

count the lines and words in a file


reverse a file
reverse list of words
18) Tree Traversal functions. recursive and non-recursive
19) recursive functions
Fibonacci number generation
finding gcm
towers of hanoi
tree traversals
linked list reversal
21) Linked Lists:
Deletion of a node containing a given value
Duplicate deletions from a linked list
Loop detectcion in a linked list
22) Implementation of atoi, strcpy , file copy etc.
How to detect a machine as a Little Endian or Big Endian machine
How to detect whether a number is power of 2
23) safity precautions while writing reentrant code
24) What is padding. How to calculate the size of a struct. How to minimize
the size of a struct.

Das könnte Ihnen auch gefallen