C++ Type Checking

  Home  C++ Programming  C++ Type Checking


“C++ Type Checking frequently Asked Questions by expert members with experience in C++ type checking. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”



27 C++ Type Checking Questions And Answers

9⟩ Tell me What is when is dynamic checking necessary?

Dynamic checking is necessary in the following scenarios:

► Whenever the definition of a variable is not necessary before its usage.

► When implicit conversion of variables into other types.

► When the program is to be compiled independently as there is no type checking at compile time.

 232 views

10⟩ What is dynamic type checking?

Dynamic type checking performs the type checking operation at the time of the program execution. To perform this operation, the arguments, expressions, variables must be given a data type.

 260 views

11⟩ What is static type checking?

Static type checking performs the type checking operation before the execution of the program. To perform this operation, the arguments, expressions, variables must be given a data type.

 242 views

12⟩ Do you know what are function prototypes?

In C++ all functions must be declared before they are used. This is accomplished using function prototype. Prototypes enable complier to provide stronger type checking. When prototype is used, the compiler can find and report any illegal type conversions between the type of arguments used to call a function and the type definition of its parameters. It can also find the difference between the no of arguments used to call a function and the number of parameters in the function. Thus function prototypes help us trap bugs before they occur. In addition, they help verify that your program is working correctly by not allowing functions to be called with mismatched arguments.

A general function prototype looks like following:

return_type func_name(type param_name1, type param_name2, …,type param_nameN);

The type indicates data type. parameter names are optional in prototype.

Following program illustrates the value of function parameters:

void sqr_it(int *i); //prototype of function sqr_it

int main()

{

int num;

num = 10;

sqr_it(num); //type mismatch

return 0;

}

void sqr_it(int *i)

{

*i = *i * *i;

}

Since sqr_it() has pointer to integer as its parameter, the program throws an error when we pass an integer to it.

 221 views

13⟩ Can you explain static function?

Static member functions are used to maintain a single copy of a class member function across various objects of the class. Static member functions can be called either by itself, independent of any object, by using class name and :: (scope resolution operator) or in connection with an object.

Restrictions on static member functions are:

1. They can directly refer to other static members of the class.

2. Static member functions do not have this pointer.

3. Static member function can not be virtual.

Though there are several restrictions on static member functions, one good use of them is to initialize private static data members of a class before any object is created.

Consider following example:

#include <iostream>

using namespace std;

class S

{

static int i;

public:

static void init(int x)

{

i = x;

}

void show()

{

cout <<i;

}

};

int S::i;

int main()

{

S::init(100); //initialize static variable i before creating object

S x;

x.show();

return 0;

}

 244 views

14⟩ Explain what are associate containers?

Containers are objects that hold other objects. An associative container stores pair of values. It is typically a key-value pair. Given one value (key), we can access the other, called the mapped value. The key needs to be unique. The value associated with that key could be unique or multiple depending upon the type of associative container.

The key-value pair could be of any data type (unlike integer in case of array).

There are various types of associative containers:

Map: It is a traditional associate array, where a single value is associated with each unique pair.

Multimap : This type of associative array allows duplicate elements (value) for a given key.

 220 views