Sie sind auf Seite 1von 9

1. 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 Ior creating a instance oI a class and it cant be
delegated to any other object by virtual keyword means.

2. 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 oI object baller is balling to , proper destructor will be called.

3. 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 deIined as pure virtual in base class.

4. What is problem with Runtime type identification?
The run time type identiIication comes at a cost oI perIormance penalty. Compiler
maintains the class.

5. How Virtual functions call up is maintained?
Through Look up tables added by the compile to every class image. This also leads to
perIormance penalty.

6. Can inline functions have a recursion?
No.
Syntax wise It is allowed. But then the Iunction is no longer Inline. As the compiler will
never know how deep the recursion is at compilation time.

7. How do you link a C++ program to C functions?
By using the extern "C" linkage speciIication around the C Iunction declarations.
Programmers should know about mangled Iunction names and type-saIe linkages. Then
they should explain how the extern "C" linkage speciIication statement turns that Ieature
oII during compilation so that the linker properly links Iunction calls to C Iunctions.

8. plain the scope resolution operator?
It permits a program to reIerence an identiIier in the global scope that has been hidden by
another identiIier with the same name in the local scope.

9. How many ways are there to initialize an 39 with a constant?
1. int Ioo 123;
2. int bar(123);

10. What is your reaction to this line of code? /elete this,
It is not a good programming Practice.
A good programmer will insist that you should absolutely never use the statement iI the
class is to be used by other programmers and instantiated as static, extern, or automatic
objects. That much should be obvious.
The code has two built-in pitIalls. First, iI it executes in a member Iunction Ior an extern,
static, or automatic object, the program will probably crash as soon as the /elete
statement executes. There is no portable way Ior an object to tell that it was instantiated
on the heap, so the class cannot assert that its object is properly instantiated. Second,
when an object commits suicide this way, the using program might not know about its
demise. As Iar as the instantiating program is concerned, the object remains in scope and
continues to exist even though the object did itselI in. Subsequent dereIerencing oI the
baller can and usually does lead to disaster. I think that the language rules should
disallow the idiom, but that's another matter.

11. What is the difference between a copy constructor and an overloaded assignment
operator?
A copy constructor constructs a new object by using the content oI the argument object.
An overloaded assignment operator assigns the contents oI an existing object to another
existing object oI the same class.

12. When should you use multiple inheritance?
There are three acceptable answers:- "Never," "Rarely," and "When the problem domain
cannot be accurately modeled any other way."
Consider an sset class, Buil/ing class, Jehicle class, and CompanyCar class. All
company cars are vehicles. Some company cars are assets because the organizations
own them. Others might be leased. Not all assets are vehicles. Money accounts are
assets. Real estate holdings are assets. Some real estate holdings are buildings. Not all
buildings are assets. Ad inIinitum. When you diagram these relationships, it becomes
apparent that multiple inheritance is a likely and intuitive way to model this common
problem domain. The applicant should understand, however, that multiple inheritance,
like a chainsaw, is a useIul tool that has its perils, needs respect, and is best avoided
except when nothing else will do.

13. What is a virtual destructor?
The simple answer is that a virtual destructor is one that is declared with the virtual
attribute.
The behavior oI a virtual destructor is what is important. II you destroy an object through
a baller or reIerence to a base class, and the base-class destructor is not virtual, the
derived-class destructors are not executed, and the destruction might not be comple

14. Can a constructor throw a eception? How to handle the error when the
constructor fails?
The constructor never throws a error.

15. What are the debugging methods you use when came across a problem?
Debugging with tools like :
GDB, DBG, Forte, Visual Studio.
Analyzing the Core dump.
&sing tusc to trace the last system call beIore crash.
Putting Debug statements in the program source code.

16. How the compilers arranges the various sections in the eecutable image?
The executable had Iollowing sections:-
Data Section (uninitialized data variable section, initialized data variable section )
Code Section
Remember that all static variables are allocated in the initialized variable section.

