Set - 7

Question 6 :

STL Containers - What are the types of STL containers?

Answer :

There are 3 types of STL containers: 
1. Adaptive containers like queue, stack 
2. Associative containers like set, map 
3. Sequence containers like vector, deque


Question 7 :

What is "mutable"?

Answer :

Answer1. 
"mutable" is a C++ keyword. When we declare const, none of its data members can change. When we want one of its members to change, we declare it as mutable. 

Answer2. 
A "mutable" keyword is useful when we want to force a "logical const" data member to have its value modified. A logical const can happen when we declare a data member as non-const, but we have a const member function attempting to modify that data member. For example: 

class Dummy {
    public:
        bool isValid() const;
    private:
        mutable int size_ = 0;
        mutable bool validStatus_ = FALSE; 
        // logical const issue resolved
};

bool Dummy::isValid() const 
// data members become bitwise const
{
    if (size > 10) {
    validStatus_ = TRUE; // fine to assign
    size = 0; // fine to assign
    }
}

Answer2. 
"mutable" keyword in C++ is used to specify that the member may be updated or modified even if it is member of constant object. Example: 

class Animal {
    private:
        string name;
        string food;
        mutable int age;
    public:
    void set_age(int a);
};

void main() {
    const Animal Tiger(’Fulffy’,'antelope’,1);
    Tiger.set_age(2); 
    // the age can be changed since its mutable
}

 


Question 8 :

What is a modifier? 

Answer :

A modifier, also called a modifying function is a member function that changes the value of  at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as 'mutators'. Example: The function mod is a modifier in the following code snippet:

class test{
	int x,y;
	public:
	test(){
		x=0; y=0;
	}
	void mod(){
		x=10;
		y=15;
	}
};

 


Question 9 :

What is a dangling pointer?

Answer :

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed. The following code snippet shows this:

class Sample{
	public:
	int *ptr;
	Sample(int i){
		ptr = new int(i);
	}

	~Sample(){
		delete ptr;
	}
	void PrintVal(){
	cout << "The value is " << *ptr;
	}
};

void SomeFunc(Sample x){
	cout << "Say i am in someFunc " << endl;
}

int main(){
	Sample s1 = 10;
	SomeFunc(s1);
	s1.PrintVal();
}

In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.


Question 10 :

What are proxy objects? 

Answer :

Objects that stand for other objects are called proxy objects or surrogates. 
template <class t="">

class Array2D{
	public:
		class Array1D{
			public:
				T& operator[] (int index);
				const T& operator[] (int index)const;
		};

    Array1D operator[] (int index);
    const Array1D operator[] (int index) const;
};

The following then becomes legal:

Array2D<float>data(10,20);
cout<<data[3][6]; // fine

Here data[3] yields an Array1D object and the operator [] invocation on that object yields the float in position(3,6) of the original two dimensional array. Clients of the Array2D class need not be aware of the presence of the Array1D class. Objects of this latter class stand for one-dimensional array objects that, conceptually, do not exist for clients of Array2D. Such clients program as if they were using real, live, two-dimensional arrays. Each Array1D object stands for a one-dimensional array that is absent from a conceptual model used by the clients of Array2D. In the above example, Array1D is a proxy class. Its instances stand for one-dimensional arrays that, conceptually, do not exist.