C++ FAQs

10:45 AM | , | 0 comments »

What are inline functions and macros in c++?



Ans : The inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline. The use of inline functions generates faster code and can sometimes generate smaller code than the equivalent function call generates. Although inline functions are similar to macros (because the function code is expanded at the point of the call at compile time), inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor. As a result, there are several important differences: Inline functions follow all the protocols of type safety enforced on normal functions. Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.

What is the difference between malloc() and calloc() function?



Ans : The calloc() function also allocates memory. Rather than allocating a group of bytes as malloc() does, calloc() allocates a group of objects. The function prototype is void *calloc(size_t num, size_t size); Remember that size_t is a synonym for unsigned on most compilers. The argument num is the number of objects to allocate, and size is the size (in bytes) of each object. If allocation is successful, all the allocated memory is cleared (set to 0), and the function returns a pointer to the first byte. If allocation fails or if either num or size is 0, the function returns NULL.
8

What is the difference between an abstract class and an interface. where exactly would you use an abstract class and an interface.



Ans : Interface is like a class itself that has variables and methods but it contain final variables and abstract methods that is methods are just declared inside the interface, they are defined in a class that implements the interface. Abstract class is a class containing one or more abstract methods, this class can contain non abstract methods as well and it may or may not contain final variables. You cannot create an object of abstract class directly.

What is "this" keyword?



Ans : The keyword this represents within a class the address in memory of the object of that class that is being executed. It is a pointer whose value is always the address of the object. It can be used to check if a parameter passed to a member function of an object is the object itself.

What is difference between #define and typedef?



Ans.#define is a preprocessor directive which defines constant value to a symbol. For eg: #define MAX 100; The symblo MAX will be replaced by 100. Typedef statement is used to give new names to existing data types. Eg: typedef unsigned long ulong; declares ulong to be a new type equivalent to unsigned long.

What is the concept of smart pointers?



A smart pointer is a C++ class for wrapping pointers. By wrapping a pointer in a class(and
specifically, a template), you can make sure certain operations are taken care of
automatically instead of deferring mundane, boilerplate-type operations to the client. One
good example of such an operation is to make sure pointers are initialized correctly so that
embarrassing crashes due to randomly assigned pointers don't occur. Another good example
is to make certain that boilerplate code is executed before function calls are made through a
pointer.

What is the difference between encapsulation and abstraction?



The idea of encapsulation comes from (i) the need to cleanly distinguish between the specification and the implementation of an operation and (ii) the need for modularity. Modularity is necessary to structure complex applications designed and implemented by a team of programmers. It is also necessary as a tool for protection and authorization. There are two views of encapsulation: the programming language view (which is the original view since the concept originated there) and the database adaptation of that view. The idea of encapsulation in programming languages comes from abstract data types. In this view, an object has an interface part and an implementation part. The interface part is the specification of the set of operations that can be performed on the object. It is the only visible part of the object. The implementation part has a data part and a procedural part. The data part is the representation or state of the object and the procedure part describes, in some programming language, the implementation of each operation. The database translation of the principle is that an object encapsulates both program and data. In the database world, it is not clear whether the structural part of the type is or is not part of the interface (this depends on the system), while in the programming language world, the data structure is clearly part of the implementation and not of the interface. Consider, for instance, an Employee. In a relational system, an employee is represented by some tuple. It is queried using a relational language and, later, an application programmer writes programs to update this record such as to raise an Employees salary or to fire an Employee. These are generally either written in a imperative programming language with embedded DML statements or in a fourth generation language and are stored in a traditional file system and not in the database. Thus, in this approach, there is a sharp distinction between program and data, and between the query language (for ad hoc queries) and the programming language (for application programs). In an object-oriented system, we define the Employee as an object that has a data part (probably very similar to the record that was defined for the relational system) and an operation part, which consists of the raise and fire operations and other operations to access the Employee data. When storing a set of Employees, both the data and the operations are stored in the database. Thus, there is a single model for data and operations, and information can be hidden. No operations, outside those specified in the interface, can be performed. This restriction holds for both update and retrieval operations. Encapsulation provides a form of ``logical data independence: we can change the implementation of a type without changing any of the programs using that type. Thus, the application programs are protected from implementation changes in the lower layers of the system. We believe that proper encapsulation is obtained when only the operations are visible and the data and the implementation of the operations are hidden in the objects. However, there are cases where encapsulation is not needed, and the use of the system can be significantly simplified if the system allows encapsulation to be be violated under certain conditions. For example, with ad-hoc queries the need for encapsulation is reduced since issues such as maintainability are not important. Thus, an encapsulation mechanism must be provided by an OODBS, but there appear to be cases where its enforcement is not appropriate.

8 . What is garbage?



