Sunteți pe pagina 1din 11

http://www.onesmartclick.com/interviews/interviews-programming.html(this) http://www.parashift.com/c++-faq-lite/ ************ http://www.possibility.com/epowiki/ iki.!sp"page#$pp%nterview&'estions http://!avaq'estion.tripo(.com/interview).html http://www.!avacamp.org/sc!p/co'rse/*+,.html http://www.cs.'iowa.e('/-o(en/co'rses/cs,.f//0*key.html http://members.fort'necity.com/!oe*)/cpp+q'es.html http://cs.hbg.ps'.e('/comp1),/e2amples.html http://www.(ev2.com/tips/vt3rowser/,.4,5"no(e#*6*) ************ http://spaces.msn.com/members/nasha/3log/cns7*p,%8'p)9:29%3iksr)mv&4w7 ,*..entry http://www.igitian.net/otherlinks/programming.

php http://baskaronline.tripo(.com/$pl's*.html ************* http://www.f'n(oosite.com/interview-q'estions/type.asp"i;ype#1* http://www.f'n('co(e.com/weekly'p(ate/<'ly+*/+.)/<'ly+*/+.).htm http://cpl's.abo't.com/library/weekly/mpreviss.htm http://www.b'il(era'.com.a'/program/!ava/ http://www.g'(('miya.com/<ob=ection/interviewq'est.htm http://www.'gra(.physics.mcgill.ca/reference/>1/m1+*?.html@=A$61 *********

