Persistent Systems Most Frequently Asked C++ Interview Questions

Write A Program That Implements A Date Class Containing Day, Month And Year As Data Members. Implement Assignment Operator And Copy Constructor In This Class.

This is shown in following program

#include

class date
{
     private
        int day ;
        int month ;
        int year ;
     public
        date ( int d = 0, int m = 0, int y = 0 )
        {
            day = d ;
            month = m ;
            year = y ;
        }
        // copy constructor
        date ( date &d )
        {
            day = d.day ;
            month = d.month ;
            year = d.year ;
        }
        // an overloaded assignment operator
        date operator = ( date d )
        {
            day = d.day ;
            month = d.month ;
            year = d.year ;
            return d ;
        }
        void display( )
        {
            cout << day << "/" << month << "/" << year ;
        }
} ;
void main( )
{
        date d1 ( 25, 9, 1979 ) ;
        date d2 = d1 ;
        date d3 ;
        d3 = d2 ;
        d3.display( ) ;
}

Write About C++ Storage Classes?

The storage classes are qualifiers that are used to specify the lifetime of a variable. The lifetime of a variable relates to the portion of the program that has access to the variable. Storage class directs the compiler about how to store the variable in memory and how to access variable within the program.
The following are the different types of storage classes

 Auto —Identifies the local variable as automatic, which means that each invocation of the statement block in which the variable is defined gets a fresh copy with its own memory space and with re-initialization each time.
 Static —Refers that the scope of a static local variable begins inside the statement block in which the variable is declared and ends when the block terminates. The variable itself retains its value between executions of the statement block.
 Extern —Declares a global variable in one program that can be accessed by another program. The default value of an extern variable is zero. This variable is useful in a scenario where we divide one large program into different small programs and use external variable in each small program. The main advantage of using external variable is that the complexity of a program can be reduced by separating a large program into smaller programs and using external variable, which is shared by all the programs.
 Register —Refers that a variable declared with the register storage class is the same as an auto variable except that the program cannot take the variable's address. Its purpose is to allow the programmer to specify conditions under which the program's performance would be improved if certain local and automatic variables were maintained in one of the computer's hardware registers.

What Are The Advantages Of Using Const Reference Arguments In A Function?

The following are the advantages of using the const reference arguments in a function
a. Protects against errors that result in altering data of a program.
b. Allows the processing of const and non-const actual arguments by the function. On the contrary, in the prototype, the acceptance of only non-constant arguments is done by the function without a const.
c. Allows the generation and usage of a temporary variable by the function appropriately.

What Kind Of Problems Can Be Solved By A Namespace?

The namespace feature is used to avoid the name collision caused due to the use of the global identifiers by multiple providers of libraries. The provider of libraries avoids such name collisions by assigning the unique namespace to the libraries. The namespace feature is a logical space which uniquely identifies a resource, such as a program or class.
The declaration of a namespace is given as follows

namespace[identifier]
{
namespace-body
}
Persistent Systems Most Frequently Asked C++ Interview Questions Answers
Persistent Systems Most Frequently Asked C++ Interview Questions Answers

Why Do We Use The Using Declaration?

A using declaration specifies that all the identifiers in the namespace are available to the program within the scope of the using declaration. It makes all the namespace's identifiers available to the program in the context of their own outer scope. The principle use of the using declaration is to support standard library interfaces that are well known.

Write About An Iterator Class?

Iterator class provides an access to the classes which are inside the containers (it holds a group of objects in an organized way). The containers include the data structure, class, and abstract data type. Each container type supports one category of iterator depending on the container's requirements. The categories are input, output, forward, bidirectional, and random access. These properties specify the behavior that the iterator must exhibit in order to support the container. Iterators can be initialized, incremented, and decremented, and their bounds can be limited to the current extent of the containers. If you can cause an iterator to be equal to another iterator by incrementing the first one, the second iterator is reachable from the first. The two iterators are also known to refer to the same container. The two iterators can therefore define a range of objects in the container.

Write About The Stack Unwinding?

Stack unwinding is a process in which a destructor is invoked in a particular program for destroying all the local objects in the stack between throwing and catching of an exception.

In Which Situation The Program Terminates Before Reaching The Breakpoint Set By The User At The Beginning Of The Mainq Method?

In C++, the initialization of variables is allowed before the calling of the main() method. If a function is invoked by the global variable's initialization and termination is done, then it results in the error before entering of the main() method.

If I Is An Integer Variable, Which Is Faster ++i Or I++?

In ++i which is a pre-increment method, there is only one instruction which is needed for incrementing the variable for returning a new value. In the case of i++ which is a post-incremental method, there is a need for two instructions -one for saving the old compiler which is used in the expression and the other for the incrementation of the variable. In the post-incremental method, first the old value is returned and then the variable is incremented. This process is slower than the pre-incremental process.

How Can I Disable The "echo" Feature?

The disabling of the echo feature is done with the help of the getpassQ function. The character keys are turned off by using the getpass(3) function.

How Can You Link A C Program With A C Function?

A C program can be linked with a C function with the help of the extern "C" linkage specification. The name mangling in C++ encodes all the codes into the symbol and therefore results in errors. Therefore, the references of these symbols should be avoided by the C compiler. One should have the knowledge about the mangled function and type-safe linkages. During the compilation, with the extern "C" linkage specification, the name mangling feature is turned off to ensure the proper linkage of the C program to the C functions.

How Are The Features Of C++ Different From C?

All the features of C are similar to C++ except some features, such as polymorphism, inheritance, operator overloading which are supported in C++ but not in C language. Both C and C++ language is similar in their functionality but C++ provides with more tools and options.

How Can A Struct In C++ Differs From A Struct In C?

The differences between struct in C++ and C are listed in the following points

 In C and C++, the variables of the structures are public; however, in C, the variable cannot be declared as private or protected. On the contrary, in C++, the variables can be declared as private or protected.
On declaring a struct in C, the addition of the struct keyword is must. On the contrary, there is no need of the struct keyword on declaring struct in C++.
 In C, structures do not have direct functions or methods (procedures). Example, pointers inside the functions. On the other hand, C++ has direct functions or methods, for example classes.
 In C, the initialization cannot be done outside the scope of a structure. However, in C++, the initialization can be done outside the scope of a structure.
 In C, the concept of inheritance is not supported. In C++, the concept of inheritance is fully supported.

Post a Comment

Previous Post Next Post