Q1: What value will be printed ?
double d1 = 16, d2=2.0;
double d3 = (1/2) * ( sqrt(d1) + d2/2);
cout << d3 << endl;

Q2: Please write your own itoa implementation

Q3: What is pure virtual destructor? Shall ( or can ) pure virtual functions be implemented ?

Q4: Give an example of abort function implementation

Q5: Why we need semicolon after closing bracket in C++ class declaration ?

Q6: Can we call destructor directly? For what we may need it ?

Q7: Can we call constructor directly? For what we may need it ?

Q8: Can we thron an exception in destructor ?

Q9: What happen if we call pure virtual methods in constructor or destructor ? Same question about ‘non-pure’ virtual methods ?

Q10: Why don’t we have virtual constructors in C++ ?

Q11: What is difference between new and new [] ? Same thing about delete and delete [] ?

Q12: What is a difference between static_cast, dynamic_cast, reinterpret_cast and const_cast ?

Q13: Why do we need explicit keyword in C++ ?

Q14: Explain difference between

const MyClass c;
const MyClass &c2 = c;
const MyClass *c2 = &c;
const MyClass * const c2 = &c;

Q15: Difference between const_iterator and iterator

Q16: Implement Rand5 ( which returns randomly 0..4 ) use only Random7 ( which returns 0..6)

Q17: Can we use object of class ( or structure ) which doesn’t have name? Can we declare and use classes and structures without names ?

Q18: What is it factory pattern ?