hat is the o'tp't of printf(BC(B)

hat will happen if % say (elete this http://www.progsoc.uts.edu.au/lists/progsoc/2000/October/msg00010.html


Generally speaking, I'd recommend against using "delete this" in code. The main problem is that the object can only be created via "new". If the object is declared "auto" i.e. not a pointer or reference! and "delete this" is called it will fail because "this" was not allocated with new and the program will crash. If you really want to use "delete this" in your own code, make the destructor protected, preventing other classes from deleting the object and causing a compiler error if an auto variable of this class is ever declared. If you want to use in it a constructor, I'd recommend against it for the above reason and also because the calling function has no way of knowing whether the object encountered an error " calling one of the object's functions will result in a crash. The object fails to instantiate itself will

have to modify some form of global variable such as errno, #et$ast%rror !, I%rrorInfo, etc, which, although acceptable, is hardly ideal. & much better way to report an error in an object's constructor is to use a static "factory" function to encapsulate the construct. %.g. static 'og( 'og))*reate'og int legs! which can return a +,$$ pointer on an error. &lternatively, the constructor could throw an e-ception. 'on't worry about memory leaks, &+#I" compliant compilers should automatically deallocate the object with calling the constructor if a newed object throws an e-ception in its constructor. *heck your compiler doco for this. The only situation I've seen "delete this" where I consider it an acceptable use is the the thread or window classes from ./*. The intention with these classes is to instantiate them, remove all references to them and let the objects manages their own lifespans. 0 &lso a good idea to check for null after any member function 0 that could cause the object to be destroyed. I haven't got &1. or a similar tome! handly but I suspect that modifying "this" in a member funcion is illegal. %ither way, changing "this" in a member function will not affect the value of the pointer the function was called through. %.g. void 2ero3ptr char( s! 4 55 This line only changes the local version of s. s 6 +,$$7 8 ...

char (s 6 "9ello, world:"7 2ero3ptr s!7 printf s!7 55 #hows "9ello, world:" ----------------------------------------------------------------------------------------------------------Difference between B$ str'ct'reB an( B$++ str'ct'reB. http://www.cs.uregina.ca/Links/class-info/210/LabFAQ/
+otice that in *, when you define a structure , you are +;T defining a new data type. That's why when you declare a variable with the newly"defined structure, you need to write down the key word struct before the name of the structure. In *<<, however, a new type is defined when a structure is defined, and we can use this new type to declare variables, and the struct keyword is +;T needed. *<< structure derives from * structure7 therefore any * structure is also a valid *<< structure . 9owever, the *<< structure also has some characteristics that a * structure does not have. #pecifically, a *<< structure can also contain functions, while a * structure can only contain variables.

----------------------------------------------------------------------------------------------------------Diffrence between a Bassignment operatorB an( a Bcopy constr'ctorB http://www.maxwell.ph.kcl.ac.uk/~courses/cp4731/C .html www.sta!"ord.edu/class/cs1#3d/$a!douts/0%&Cop'&Co!structors.pd" The copy constructor initialises uninitialised memory, whereas the assignment operator must correctly deal with copying values into an e-isting,well"constructed object. The key difference between assignment and copy construction is that assignment possibly involves the destruction of embedded resources. We need to overload the assignment operator whenever we want to guarantee that memory (or perhaps other resources) aren't orphaned improperly. ----------------------------------------------------------------------------------------------------------hat is the (ifference between Boverloa(ingB an( Boverri((ingB" Overloading is declaring a new method with the same name but different parameter lists as one that is already in scope (in the current class or inherited). Overriding is declaring a new method in a sublcass that has the same signature as an inherited method. The new method overrides the inherited method. http://www.cs.uiowa.edu/~ode!/courses/cs20"##(1ke'.html http://people.cs.)t.edu/~ka"ura/cs2704/i!herita!ce.html ----------------------------------------------------------------------------------------------------------A2plain the nee( for BEirt'al Destr'ctorB. http://cs.hbg.psu.edu/comp432/examples/)irtual&destructor.C http://www.gotw.ca/publicatio!s/mill1 .htm ----------------------------------------------------------------------------------------------------------$an we have BEirt'al $onstr'ctorsB"
C++ doesn't have virtual constructors for two good reasons: 1) constructors don't have names so you can't call them explicitly. 2) more importantly: in order to call a virtual function, you must have a fully constructed o!"ect. #n o!"ect is considered fully constructor once its constructor has completed execution without throwing an exception. $o you can see the pro!lem here: calling a virtual constructor would re%uire an already constructed o!"ect, which can !e accomplished !y calling a constrcutor etc. &hat said, there are several porogramming idioms that sumulate a virtual constrcutor. 'ere's an example of this: http:((www.devx.com(tips(&ip())*1 &he Class +actory ,esign -attern also offers a similar mechanism. .our %uestion raises another su!"ect: why /0 ! 1 new # won't compile. 2emem!er that the is a rlationship applies to derived classes. 3f , is pu!licly derived from /, then it's a /. &he reverse isn't true though. 4ould you want to say for example that a C5!"ect class is a C6etwor7,ata class, assuming that the latter is derived from the former8 Certainly not.

http://"orums.de)x.com/showthread.php* s+#44 134 e4##0e1ab#ce 3e"b20"b"77,p+420% -post420% ----------------------------------------------------------------------------------------------------------hat are the (ifferent types of polymorphism"
There are two types of polymorphism inheritance based and interface based. In Inheritance based you have a common base class and in interface based you have a common interface between classes.

----------------------------------------------------------------------------------------------------------hat are Eirt'al 9'nctions" Fow to implement virt'al f'nctions in B$B ----------------------------------------------------------------------------------------------------------hat are the (ifferent types of =torage classes http://www.tim&ma!!.org/trs 0/doc/ldos.2&1.txt
There are basically = types of storage classes for variables) e-tern, auto, static and register. &n important concept to introduce before e-plaining these = types is the difference between the declaration and the definition of a variable. >hen a variable is declared, properties of the variable are assigned type, si2e, etc.!. >hen a variable is defined done only once! storage is assigned. %-cept for use with e-ternal variables these terms definition and declaration! are almost synonymous. &utomatic auto! variables are variables "local" or "internal" to a single function only. This is the default for variables declared within a function. &uto variables have a scope or "lifetime" only within the braces 48 or block in which they are defined. ?ariables declared as or defaulted to auto, appear as a function is called and disappear when the function ends. #ince they have this dynamic "local" nature they are said to be "automatic". %-ternal e-tern! variables are "global" in nature. They are permanent and are accessible throughout the entire range of a * program. They can be shared between functions of a single * program and between many * source programs. ;nly one of the * programs would define the variable, the others would have to only declare it as e-tern. &n e-ternal variable definition appears outside of any function. This actually defines the variable as e-ternal e-tern!. %-ternal variables are defined outside of any function and declared in the function which uses them. #tatic static! variables are stored in a fi-ed memory space. They can be e-ternal or internal in nature. >hen declared to be internal in nature they are like auto variables e-cept they remain in e-istence in their fi-ed space. %-ternal static variables are global in nature but are only accessible within a single * source program. #tatic variables represent private permanent storage. 1egister register! variables are stored in machine registers. They are used to store heavily used variables in order to improve performance of the * program.

----------------------------------------------------------------------------------------------------------hat is :amespace" http://www."u!doosite.com/i!ter)iew&.uestio!s/detail/11/%/ http://www.geeki!ter)iew.com/0!ter)iew&1uestio!s/2a!guages/C&3lus&3lus

----------------------------------------------------------------------------------------------------------hat are the types of =;G containers". vector $inear, contiguous storage, fast inserts at end only deque $inear, non"contiguous storage, fast inserts at both ends list 'oubly"linked list, fast inserts anywhere set #et of items, fast associative lookup multiset $ike set but duplicate objects allowed map *ollection of one"to"one mappings multimap *ollection of one"to"many mappings stack /irst"in, last"out data structure queue /irst"in, first"out data structure priority_queue .aintains objects in sorted order ----------------------------------------------------------------------------------------------------------Difference between BvectorB an( BarrayB Vector is 1-D array, its also an STL class. Array is a datatype. http://baskaro!li!e.tripod.com/Cplus1.html ----------------------------------------------------------------------------------------------------------Fow to write a program s'ch that it will (elete itself after e2ect'tion
/or $inu-, at the end of the program simply use system ! to delete that e-ecutable file. This doesn@t work in >indows as it wont allow to kill a running prog.

----------------------------------------------------------------------------------------------------------$an we generate a $++ so'rce co(e from the binary file" ----------------------------------------------------------------------------------------------------------hat are inline f'nctions"
&n inline function is a function which gets te-tually inserted by the compiler, much like macros. The advantage is that e-ecution time is shortened because linker overhead is minimi2ed. They are declared by using the inline keyword when the function is declared) inline void func void! 4 cout AA "printing inline function Bn"7 8 or by including the function declaration and code body within a class) class test 4 public) void func void! 4 cout AA "inline function within a class.Bn"8 87

