1) What is the difference between reference and pointer?
Reference | Pointer |
Reference is used to refer to an existing variable in another name. | Pointers are used to store the address of a variable. |
References cannot have a null value assigned | The pointer can have a null value assigned |
A reference variable can be referenced by passing by the value | The pointer can be referenced but passed by reference |
A reference must be initialized on the declaration | Pointers needs no initialization on the declaration |
A reference shares the same memory address with the original variable and takes up some space on the stack | Pointer has its own memory address and size on the stack |
- What is STL in C++ with an example?
STL in C++ is a library and abbreviation of Standard Template Library. STL is a generalized library that provides common programming data structures/ container classes, functions, algorithms, and iterators. STL has four components
- Algorithms: Searching and sorting algorithms such as binary search,
merge sort etc.
- Containers: Vector, list, queue, arrays, map etc.
- Functions: They are objects that act like functions.
- Iterators: It is an object that allows transversing through elements
of a container, e.g., vector<int>::iterator.
3) What is an inline function in C++?
Inline functions are used to increase the execution time of a program. If a function is inline, the compiler puts the function code wherever the function is used during compile time. The syntax for the same is as follows:
4) What is friend function in C++?
A friend function has access rights to all private and protected members of the class.
class Circle
{
double radius;
public:
friend void printradius( Circle c );
};
void printradius(Circle c )
{
/* Because printradius() is a friend of Circle, it can
directly access any member of this class */
cout << "Radius of circle: " << c.radius;
}
int main()
{
Circle c;
// Use friend function to print the radius.
printradius( c);
return 0;
}
ย