C++ Pointers & Functions Interview Preparation Guide
Strengthen your C++ Pointers & Functions interview skills with our collection of 32 important questions. These questions are specifically selected to challenge and enhance your knowledge in C++ Pointers & Functions. Perfect for all proficiency levels, they are key to your interview success. Dont miss out on our free PDF download, containing all 32 questions to help you succeed in your C++ Pointers & Functions interview. Its an invaluable tool for reinforcing your knowledge and building confidence.32 C++ Pointers & Functions Questions and Answers:
1 :: What is const pointer?
const pointer is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points. It is declared as:
type * const name
type is data type
name is name of the pointer
eg: char * const p
Since the location to which a const pointer points to can not be changed, the following code:
char ch1 = ‘A’;
char ch2 = ‘B’;
char * const p = &ch1;
p = &ch2;
will throw an error since address stored in p can not be changed.
type * const name
type is data type
name is name of the pointer
eg: char * const p
Since the location to which a const pointer points to can not be changed, the following code:
char ch1 = ‘A’;
char ch2 = ‘B’;
char * const p = &ch1;
p = &ch2;
will throw an error since address stored in p can not be changed.
2 :: What is const reference?
const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it’s a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object. For example:
int &ri = i;
binds ri to refer to i. Then assignment such as:
ri = j;
doesn’t bind ri to j. It assigns the value in j to the object referenced by ri, ie i;
This means, if we pass arguments to a function by const references; the function can not change the value stored in those references. This allows us to use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. The compiler will throw an error if the function tries to modify the value of a const reference.
int &ri = i;
binds ri to refer to i. Then assignment such as:
ri = j;
doesn’t bind ri to j. It assigns the value in j to the object referenced by ri, ie i;
This means, if we pass arguments to a function by const references; the function can not change the value stored in those references. This allows us to use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. The compiler will throw an error if the function tries to modify the value of a const reference.
3 :: Explain what is NULL pointer and void pointer and what is their use?
A NULL pointer has a fixed reserved value that is not zero or space, which indicates that no object is referred. NULL pointers are used in C and C++ as compile-time constant. NULL pointer represents certain conditions, like successor to the last element in linked list, while a consistent structure of the list of nodes are maintained.
A void pointer is a special pointer that points to an unspecified object. A null pointer can not be dereferenced. The address manipulation can directly be done by pointer casting to and from an integral type of sufficient size.
A void pointer is a special pointer that points to an unspecified object. A null pointer can not be dereferenced. The address manipulation can directly be done by pointer casting to and from an integral type of sufficient size.
4 :: What is void pointer using C++?
In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type. The void pointers can point to any data type.
We can declare void pointer as follows.
Void *p;.
We can declare void pointer as follows.
Void *p;.
5 :: Tell me what happens when a pointer is deleted twice?
A pointer if not nullified and deleted twice, leads to a trap. If set to null, it wont have much affect if deleted twice.
6 :: Can you please explain the use of this pointer?
When a member function is called, it is automatically passed an implicit argument that is a pointer to the invoking object (ie the object on which the function is invoked). This pointer is known as this pointer. It is internally created at the time of function call.
The this pointer is very important when operators are overloaded.
Example: Consider a class with int and float data members and overloaded Pre-increment operator ++:
class MyClass
{
int i;
float f;
public:
MyClass ()
{
i = 0;
f = 0.0;
}
MyClass (int x, float y)
{
i = x; f = y;
}
MyClass operator ++()
{
i = i + 1;
f = f + 1.0;
return *this; //this pointer which points to the caller object
}
MyClass show()
{
cout<<”The elements are:\n” cout<i<<”\n<f; //accessing
data members using this
}
};
int main()
{
MyClass a;
++a; //The overloaded operator function ++()will return a’s this
pointer
a.show();
retun 0;
}
The o/p would be:
The elements are:
1
1.0
The this pointer is very important when operators are overloaded.
Example: Consider a class with int and float data members and overloaded Pre-increment operator ++:
class MyClass
{
int i;
float f;
public:
MyClass ()
{
i = 0;
f = 0.0;
}
MyClass (int x, float y)
{
i = x; f = y;
}
MyClass operator ++()
{
i = i + 1;
f = f + 1.0;
return *this; //this pointer which points to the caller object
}
MyClass show()
{
cout<<”The elements are:\n” cout<i<<”\n<f; //accessing
data members using this
}
};
int main()
{
MyClass a;
++a; //The overloaded operator function ++()will return a’s this
pointer
a.show();
retun 0;
}
The o/p would be:
The elements are:
1
1.0
7 :: When we use this pointer?
'this pointer' is used as a pointer to the class object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions.
8 :: Can you please explain the difference between an inspector and a mutator?
An object’s state is returned without modifying the object’s abstract state by using a function called inspector. Invoking an inspector does not cause any noticeable change in the object’s behavior of any of the functions of that object.
A mutator, on the other hand, changes the state of an object which is noticeable by outsiders. It means, it changes the abstract state of the object.
The following code snippet depicts the usage of these two functions:
class ShoppingCart {
public:
int addItem(); //Mutator
int numItems() const; //Inspector
};
The function addItems() is a mutator. The reason is that it changes the ‘ShoppingCart’ by adding an item.
The function numItems() is an inspector. The reason is that it just updates the count of number of items in the ‘ShoppingCart’. The const declaration followed by int numItems() specifies that numItems() never change the ‘ShoppingCart’ object..
A mutator, on the other hand, changes the state of an object which is noticeable by outsiders. It means, it changes the abstract state of the object.
The following code snippet depicts the usage of these two functions:
class ShoppingCart {
public:
int addItem(); //Mutator
int numItems() const; //Inspector
};
The function addItems() is a mutator. The reason is that it changes the ‘ShoppingCart’ by adding an item.
The function numItems() is an inspector. The reason is that it just updates the count of number of items in the ‘ShoppingCart’. The const declaration followed by int numItems() specifies that numItems() never change the ‘ShoppingCart’ object..
9 :: What is pointer to member?
not to a specific instance of that member in an object. This type of pointer is called a pointer to a class member or a pointer-to-member. It is not same as normal C++ pointer. Instead it provides only an offset into an object of the member’s class at which that member can be found. Since member pointers are not true pointers, the . and -> can not be applied to them. Instead we must use the special operators .* and ->* . They allow access to a member of a class.
Example:
#include <iostream>
using namespace std;
class MyClass
{
public:
int val;
MyClass(int i)
{
val = i;
}
int double_val()
{
return val + val;
}
};
int main()
{
int MyClass::*data; //data member pointer
int(MyClass::*func)(); //function member pointer
MyClass obj1(1), obj2(2); //create objects
data =&MyClass::val; //get offset of data val
func=&MyClass::double_val; //get offset of function double_val()
cout << “The values are:”;
cout << ob1.*data << “ “ <<ob2.*data << “\n”;
cout<< “Here they are doubled:”;
cout << (ob1.*func)() << “ “ <<(ob2.*func)()<< “\n”;
return 0;
}
Here data and func are member pointers. As shown in the program, when declaring pointers to members, you must specify the class and use the scope resolution operator.
Example:
#include <iostream>
using namespace std;
class MyClass
{
public:
int val;
MyClass(int i)
{
val = i;
}
int double_val()
{
return val + val;
}
};
int main()
{
int MyClass::*data; //data member pointer
int(MyClass::*func)(); //function member pointer
MyClass obj1(1), obj2(2); //create objects
data =&MyClass::val; //get offset of data val
func=&MyClass::double_val; //get offset of function double_val()
cout << “The values are:”;
cout << ob1.*data << “ “ <<ob2.*data << “\n”;
cout<< “Here they are doubled:”;
cout << (ob1.*func)() << “ “ <<(ob2.*func)()<< “\n”;
return 0;
}
Here data and func are member pointers. As shown in the program, when declaring pointers to members, you must specify the class and use the scope resolution operator.
10 :: What is smart pointer?
Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They are like built-in C++ pointers. However, they automatically delete the object pointed to at the appropriate time. They are useful as they ensure proper destruction of dynamically allocated objects (Exceptions). They can also be used to keep track of dynamically allocated objects shared by multiple owners.
They appear as owning the object pointed to and are responsible for deletion of the object when it is no longer needed.
The smart pointer library provides five smart pointer class templates:
scoped_ptr : Simple sole ownership of single objects. Noncopyable.
scoped_array: Simple sole ownership of arrays. Noncopyable.
shared_ptr: Object ownership shared among multiple pointers
shared_array: Array ownership shared among multiple pointers.
weak_ptr: Non-owning observers of an object owned by shared_ptr.
intrusive_ptr: Shared ownership of objects with an embedded reference count.
These templates are designed to complement the std::auto_ptr template.
They appear as owning the object pointed to and are responsible for deletion of the object when it is no longer needed.
The smart pointer library provides five smart pointer class templates:
scoped_ptr : Simple sole ownership of single objects. Noncopyable.
scoped_array: Simple sole ownership of arrays. Noncopyable.
shared_ptr: Object ownership shared among multiple pointers
shared_array: Array ownership shared among multiple pointers.
weak_ptr: Non-owning observers of an object owned by shared_ptr.
intrusive_ptr: Shared ownership of objects with an embedded reference count.
These templates are designed to complement the std::auto_ptr template.