-----------------------------------------------------------------------------------------------------------

;alk sometiming abo't profiling


Crofiling is a set of techniDues for estimating the amount of time spent in various portions of your program. The si2e of the program unit being profiled is sometimes! called the granularity. /or e-ample, you can profile 1outines $oops #tatements &ddresses +;T%) /or statistical profiling, however, granularity means the time fraction allocated to The profile will tell you how much of the e-ecution time can be attributed to each grain you have selected. The most common grain, ET>, is routine level profiling. The output of a profiler indicates what fraction of the e-ecution time was spent in each grain. Cresumably, to improve performance, you focus on the grains that are taking the most time.

http://www.caam.rice.edu/~caam/20/4opics/3ro"ili!g/what&is.html ----------------------------------------------------------------------------------------------------------hat is BstrstreamB strstream is a type of input/output stream that works with the memory. t allows usin! section of the memory as a stream o"#ect. These streams pro$ide the classes that can "e used for storin! the stream of "ytes into memory. %or e&ample, we can store inte!ers, floats and strin!s as a stream of "ytes. There are se$eral classes that implement this in-memory formattin!. The class ostrstream deri$ed from ostream is used when output is to "e sent to memory, the class istrstream deri$ed from istream is used when input is taken from memory and strstream class deri$ed from iostream is used for memory o"#ects that do "oth input and output. ----------------------------------------------------------------------------------------------------------Fow to write >'ltithrea(e( applications 'sing $++" ----------------------------------------------------------------------------------------------------------A2plain Bpassing by val'eBH Bpassing by pointerB an( Bpassing by referenceB http://cplus.about.com/od/cprogrammi!gtip1/l/aa010402a.htm
&here are three techni%ues used to pass varia!les into a C or C++ functions. -ass !y value C(C++ -ass a pointer !y value C(C++ -ass !y reference C++ only

Pass by value &he function receives a copy of the varia!le. &his local copy has scope, that is, exists only within the function. #ny changes to the varia!le made in the function are not passed !ac7 to the calling routine. &he advantages of passing !y values are simplicity and that is guaranteed that the varia!le in the calling routine will !e

unchanged after return from the function. &here are two main disadvantages. +irst, it is inefficient to ma7e a copy of a varia!le, particularly if it is large such as an array, structure or class. $econd, since the varia!le in the calling routine will not !e modified even if that's what is desired, only way to pass information !ac7 to the calling routine is via the return value of the function. 5nly one value may !e passed !ac7 this way. Passing a pointer by value # pointer to the varia!le is passed to the function. &he pointer can then !e manipulated to change the value of the varia!le in the calling routine. 3t is interesting to note that the pointer itself is passed !y value. &he function cannot change the pointer itself since it gets a local copy of the pointer. 'owever, the function can change the contents of memory, the varia!le, to which the pointer refers. &he advantages of passing !y pointer are that any changes to varia!les will !e passed !ac7 to the calling routine and that multiple varia!les can !e changed. Passing by reference (C++ only) C++ provides this third way to pass varia!les to a function. # reference in C++ is an alias to a varia!le. #ny changes made to the reference will also !e made to the original varia!le. 4hen varia!les are passed into a function !y reference, the modifications made !y the function will !e seen in the calling routine. 2eferences in C++ allow passing !y reference li7e pointers do, !ut without the complicated notation. $ince no local copies of the varia!les are made in the function, this techni%ue is efficient. #dditionally, passing multiple references into the function can modify multiple varia!les.

----------------------------------------------------------------------------------------------------------rite any small program that will compile in B$B b't not in B$++B
&he only one 3 can thin7 of is one that tries to downcast a void 0 to a more specific pointer without a cast: main9) : void 0vptr; int 0iptr; (0 will compile in C !ut not C++ 0( iptr 1 vptr; <

8I
> main() 3n C=>, that was legal 9implicit int), !ut in C== and C++, you must explicitly specify a return type. &here are some other differences; 3 thin7 Christian's suggestion is the most straight forward, though. $imply use an identifier that's a 7eyword in C++ !ut not in C.

-----------------------------------------------------------------------------------------------------------

Fave yo' hear( of Bm'tableB keywor(" The muta"le keyword is a stora!e specifier that you can only apply to nonstatic data mem"ers of a class. class ma!e ' pu"lic( int )edraw*+ const, //.. pri$ate( muta"le "ool isLoaded,//can "e chan!ed "y a const function -, .nlike ordinary data mem"ers, a const mem"er function can modify muta"le data mem"ers. The use of muta"le data mem"ers mi!ht seem like cheatin! "ecause it ena"les a const function to modify an o"#ect/s data mem"ers. 0owe$er, when used #udiciously, the muta"le keyword impro$es your code 1uality as it ena"les you to hide implementation details from users without resortin! to du"ious hacks such as const2cast34. http://www.builderau.com.au/program/0 !"02#$1# !"12"22$ 00.htm ----------------------------------------------------------------------------------------------------------hat is a BI;;%B"
1untime type identification 1TTI! lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. 1TTI is the official way in standard *<< to discover the type of an object and to convert the type of a pointer or reference that is, dynamic typing!. The need came from practical e-perience with *<<. 1TTI replaces many homegrown versions with a solid, consistent approach.

http://www.geeki!ter)iew.com/0!ter)iew&1uestio!s/2a!guages/C&3lus&3lus ----------------------------------------------------------------------------------------------------------%s there something that % can (o in $ an( not in $++" See 5 6s "ack7 ----------------------------------------------------------------------------------------------------------hy preincrement operator is faster than postincrement8 %or the standard data types there is usually no performance difference, "ut for classes there are9 The reason is that 5"or6most6commo!6impleme!tatio!s6o"7 postfi& operators retain a temporary copy of ori!inal $aria"le and "ecause the return $alue is returned "y $alue, not "y reference. http://www.codeguru.com/"orum/archi)e/i!dex.php/"&7.html ----------------------------------------------------------------------------------------------------------hat is the (ifference between BcallocB an( BmallocB" malloc takes one ar!, calloc two and calloc initiali:es the memory to ;. <ust wanted to say that find it useful to use calloc for "uffers in which /m !oin! to store some characters to make up a strin!, "ecause *if keep within "ufferlen!th-1 characters+ the result will then "e

!uaranteed to "e /=;/ terminated. Same with a "uffer /m !oin! to strncat*+ stuff to. >f course, memset*+ could "e used as well, if the "uffer is !oin! to "e reused. This may "e !ettin! off topic, "ut on some architectures the e&tra initialisation of calloc will not actually "e e&tra, since the Sun Sparc, for e&ample, initiali:es all memory to :ero anyway *or is it Solaris doin! that8 ?ot sure, actually+, whereas it would take a rather smart compiler to see that the memset*+ call would "e useless... http://db"orums.com/t%#7#//.html ----------------------------------------------------------------------------------------------------------hat will happen if % allocate memory 'sing BnewB an( free it 'sing BfreeB or allocate sing BcallocB an( free it 'sing B(eleteB" http://li!ux"romscratch.org/pipermail/l"s&chat/2004&8pril/022270.html666666666699999999 ----------------------------------------------------------------------------------------------------------hat is >emory Jlignment8 ----------------------------------------------------------------------------------------------------------A2plain working of printf. ----------------------------------------------------------------------------------------------------------Difference between BprintfB an( BsprintfB. ----------------------------------------------------------------------------------------------------------hat is BmapB in =;G8 ----------------------------------------------------------------------------------------------------------hen shall % 'se >'ltiple %nheritance8 ----------------------------------------------------------------------------------------------------------hat are the techniq'es yo' 'se for (eb'gging8 ----------------------------------------------------------------------------------------------------------Fow to re('ce a final siKe of e2ec'table8 ----------------------------------------------------------------------------------------------------------Live , e2amples of a co(e optimiKation.

----------------------------------------------------------------------------------------------------------hy wo'l( yo' wish to make a constr'ctor private ----------------------------------------------------------------------------------------------------------What is name encoding or name mangling? ----------------------------------------------------------------------------------------------------------What is a copy constructor? How do you prevent a class from being copied? ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

S-ar putea să vă placă și