ANINDAM IT

SEO/SEM

C++

What is a compiler?


This is not a silly question. We were all beginners at one time and asked the same question. The following answer is provided for those that have no programming experience.

A computer cannot understand the spoken or written language that we humans use in our day to day conversations, and likewise, we cannot understand the binary language that the computer uses to do it's tasks. It is therefore necessary for us to write instructions in some specially defined language, in this case C++, which we can understand, then have that very precise language converted into the very terse language that the computer can understand. This is the job of the compiler.

A C++ compiler is itself a computer program who's only job is to convert the C++ program from our form to a form the computer can read and execute. The computer prefers a string of 1's and 0's that mean very little to us, but can be very quickly and accurately understood by the computer. The original C++ program is called the "source code", and the resulting compiled code produced by the compiler is usually called an "object file".

One or more object files are combined with predefined libraries by a linker, sometimes called a binder, to produce the final complete file that can be executed by the computer. A library is a collection of pre-compiled "object code" that provides operations that are done repeatedly by many computer programs.

Any good compiler that you purchase will provide not only a compiler, but an editor, a debugger, a library, and a linker. Online documentation and help files are usually included, and many compilers have a tutorial to walk you through the steps of compiling, linking and executing your first progra

SOME QUESTION FROM C++ ?

 

 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.        

 How do you link a C++ program to C functions?
By using the extern "C" linkage specification around the C function declarations.
Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions.

Explain the scope resolution operator?
It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

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

 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. 

 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 Asset class, Building class, Vehicle 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 infinitum. 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 useful tool that has its perils, needs respect, and is best avoided except when nothing else will do.

What is a virtual destructor?
The simple answer is that a virtual destructor is one that is declared with the virtual attribute.
The behavior of a virtual destructor is what is important. If you destroy an object through a baller or reference 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 compl

 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.

Using tusc to trace the last system call before crash.

Putting Debug statements in the program source code.

 

How the compilers arranges the various sections in the executable image?
The executable had following 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.

 

Explain the ISA and HASA class relationships. How would you implement each in a class design?
    A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class.
This relationship is best implemented by embedding an object of the Salary class in the Employee class.

 

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

 What are the differences between a C++ struct and C++ class?
   The default member and base-class access specifies are different.
This is one of the commonly misunderstood aspects of 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 specifies, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specified and private base-class inheritance.

 

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

 

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

 

What happens when a function throws an exception that was not specified by an exception specification for this function?
Unexpected() is called, which, by default, will eventually trigger abort().

 

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 for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.

 

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

 

Is there any problem with the following:
char *a=NULL; char& p = *a;?
The result is undefined. You should never do this. A reference must always refer to some object.

 

Why do C++ compilers need name mangling?
Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions 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. After all you can write a C++ compiler in C

 

Recent Videos

No recent videos