( to be updated and continued very soon 🙂

Answers ( if you need it 🙂 ) below

Q1. Value which will be printed on a screen is “0”, because 1/2 is all about integer division and it will be 0, so 0 * ( 4 + 1 ) will be 0. To have a more expected value this expression should be caste to a double :

double d3 = double(1)/2 * ( sqrt(d1) + d2/2);

Q2. Take a look on fastest variants of itoa : Programming Contest

Q3: What is pure virtual destructor? Shall ( or can ) pure virtual functions be implemented ?
Good explanation : Why do we need a pure virtual destructors in C++.Yes, pure virtual functions ( dtor included ) can be implemented – you even can call them.But you can’t create object of class who have pure virtual destructor ( or method ) – you can call them via childs objects.
Here’s an simple example of declaration and calling pure virtual methods in c++

Q4> :Give an example of abort function implementation
like this one ( nice thing isn’t it? ) :

void abort() { // scribble through null ptr
memset( 0, 1, 1 );
}

Q5: Why we need semicolon after closing bracket in C++ class declaration ?
Because ( in compatability with C ) right after class declaration there’s can be object :

class A {
...
} object_a;

Take a look on this link :In C++ classes, why the semi-colon after the closing brace

Q6: Can we call destructor directly? For what we may need it ?
Yes, we surely can. It can be required when you allocate memory for object not in heap, but in stack via using new

unsigned char buffer [256];
Dolphin * d = new(buffer) Dolphin;

Here’s an Example of explicitly destructor calling. See also :What is “placement new” and why would I use it?

Q7: Can we call constructor directly? For what we may need it ?
It is not correct to say that you can call the constructor directly. The standard explicitly has (12.1/1): “Constructors do not have names.” You can only call the constructor via other constructs such as a function style cast, or placement new – see Explicitly calling constructos in c++.

Q8: Can we thron an exception in destructor ?
We can, but it’s bad idea. Take a look on this How can I handle a destructor that fails?

Q9: What happen if we call pure virtual methods in constructor or destructor ? Same question about ‘non-pure’ virtual methods ?
If you implement pure virtual methods you can call them. If not – you’ll have linkage error. Anyway it’s almost bad idea – when you call virtual methods in constructors/destructors you will you base class virtual methods tables – it’s not normal behavious from my point. Take look on source : Calling virtual methods from constructors and destructors.

Q10: Why don’t we have virtual constructors in C++ ?
Why don’t we have virtual constructors?

Q11: What is difference between new and new [] ? Same thing about delete and delete [] ?
In short new [] used for arrays, and delete too – so if you allocate some A * a = new [3] A – there’s will be space alloced to 3 A and three times A::A() will be called, if you delete [] a – A::~A() will be called 3 times. If you use delete a – only one time A::~A() will be called ( some compilers warn about it ). Here’s c++ example – difference between new and new [] – delete and delete []

Q12: What is a difference between static_cast, dynamic_cast, reinterpret_cast and const_cast ?

static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating static_cast isn’t necessary, but it’s important to note that the T(something) syntax is equivalent to (T)something and should be avoided (more on that later). A T(something, something_else) is safe, however, and guaranteed to call the constructor.static_cast can also cast through inheritance hierarchies. It is unecessary when casting upwards (towards a base class), but when casting downwards it can be used as long as it doesn’t cast through virtual inheritance. It does not do checking, however, and it is undefined behavior to static_cast down a hierarchy to a type that isn’t actually the type of the object.

const_cast can be used to remove or add const to a variable; no other C++ cast is capable of removing it (not even reinterpret_cast). It is important to note that using it is only undefined if the orginial variable is const; if you use it to take the const of a reference to something that wasn’t declared with const, it is safe. This can be useful when overloading member functions based on const, for instance. It can also be used to add const to an object, such as to call a member function overload. const_cast also works similarly on volatile, though that’s less common .

dynamic_cast is almost exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You don’t have to use it to cast downwards, you can cast sideways or even up another chain. The dynamic_cast will seek out the desired object and return it if possible. If it can’t, it will return NULL in the case of a pointer, or throw std::bad_cast in the case of a reference.dynamic_cast has some limitations, though. It doesn’t work if there are multiple objects of the same type in the inheritance hierarchy (the so-called ‘dreaded diamond’) and you aren’t using virtual inheritance. It also can only go through public inheritance – it will always fail to travel through protected or private inheritance. This is rarely an issue, however, as such forms of inheritance are rare.

reinterpret_cast is the most dangerous cast, and should be used very sparingly. It turns one type directly into another – such as casting the value from one pointer to another, or storing a pointer in an int, or all sorts of other nasty things. Largely, the only guarantee you get with reinterpret_cast is that if you cast the result back to the original type, you will get the same value. Other than that, you’re on your own. reinterpret_cast cannot do all sorts of conversions; in fact it is relatively limited. It should almost never be used (even interfacing with C code using void* can be done with static_cast)

See When should static_cast, dynamic_cast and reinterpret_cast be used?

Q13: Why do we need explicit keyword in C++ ?
In this case we will call A::A(int) two times and call A() in = as ‘converting constructor’, for some situations it’s not okay.

#include
class A {
public:
A(int i) {  std::cout<<"A::A("<<<")\n"; }
};


int main(void) {
A a(1);
a = 2;
return 1;
};

It will type :

A::A(1)
A::A(2)

To prevent use ‘converting constructor’ use ‘explicit’ keyword at constructor declaration – same code will looks like below ( we can’t use a=1 because of compilation error )
#include
class A {
public:
explicit A(int i) {std::cout<<"A::A("<<<")\n"; }

int main(void) {
A a(1);
a = A(2);
return 1;
};

Q14: Explain difference between

const MyClass c;
const MyClass &c2 = c;
const MyClass *c2 = &c;
const MyClass * const c2 = &c;

Q15: Difference between const_iterator and iterator

const_iterators don’t allow you to change the values that they point to, regular iterators do.
As with all things in C++, always prefer const, unless there’s a good reason to use regular iterators (i.e. you want to use the fact that they’re not const to change the pointed-to value).

Q16: Implement Rand5 ( which returns randomly 0..4 ) use only Random7 ( which returns 0..6)

Example of recursive :

int Rand5(void) {
int val=Rand7();
if (val <6)
return val;
return Rand5();
}

Q17: Can we use object of class ( or structure ) which doesn’t have name? Can we declare and use classes and structures without names ?

Yes, we can ( for classes which doesn’t have constructor-desctructors ) . This code will be compiled and works fine :

#include <iostream>
class  {
public:
int i;
} test_obj;

struct {
int j;
} test_rec;

int main(void) {
test_rec.j = 0;
test_obj.i = 1;
return 1;
};

Q18: What is it factory pattern ?

And a some humor about :
Hello world c++

Leave a Reply