Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Write A Code Snippet To Display An Integer In A Binary Format?

Answer»

 

 

2.

Explain The Concept Of Dynamic Allocation Of Memory?

Answer»

In C++, memory is divided into three areas, AUTOMATIC storage (where function's variables are allocated and stored), static storage (where static data is stored and LEFT for the duration of the program's runtime) and the free storage (the memory set for the use of your code at runtime; the new and DELETE operators are used to allocate and handle space in the free storage).
Allocating the memory dynamically means that you can allocate space for as many elements as you need at runtime. The memory can be allocated dynamically with the new operator and free with the delete operator. Let's understand it with the help of an example-


In the preceding code snippet, allocation of memory is done for a double variable, PI, by using the new operator. This operator returns a pointer to a double variable.

In C++, memory is divided into three areas, automatic storage (where function's variables are allocated and stored), static storage (where static data is stored and left for the duration of the program's runtime) and the free storage (the memory set for the use of your code at runtime; the new and delete operators are used to allocate and handle space in the free storage).
Allocating the memory dynamically means that you can allocate space for as many elements as you need at runtime. The memory can be allocated dynamically with the new operator and free with the delete operator. Let's understand it with the help of an example-


In the preceding code snippet, allocation of memory is done for a double variable, pi, by using the new operator. This operator returns a pointer to a double variable.

3.

Write About The Local Class And Mention Its Use?

Answer»

A local class is DEFINED as the abstract characteristics of an object, such as attributes, fields, or properties. It also defines the behaviors of objects, such as the METHODS, operations, or its features. In other WORDS, you can say classes describe objects. A class nested inside the SCOPE of the local helps in managing the codes. Let's TAKE an example:

A local class is defined as the abstract characteristics of an object, such as attributes, fields, or properties. It also defines the behaviors of objects, such as the methods, operations, or its features. In other words, you can say classes describe objects. A class nested inside the scope of the local helps in managing the codes. Let's take an example:

4.

Write About A Nested Class And Mention Its Use?

Answer»

A nested class is a class, DEFINED within the scope of another class. Let's take an example:


Nested CLASSES are USED to organize the codes and control the access of DIFFERENT codes. In the preceding example, if the access specifier is made PUBLIC for the nested classes, then the name should be classA (outer class):: classD(nested class). If the access specifier is made private, then the nested class can be accessed by the members of the outer class only.

A nested class is a class, defined within the scope of another class. Let's take an example:


Nested classes are used to organize the codes and control the access of different codes. In the preceding example, if the access specifier is made public for the nested classes, then the name should be classA (outer class):: classD(nested class). If the access specifier is made private, then the nested class can be accessed by the members of the outer class only.

5.

Define Pure Virtual Function?

Answer»

Pure virtual function is defined as a virtual function in a BASE class. It is IMPLEMENTED in a DERIVED class. A program may not DECLARE an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an overriding function for each pure virtual function in the base. A pure virtual function can be created through the following WAY:

Pure virtual function is defined as a virtual function in a base class. It is implemented in a derived class. A program may not declare an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an overriding function for each pure virtual function in the base. A pure virtual function can be created through the following way:

6.

How Does List R; Differs From List R();?

Answer»

Suppose that MEMBER is the name of some class. Then, function f() DECLARES a local member object called R: Example:


In the preceding CODE snippet, r is declared as a local variable and the other r() is declared as a function. That is the main difference.

Suppose that Member is the name of some class. Then, function f() declares a local member object called r: Example:


In the preceding code snippet, r is declared as a local variable and the other r() is declared as a function. That is the main difference.

7.

Explain How Would You Handle A Situation Where You Cannot Call The Destructor Of A Local Explicitly?

Answer»

To avoid the explicit CALLING of a destructor, the TIME extent of the LOCAL VARIABLE can be wrapped in an artificial block {...}:

To avoid the explicit calling of a destructor, the time extent of the local variable can be wrapped in an artificial block {...}:

8.

What Is The Sequence Of Destruction Of Local Objects?

Answer»

The destructor is executed in the REVERSE order of a CONSTRUCTOR. Firstly, the OBJECTS are constructed, METHODS are INVOKED on them, and then they are destructed. In the following example, a's destructor will be executed after b's destructor.