Garbage is data that has been placed in random access memory space obtained from the operating system that is no longer needed. Freeing the space for reuse is called "garbage collecting." In the past, programmers have had to write programs that explicitly requested storage and then returned it to the system when it was no longer needed. Java is a newer programming language that, like LISP, handles garbage-collecting for the program, freeing the programmer from being concerned about it.

What is the difference between heap and stack?



A heap is an area of pre-reserved computer main storage (memory) that a program process can use to store data in some variable amount that won't be known until the program is running. For example, a program may accept different amounts of input from one or more users for processing and then do the processing on all the input data at once. Having a certain amount of heap storage already obtained from the operating system makes it easier for the process to manage storage and is generally faster than asking the operating system for storage every time its needed. The process manages its allocated heap by requesting a "chunk" of the heap (called a heap block) when needed, returning the blocks when no longer needed, and doing occasional "garbage collecting," which makes blocks available that are no longer being used and also reorganizes the available space in the heap so that it isn't being wasted in small unused pieces. The term is apparently inspired by another term, stack. A stack is similar to a heap except that the blocks are taken out of storage in a certain order and returned in the same way.

What is difference between Template and function in c++ and why we use templates?



Template - a parameterized type. A template can accept type parameters that are used to customize the resulting type. Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones: template function_declaration; template <typename indetifier> function_declaration; the only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way. For example, to create a template function that returns the greater one of two objects we could use: template <class GenericType> GenericType GetMax (GenericType a, GenericType b) { return (a>b?a:b); } As specifies the first line, we have created a template for a generic data type that we have called GenericType. Therefore in the function that follows, GenericType becomes a valid data type and it is used as type for its two parameters a and b and as return value for the function GetMax. GenericType still does not represent any concrete data type; when the function GetMax will be called we will be able to call it with any valid data type. This data type will serve as pattern and will replace GenericType in the function. The way to call a template class with a type pattern is the following: function <pattern> (parameters); Thus, for example, to call GetMax and to compare two integer values of type int we can write: int x,y; GetMax <int> (x,y); so GetMax will be called as if each appearance of GenericType was replaced by an int expression.

What is name Mangling? How is it handled in c++ and in com



Both C++ and Java provide overloaded function and methods, which are methods with the same types but different parameter lists. Selecting the correct version is done at compile time. Though the overloaded functions have the same name in the source code, they need to be translated into different assembler-level names, since typical assemblers and linkers cannot handle overloading. This process of encoding the parameter types with the method name into a unique name is called name mangling. The inverse process is called demangling. It is convenient that C++ and Java use compatible mangling schemes, since the makes life easier for tools such as gdb, and it eases integration between C++ and Java. Note there is also a standard "Java Native Interface" (JNI) which implements a different calling convention, and uses a different mangling scheme. The JNI is a rather abstract ABI so Java can call methods written in C or C++; we are concerned here about a lower-level interface primarily intended for methods written in Java, but that can also be used for C++ (and less easily C). Method name mangling C++ mangles a method by emitting the function name, followed by __, followed by encoding of any method qualifiers (such as const), followed by the mangling of the methods class, followed by the mangling of the parameters, in order. For example Foo::bar(int, long) const is mangled as `bar__C3Fooil. For a constructor, the method name is left out. That is Foo::Foo(int, long) const is mangled as `__C3Fooil.

What is namespace in C++ ?


C++ supports the concept of a namespace, to help avoid naming conflicts that inevitably occur in software development. A problem that people ran into when writing software was that two different pieces of code might logically want to use the same identifier names. A namespace allows you to enclose all of your identifiers inside of a named entity. So, for example, if you had a function named bar in the foo namespace, to access it you would use the fully qualified name foo::bar. The namespace is separated from the identifier by a double colon. By using a namespace, you have at most one conflict: your namespace name, which is much easier to change than hundreds or even thousands of other identifiers

What is the difference between namespaces and structures?
A namespace declaration identifies and assigns a name to a declarative region. The syntax is : Namespace [identifier] {namespace-body} The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members. The declarative region of a namespace declaration is its namespace-body. A structure type is a user-defined composite type. It is composed of "fields" or "members" that can have different types. In C, you must explicitly use the struct keyword to declare a structure. In C++, this is unnecessary once the type has been defined.

What is lvalue and rvalue?


C and C++ have the notion of lvalues and rvalues associated with variables and constants. The rvalue is the data value of the variable, that is, what information it contains. The "r" in rvalue can be thought of as "read" value. A variable also has an associated lvalue. The "l" in lvalue can be though of as location, meaning that a variable has a location that data or information can be put into. This is contrasted with a constant. A constant has some data value, that is an rvalue. But, it cannot be written to. It does not have an lvalue. Another view of these terms is that objects with an rvalue, namely a variable or a constant can appear on the right hand side of a statement. They have some data value that can be manipulated. Only objects with an lvalue, such as variable, can appear on the left hand side of a statement. An object must be addressable to store a value. Ex: int x; x = 5; // Here, 5 is an rvalue, x can be an lvalue

0 comments