17. plain the ISA and HASA class relationships. How would you implement each in
a class design?
A specialized class "is" a specialization oI another class and, thereIore, has the ISA
relationship with the other class.
This relationship is best implemented by embedding an object oI the $alary class in the
Employee class.

18. When is a template a better solution than a base class?
When you are designing a generic class to contain or otherwise manage objects oI other
types, when the Iormat and behavior oI those other types are unimportant to their
containment or management, and particularly when those other types are unknown (thus,
the generality) to the designer oI the container or manager class.

19. What are the differences between a C++ 897:.9and C++ .,88?
The deIault member and base-class access speciIies are diIIerent.
This is one oI the commonly misunderstood aspects oI C. Believe it or not, many
programmers think that a C struct is just like a C struct, while a C class has
inheritance, access speciIies, member Iunctions, overloaded operators, and so on.
Actually, the C struct has all the Ieatures oI the class. The only diIIerences are that a
struct deIaults to public member access and public base-class inheritance, and a class
deIaults to the private access speciIied and private base-class inheritance.

20. How do you know that your class needs a virtual destructor?
II your class has at least one virtual Iunction, you should make a destructor Ior this class
virtual. This will allow you to delete a dynamic object through a baller to a base class
object. II the destructor is non-virtual, then wrong destructor will be invoked during
deletion oI the dynamic object.

21. What is the difference between new/delete and malloc/free?
Malloc/Iree do not know about constructors and destructors. New and delete create and
destroy objects, while malloc and Iree allocate and deallocate memory.

22. What happens when a function throws an eception that was not specified by an
eception specification for this function?
&nexpected() is called, which, by deIault, will eventually trigger abort().

23. Can you think of a situation where your program would crash without reaching
the breakball, which you set at the beginning of main()?
C allows Ior dynamic initialization oI global variables beIore main() is invoked. It is
possible that initialization oI global will invoke some Iunction. II this Iunction crashes
the crash will occur beIore main() is entered.

24. What issue do auto_ptr objects address?
II you use autoptr objects you would not have to be concerned with heap objects not
being deleted even iI the exception is thrown.

25. Is there any problem with the following:
char *aNULL, char& p *a,?
The result is undeIined. You should never do this. A reIerence must always reIer to some
object.

26. Why do C++ compilers need name mangling?
Name mangling is the rule according to which C changes Iunction's name into
Iunction signature beIore passing that Iunction to a linker. This is how the linker
diIIerentiates between diIIerent Iunctions with the same name.

27. Is there anything you can do in C++ that you cannot do in C?

No. There is nothing you can do in C that you cannot do in C. AIter all you can
write a C compiler in C

O What are the major diIIerences between C and C?
4 What are the diIIerences between new and malloc?
4 What is the diIIerence between delete and delete||?
4 What are the diIIerences between a struct in C and in C?
4 What are the advantages/disadvantages oI using #deIine?
4 What are the advantages/disadvantages oI using inline and const?
O What is the diIIerence between a baller and a reIerence?
4 When would you use a baller? A reIerence?
4 What does it mean to take the address oI a reIerence?
O What does it mean to declare a Iunction or variable as static?
O What is the order oI initalization Ior data?
O What is name mangling/name decoration?
4 What kind oI problems does name mangling cause?
4 ow do you work around them?
O What is a class?
4 What are the diIIerences between a struct and a class in C?
4 What is the diIIerence between public, private, and protected access?
4 For class CFoo }; what deIault methods will the compiler generate Ior you~?
4 ow can you Iorce the compiler to not generate them?
4 What is the purpose oI a constructor? Destructor?
4 What is a constructor initializer list?
4 When must you use a constructor initializer list?
4 What is a:
Constructor?
Destructor?
DeIault constructor?
Copy constructor?
Conversion constructor?
4 What does it mean to declare a...
member Iunction as virtual?
member Iunction as static?
member Iunction as static?
member variable as static?
destructor as static?
4 Can you explain the term "resource acquisition is initialization?"
4 What is a "pure virtual" member Iunction?
4 What is the diIIerence between public, private, and protected inheritance?
4 What is virtual inheritance?
4 What is placement
new?
4 What is the diIIerence between
operator new
and the
new
operator?
O What is exception handling?
4 xplain what happens when an exception is thrown in C.
4 What happens iI an exception is not caught?
4 What happens iI an exception is throws Irom an object's constructor?
4 What happens iI an exception is throws Irom an object's destructor?
4 What are the costs and beneIits oI using exceptions?
4 When would you choose to return an error code rather than throw an exception?
O What is a template?
O What is partial specialization or template specialization?
O ow can you Iorce instantiation oI a template?
O What is an iterator?
O What is an algorithm (in terms oI the STL/C standard library)?
O What is
std::auto_ptr?
O What is wrong with this statement?
std::auto_ptr ptr(new char10]);
O It is possible to build a C compiler on top oI a C compiler. ow would you do this?