The destructor is executed in the reverse order of a constructor. Firstly, the objects are constructed, methods are invoked on them, and then they are destructed. In the following example, a's destructor will be executed after b's destructor.

9.

Define A Conversion Constructor?

Answer»

A CONVERSION constructor is a single argument constructor. It is used by the compiler to convert a type of object as an argument to a class type.

The above example has the FOLLOWING two conversion constructors:

  •  Y(inti)— Helps in the conversion of integers to OBJECTS of class Y.
  •  Y(const char* n, int j = 0) —Helps in the conversion of pointers from strings to objects of the Y class

A conversion constructor is a single argument constructor. It is used by the compiler to convert a type of object as an argument to a class type.

The above example has the following two conversion constructors:

10.

Is It Possible To Pass An Object Of The Same Class In Place Of Object Reference To The Copy Constructor?

Answer»

Yes, when an object is created without the reference formal ARGUMENT, then the copy constructor is called for the argument object. The object that is passed as a parameter to the function is then passed as a parameter to the copy constructor.
For example:


In this CASE, the copy constructor is called for A2 while A1 is passed as a parameter to the copy constructor.

Yes, when an object is created without the reference formal argument, then the copy constructor is called for the argument object. The object that is passed as a parameter to the function is then passed as a parameter to the copy constructor.
For example:


In this case, the copy constructor is called for A2 while A1 is passed as a parameter to the copy constructor.

11.

Explain The Concept Of Copy Constructor?

Answer»

Copy CONSTRUCTOR is a special TYPE of parameterized constructor. It copies one OBJECT to another. It is called when an object is created and equated to an EXISTING object at the same time. An existing object is PASSED as a parameter to it.

Copy constructor is a special type of parameterized constructor. It copies one object to another. It is called when an object is created and equated to an existing object at the same time. An existing object is passed as a parameter to it.

12.

Define What Is Constructor ?

Answer»

A constructor is a method that is called when a NEW instance of an object of a class is created. It INITIALIZES all class members whenever you create an object of the class. It is a member function with the same name as its class. It may or may not take any parameters. It does not have any return type. For example, the compiler embeds a call to the constructor for each object when it is created. SUPPOSE a class Z has been declared as FOLLOWS:


We cannot DECLARE a constructor as virtual or static, nor we can declare a constructor as const, volatile, or const volatile. The short form of constructor is ctor.

A constructor is a method that is called when a new instance of an object of a class is created. It initializes all class members whenever you create an object of the class. It is a member function with the same name as its class. It may or may not take any parameters. It does not have any return type. For example, the compiler embeds a call to the constructor for each object when it is created. Suppose a class Z has been declared as follows:


We cannot declare a constructor as virtual or static, nor we can declare a constructor as const, volatile, or const volatile. The short form of constructor is ctor.

13.

Describe The Role Of The C++ In The Tradeoff Of Safety Vs. Usability?

Answer»

Earlier in C, encapsulation is meant to hide the information. Encapsulation is included in C by making other class members static in a class to prevent accessing the members by another module. However, the concept of multiple instances is not supported by the encapsulation because it is impossible to directly make multiple instances inside the static class module. If there is a need of multiple instances, a struct is used. On the CONTRARY, encapsulation is not supported by structs.
In C++, a class supports both encapsulation and multiple instances. The class's interface that contains the public MEMBER functions and friend functions of the class can be accessed by multiple users. On the other hand, an implementation of the class is defined by the private and protected parts of a class. This encapsulated part forms a struct; thereby, reducing the TRADEOFF (loosing of ONE quality as a result of gaining of another quality) between the encapsulation and usability.

Earlier in C, encapsulation is meant to hide the information. Encapsulation is included in C by making other class members static in a class to prevent accessing the members by another module. However, the concept of multiple instances is not supported by the encapsulation because it is impossible to directly make multiple instances inside the static class module. If there is a need of multiple instances, a struct is used. On the contrary, encapsulation is not supported by structs.
In C++, a class supports both encapsulation and multiple instances. The class's interface that contains the public member functions and friend functions of the class can be accessed by multiple users. On the other hand, an implementation of the class is defined by the private and protected parts of a class. This encapsulated part forms a struct; thereby, reducing the tradeoff (loosing of one quality as a result of gaining of another quality) between the encapsulation and usability.

