Q4. Write about the pointer.
Answer:
A constant pointer contained in each object of a class points to the object for which the member function is currently executing is called as 'this' pointer. The general form or the syntax of this pointer is,
this →class members
When a member function of an object is called, the compiler assigns the address of an object to this pointer and then calls the function. The 'this' pointer is implicitly used to refer both the data members and the member function of an object. Only the non-static member functions can use the keyword.
Q5. What are static members of a class?
Answer:
Static Members of a Class
The static members of a class are static member function and static member variable.
Static Member Function
When a member function is preceded by static keyword then it is called as static member function. The static member the functions have the ability to access static member variables and the member functions of the same class. These static member functions are invoked by the class name without using the class object. The scope of static member is valid in the entire class, but, it does not create any side effects to other part of the program.
Q6. Define constructor and destructor.
Answer:
Constructor
A constructor is a member function which automatically gets called whenever the object of the class is instantiated. It has same name as that of its class and does not return any value.
Destructor
A destructor is a special member function that is used to destroy the objects created by constructor. It takes the same name as the class but with a ‘tilde’ (~) at the beginning. It doesn’t have any return type. It is called automatically at the end of program execution to free up the acquired storage.
Memory allocation is done by using new operator in a constructor whereas delete operator is used in a destructor to deallocate the previously allocated memory.