How do you check whether a linked list is circular?
2 How do you decide which integer type to use?
3 How do you differentiate between a constructor and destructor?
4 How do you differentiate between aggregation and association?
5 How do you find out the size of a class?
6 How do you implement an itoa function?
7 How do you initialize a pointer to a function?
8 How do you link a C++ program to C functions?
9 How do you return a structure from functions?
How do you write a function that can reverse a linked-list?
hat are the benefits of using exceptions in C++?
2hat are the differences between a struct and a class in C++?
3hat are the different types of Storage classes?
4hat do you mean by inline function?
5hat does extern mean in a function declaration?
6hat does extern mean in a function declaration?
7hat does it mean to declare a function or variable as static?
8hat is a conversion constructor?
9hat is a copy constructor?
2hat is a namespace?
2hat is a pure virtual function?
22hat is a scope resolution operator?
23hat is abstraction?
24hat is difference between #define and const?
25hat is difference between function overloading and overriding?
26hat is encapsulation?
27hat is function overloading?
28hat is multiple inheritance?
29hat is operator overloading?
3hat is partial specialization or template specialization?
3hat is polymorphism?
32hat is the difference between "passing by value and "passing by reference?
33hat is the difference between an object and a class?
34hat is the difference between declaration and definition?
35hat is the difference between declaration and definition?
36hat is the difference between delete and delete[]?
37hat is the difference between inner class and abstract class?
38hat is the difference between persistent & non-persistent objects?
39hat is the difference between structure and union?
4hat is the difference between the deep copy and shallow copy?
4hat is the difference between the private public and protected members?
42hat is the maximum size that an array can hold?
43hat is the most efficient way to reverse a linked-list?
44hat is the size of an empty class?
45hat is the use of virtual destructor?
46hat is virtual class and friend class?
47hats the best way to declare and define global variables?
48here is memory for class-object allocated?
49here is memory for struct allocated?
5hy an array always starts with index zero in C++?
#010703.08
http://wwwallinterviewcom
http://wwwcoolinterviewcom/
http://wwwtechinterviewscom