14.

Write About The Access Privileges In C++ And Also Mention About Its Default Access Level?

Answer»

There are THREE access privileges PROVIDED in C++ which are private, protected, and public. The members of a class have private access by default.The public members can be accessed by member FUNCTIONS and by other functions that declare an INSTANCE of the class. The private members can be accessed by member functions of the same class. The protected members are included by using the class inheritance. The protected members can be accessed only within the class.

There are three access privileges provided in C++ which are private, protected, and public. The members of a class have private access by default.The public members can be accessed by member functions and by other functions that declare an instance of the class. The private members can be accessed by member functions of the same class. The protected members are included by using the class inheritance. The protected members can be accessed only within the class.

15.

What Happens When The Extern "c" Char Func (char*,waste) Executes?

Answer»

On executing the extern "C" char FUNC (char*,WASTE), the link to the code compiled by the C compiler can be ESTABLISHED by the programmer because the "name mangling" (which includes the tokens that identify the function's return TYPE and the types of its ARGUMENTS) will be turn off for the function.

On executing the extern "C" char func (char*,waste), the link to the code compiled by the C compiler can be established by the programmer because the "name mangling" (which includes the tokens that identify the function's return type and the types of its arguments) will be turn off for the function.

16.

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

Answer»

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

  1.  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.
  2. 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++.
  3.  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.
  4.  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.
  5.  In C, the concept of inheritance is not supported. In C++, the concept of inheritance is fully supported.

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

17.

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

Answer»

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.

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.

18.

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

Answer»

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.

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.

19.

How Can I Disable The "echo" Feature?

Answer»

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.

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.

20.

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

Answer»

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.

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.

21.

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

Answer»

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.

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.

22.

Write About The Stack Unwinding?

Answer»

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.

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.

23.

Write About An Iterator Class?

Answer»

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.

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.

24.

Why Do We Use The Using Declaration?

Answer»

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.

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.

25.

What Kind Of Problems Can Be Solved By A Namespace?

Answer»

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 }

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:

26.

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

Answer»

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.

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.

27.

Write About C++ Storage Classes?

Answer»

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.

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:

28.

How A Macro Differs From A Template?

Answer»

A macro defines the meaning for an identifier.The preprocessor replaces macro in the form of strings in the source code with values DERIVED from the macro definition.The most common usage for macros defines a GLOBAL symbol that represents a VALUE. A macro cannot call itself.

On the contrary, a template allows you to create generic FUNCTIONS that admit any data type as parameters and return a value without having to OVERLOAD the function with all the possible data types. A template can call itself.

A macro defines the meaning for an identifier.The preprocessor replaces macro in the form of strings in the source code with values derived from the macro definition.The most common usage for macros defines a global symbol that represents a value. A macro cannot call itself.

On the contrary, a template allows you to create generic functions that admit any data type as parameters and return a value without having to overload the function with all the possible data types. A template can call itself.

29.

What Is Meant By The Term Name Mangling In C++?

Answer»

The mangled name includes TOKENS that identify the function's return type and the types of arguments. Calls to the function and the function definition itself are recorded in the relocatable object file as REFERENCES to the mangled name, which is unique even though several functions might have the same similar name. Through name mangling, the name of the function is changed into coded and unique names or symbols which can be UNDERSTAND by the USER. In this way, the function with the same name can be DIFFERENTIATED with the help of this coded language.

The mangled name includes tokens that identify the function's return type and the types of arguments. Calls to the function and the function definition itself are recorded in the relocatable object file as references to the mangled name, which is unique even though several functions might have the same similar name. Through name mangling, the name of the function is changed into coded and unique names or symbols which can be understand by the user. In this way, the function with the same name can be differentiated with the help of this coded language.

30.

Discuss The Possibilities Related To The Termination Of A Program Before Entering The Mainq Method?

Answer»

The global variables are initialized dynamically before invoking the MAIN() METHOD. The PROCESS of invoking the global variables is slow. If the function is called by INITIALIZATION of the global variables, then the program is terminated before entering the main() method.

The global variables are initialized dynamically before invoking the main() method. The process of invoking the global variables is slow. If the function is called by initialization of the global variables, then the program is terminated before entering the main() method.

31.

Write About The Various Sections Of The Executable Image?

Answer»

The SECTIONS of an executable image are as follows:
a. The different sections of DATA, such as the SECTION of the data which is not initialized and the initialized section of the VARIABLE of the data
b. The specific section of the code
c. The ALLOCATION of all the static variables is done in the variable section which is initialized.

The sections of an executable image are as follows:
a. The different sections of data, such as the section of the data which is not initialized and the initialized section of the variable of the data
b. The specific section of the code
c. The allocation of all the static variables is done in the variable section which is initialized.

32.

Name The Debugging Methods That Are Used To Solve Problems?

Answer»

The following are the debugging methods used by C++ to SOLVE PROBLEMS:

  •  GOB—Allows debugging of the global objects which have the reference
  •  Forte —Used as a debugger for building the application based on high performance
  •  VISUAL ^:udio —Traces the program's source code and supports break-points, WATCHPOINTS, and threads
  •  Tusc —Traces the last call of the SYSTEM before its termination

The following are the debugging methods used by C++ to solve problems:

33.

Is It Possible For A Member Function To Delete The Pointer, Named This?

Answer»

It is POSSIBLE for a member function to use delete this pointer but on certain conditions.
The conditions which allow the member function to use delete this are listed in the following ORDER:

  1. Make sure that the allocation of this (current) object is DONE through new [] operator only.
  2. Make sure that the invocation of your member function done on this (current) object will be the last member function. It made the calling of the last member function by the current object easy.
  3. Make sure that calling of other member functions and data members should not be done after the line of code which includes delete this.
  4. Examination or comparison of this pointer with other pointers and with NULL, printing or casting it, MUST be avoided after using the delete this line.

It is possible for a member function to use delete this pointer but on certain conditions.
The conditions which allow the member function to use delete this are listed in the following order:

34.

Write About The Scope Resolution Operator?

Answer»

The scope resolution operator (::) can be used to define the member functions of a program outside the BOUNDARY of a class and not WITHIN the class specifier. The global scope resolution operator, which is coded as a prefix to the variable's NAME (for example,:: varname), lets you explicitly reference a global variable from a scope in which a local variable has the same name.

A program can USE scope resolution operator (::) to BYPASS the override of a base class member that a derived class has overridden.

The scope resolution operator (::) can be used to define the member functions of a program outside the boundary of a class and not within the class specifier. The global scope resolution operator, which is coded as a prefix to the variable's name (for example,:: varname), lets you explicitly reference a global variable from a scope in which a local variable has the same name.

A program can use scope resolution operator (::) to bypass the override of a base class member that a derived class has overridden.

35.

Is Recursion Allowed In Inline Functions?

Answer»

Syntactically, the RECURSION (calling of the function by itself) is allowed in INLINE function but PRACTICALLY, the inline FUNCTIONS and their properties do not remain inside the program. Moreover, the compiler is not sure about the depth of the recursion at the time of compilation.

Syntactically, the recursion (calling of the function by itself) is allowed in inline function but practically, the inline functions and their properties do not remain inside the program. Moreover, the compiler is not sure about the depth of the recursion at the time of compilation.

36.

Why Should We Use Null Or Zero In A Program?

Answer»

In C++, <cstdio> header defines NULL, a global SYMBOL that represents a null pointer. NULL has nothing to do with standard input/output except that some functions return a null pointer. C++ programmers do not USE the NULL global symbol, preferring to address zero pointer values with the CONSTANT INTEGER value 0.

Another problem is that ignoring a 0 return could crash the system when the program tries to assign values through a zero-value pointer.

In C++, <cstdio> header defines NULL, a global symbol that represents a null pointer. NULL has nothing to do with standard input/output except that some functions return a null pointer. C++ programmers do not use the NULL global symbol, preferring to address zero pointer values with the constant integer value 0.

Another problem is that ignoring a 0 return could crash the system when the program tries to assign values through a zero-value pointer.

37.

How The Programmer Of A Class Should Decide Whether To Declare Member Function Or A Friend Function?

Answer»

A programmer should analyze the situations or requirements for deciding WHETHER to declare the member function or friend function for a class. When a function is declared as a friend, the function is able to access all the PRIVATE and protected member of the class. A member function can be used with encapsulation point of VIEW as friend functions violate the encapsulation and can also access all the private members ALTHOUGH they are not a PART of the class. From the security point of view, use of member function is safer than the friend function.

A programmer should analyze the situations or requirements for deciding whether to declare the member function or friend function for a class. When a function is declared as a friend, the function is able to access all the private and protected member of the class. A member function can be used with encapsulation point of view as friend functions violate the encapsulation and can also access all the private members although they are not a part of the class. From the security point of view, use of member function is safer than the friend function.

38.

Explain The Concept Of Friend Function In C++?

Answer»

The friend function in C++ refers to a class or function that WORKS with the private or protected members of the other class. It gets privileges to ACCESS and work with the private members of the other class. The programmer has full CONTROL over both the friend and member FUNCTIONS of the class and can use the features of both. The friend function does not require the use of an object and can be invoked WITHOUT creating the object.

The friend function in C++ refers to a class or function that works with the private or protected members of the other class. It gets privileges to access and work with the private members of the other class. The programmer has full control over both the friend and member functions of the class and can use the features of both. The friend function does not require the use of an object and can be invoked without creating the object.

39.

Specify Some Guidelines That Should Be Followed While Overloading Operators?

Answer»

Following are some of the guidelines that should be followed while OVERLOADING operators:

  1.  Subscript bracket operators should be USED for overloading when there is a need of fetching DATA from the container class.
  2. Arithmetic operators should be used for providing the numerical CALCULATION to the operators.
  3. Comma operator overloading should be used less often as the ordering properties of this operator vary before and after overloading. This variation in the ordering properties confuses the users of this operator.
  4. The priority order in which operators are EXECUTED cannot be modified by overloading the operators.
  5. The overloaded operators must follow the syntax of the language in which they are used.

Following are some of the guidelines that should be followed while overloading operators:

40.

What Is The Main Purpose Of Overloading Operators?

Answer»

The main purpose of operator overloading is to minimize the chances of OCCURRENCE of errors in a class that is USING the overloaded operators. It ALSO helps in redefining the FUNCTIONALITIES of the operators to improve their performance. Operator overloading also makes the PROGRAM clearer, readable, and more understandable by using common operators, such as +, =, and [].

The main purpose of operator overloading is to minimize the chances of occurrence of errors in a class that is using the overloaded operators. It also helps in redefining the functionalities of the operators to improve their performance. Operator overloading also makes the program clearer, readable, and more understandable by using common operators, such as +, =, and [].

41.

Can The Creation Of Operator** Is Allowed To Perform The To-the-power-of Operations?

Answer»

No, you cannot create OPERATOR** for to-the-power-of operations. The number of PARAMETERS taken by an operator, names of the operators, priority level of the operators, and the ASSOCIATIVITY of the operators depend on LANGUAGE in which they are being used. The operator** is not present in C++, so it cannot be created.

No, you cannot create operator** for to-the-power-of operations. The number of parameters taken by an operator, names of the operators, priority level of the operators, and the associativity of the operators depend on language in which they are being used. The operator** is not present in C++, so it cannot be created.

42.

Can The Operator == Be Overloaded For Comparing Two Arrays Consisting Of Characters By Using String Comparison?

Answer»

The operator == cannot be OVERLOADED to compare two arrays consisting of characters using string COMPARISONS. It should be noted that out of the two operands of an overloaded operator, at least one operand should be of USER- defined TYPE. This user-defined type usually refers to a class. Two characters can be compared easily using classes, such as std::string, rather than using an ARRAY containing characters.

The operator == cannot be overloaded to compare two arrays consisting of characters using string comparisons. It should be noted that out of the two operands of an overloaded operator, at least one operand should be of user- defined type. This user-defined type usually refers to a class. Two characters can be compared easily using classes, such as std::string, rather than using an array containing characters.

43.

Provide Some Examples Of Operator Overloading?

Answer»

The implementation of operator overloading is done in the following WAYS:

  1. Concatenating two std:-.string objects by using the + operator
  2.  Incrementing a Date object by using the ++ operator
  3. Multiplying two different number objects by using the * operator
  4. Accessing array ELEMENTS from an object of the Array CLASS by using the subscript operator

The implementation of operator overloading is done in the following ways:

44.

Describe The Advantages Of Operator Overloading?

Answer»

Operator overloading is used to provide some extra FEATURES, behaviors, and abilities to the USERS of a PARTICULAR CLASS. This feature in C++ helps in controlling the functions performed by an operator and reduces the chance of occurrence of errors in a program.

Operator overloading is used to provide some extra features, behaviors, and abilities to the users of a particular class. This feature in C++ helps in controlling the functions performed by an operator and reduces the chance of occurrence of errors in a program.

45.

What Is The Difference Between Prefix And Postfix Versions Of Operator++()?

Answer»

The prefix and postfix versions of operator ++() can be DIFFERENTIATED on the basis of arguments defined. The postfix operator ++() CONSISTS of a dummy parameter of INT datatype; WHEREAS, a dummy parameter is not found in the prefix operator ++().

The prefix and postfix versions of operator ++() can be differentiated on the basis of arguments defined. The postfix operator ++() consists of a dummy parameter of int datatype; whereas, a dummy parameter is not found in the prefix operator ++().

46.

Explain How Overloading Takes Place In C++?

Answer»

C++ supports two TYPES of overloading namely, function overloading and operator overloading. Function overloading helps in defining more than ONE function with the same name, but with different signatures. An error is raised if two overloaded functions are PROVIDED with the same function signature. Operator overloading helps in giving special meanings to operators, when they are used with user-defined classes.

C++ supports two types of overloading namely, function overloading and operator overloading. Function overloading helps in defining more than one function with the same name, but with different signatures. An error is raised if two overloaded functions are provided with the same function signature. Operator overloading helps in giving special meanings to operators, when they are used with user-defined classes.

47.

How Const Int *ourpointer Differs From Int Const *ourpointer?

Answer»

As a rule, POINTER declarations should be read from RIGHT to left. In the pointer declaration, const INT *ourPointer, ourPointer is a pointer to a const int object, which cannot be changed by using a pointer. Whereas in CASE of the int const *ourPointer pointer declaration, ourPointer is a const pointer to an int object, in which an int object can be changed by using a pointer but the pointer itself cannot be changed because it is constant.

As a rule, pointer declarations should be read from right to left. In the pointer declaration, const int *ourPointer, ourPointer is a pointer to a const int object, which cannot be changed by using a pointer. Whereas in case of the int const *ourPointer pointer declaration, ourPointer is a const pointer to an int object, in which an int object can be changed by using a pointer but the pointer itself cannot be changed because it is constant.

48.

How A Pointer Differs From A Reference?

Answer»

A POINTER DIFFERS from a reference in the following ways:

  1.  In the case of reference, an object must always be referred while initializing. On the contrary, such restrictions are not MEANT for pointers.
  2. The different types of objects can be pointed by the pointers by reassigning the pointers with different objects. On the contrary, in a reference, the same object which was initialized earlier can only be referred by a reference.
  3. You can USE a null ADDRESS in a pointer parameter to indicate that a variable does not exist; whereas, there is no existence of a null reference in C++.
  4.  A reference usually appears outside an object; whereas, a pointer generally appears inside an object.

A pointer differs from a reference in the following ways:

49.

What Are Smart Pointers?

Answer»

Smart POINTERS are almost similar to pointers with additional features, such as AUTOMATIC destruction of a variable when it becomes out of scope and the throwing of exceptions that ensures the proper destruction of the dynamically allocated objects. They are useful in keeping tracks of dynamically allocated objects. Due to these additional capabilities, they REDUCE the possibilities of occurrence of exceptions and errors in a program and ENSURE that the written code is safe and efficient.

Smart pointers are almost similar to pointers with additional features, such as automatic destruction of a variable when it becomes out of scope and the throwing of exceptions that ensures the proper destruction of the dynamically allocated objects. They are useful in keeping tracks of dynamically allocated objects. Due to these additional capabilities, they reduce the possibilities of occurrence of exceptions and errors in a program and ensure that the written code is safe and efficient.

50.

List The Issue That The Auto_ptr Object Handles?

Answer»

The auto_ptr OBJECT is USED to deallocate MEMORY, which is ALLOCATED to a variable, when the variable goes out of scope.

The auto_ptr object is used to deallocate memory, which is allocated to a variable, when the variable goes out of scope.