TIIs seL oI C++ InLervIew quesLIons wus senL Lo TecInLervIews Irom AusLruIIu:
IuL Is LIe dIIIerence beLween un ARRAY und u ST?
zIuL Is IusLer : uccess LIe eIemenL In un ARRAY or In u ST?
eIIne u consLrucLor - wIuL IL Is und Iow IL mIgIL be cuIIed (z meLIods)
qescrIbe PRVATE, PROTECTE und PUBC |u,-uCo LIe dIIIerences und gIve exumpIes
IuL Is u COPY CONSTRUCTOR und wIen Is IL cuIIed (LIIs Is u IrequenL quesLIon !)?
6ExpIuIn Lerm POMORPHSM und gIve un exumpIe usIng eg SHAPE objecL: I Iuve u buse cIuss SHAPE,
Iow wouId deIIne RA meLIods Ior Lwo objecLs CRCE und SQUARE
; IuL Is LIe word you wIII use wIen deIInIng u IuncLIon In buse cIuss Lo uIIow LIIs IuncLIon Lo be u
poIImorpIIc IuncLIon?
8IuL ure z wuys oI exporLIng u IuncLIon Irom u ?
qYou Iuve Lwo puIrs: new() und deIeLe() und unoLIer puIr : uIIoc() und Iree() ExpIuIn dIIIerences beLween eg
new() und muIIoc()
IuL Is u cuIIbuck IuncLIon ExpIuIn In C und C++ und N AP envIronmenL
(rom NOS AP ureu): wIuL Is PARAM und PARAM
Basic concepts of UUPS and Structure of C++ program
AuLhor f ubllshed on 26Lh !ul 2006 | LasL updaLed on 23Lh !ul 2011
In this tutorial you will learn about Objects, Classes, Inheritance, Data Abstraction, Data ncapsulation, Polymorphism,
Overloading, and Reusability.
BeIore starting to learn C it is essential to have a basic knowledge oI the concepts oI Object oriented programming. Some oI
the important object oriented Ieatures are namely:
O b[ecLs
O lasses
O nherlLance
O aLa AbsLracLlon
O aLa LncapsulaLlon
O olymorphlsm
O erloadlng
O eusablllLy
In order to understand the basic concepts in C, a programmer must have a good knowledge oI the basic terminology in object-
oriented programming. Below is a brieI outline oI the concepts oI object-oriented programming languages :
Ub|ects:
Object is the basic unit oI object-oriented programming. Objects are identiIied by its unique name. An object represents a
particular instance oI a class. There can be more than one instance oI a class. ach instance oI a class can hold its own relevant
data.

An Object is a collection oI data members and associated member Iunctions also known as methods.
Classes:
Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to
Iorm a Class. Thus a Class represents a set oI individual objects. Characteristics oI an object are represented in a class as
Properties. The actions that can be perIormed by objects become Iunctions oI the class and are reIerred to as Methods.
For example consider we have a Class oI Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this
context each Car Object will have its own, Model, Year oI ManuIacture, Color, Top Speed, ngine Power etc., which Iorm
Properties oI the Car class and the associated actions i.e., object Iunctions like Start, Move, and Stop Iorm the Methods oI Car
Class.
No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance oI a
class is created.
Inberitance:
Inheritance is the process oI Iorming a new class Irom an existing class or base class. The base class is also known as parent class
or super class. The new class that is Iormed is called derived class. Derived class is also known as a child class or sub class.
Inheritance helps in reducing the overall code size oI the program, which is an important concept in object-oriented
programming.
Data Abstraction:
Data Abstraction increases the power oI programming language by creating user deIined data types. Data Abstraction also
represents the needed inIormation in the program without presenting the details.
Data Encapsulation:
Data ncapsulation combines data and Iunctions into a single unit called Class. When using Data ncapsulation, data is not
accessed directly; it is only accessible through the Iunctions present inside the class. Data ncapsulation enables the important
concept oI data hiding possible.
Polymorpbism:
Polymorphism allows routines to use variables oI diIIerent types at diIIerent times. An operator or Iunction can be given diIIerent
meanings or Iunctions. Polymorphism reIers to a single Iunction or multi-Iunctioning operator perIorming in diIIerent ways.
Uverloading:
Overloading is one type oI Polymorphism. It allows an object to have diIIerent meanings, depending on its context. When an
existing operator or Iunction begins to operate on new data type, or class, it is understood to be overloaded.
Reusability:
This term reIers to the ability Ior multiple programmers to use the same written and debugged existing class oI data. This is a
time saving device and adds code eIIiciency to the language. Additionally, the programmer can incorporate new Ieatures to the
existing class, Iurther developing the application and allowing users to achieve increased perIormance. This time saving Ieature
optimizes code, helps in gaining secured applications and Iacilitates easier maintenance on the application.

Das könnte Ihnen auch gefallen