1.

What is the relationship between object and pointers? Give an example.

Answer»

The pointers pointing to objects are referred to as object pointers. There is a relationship between pointers and objects. Using pointers all the members of the class can be accessed. The following example shows various operations that can be performed on the members of the class through a pointer to objects.

Declaration : class name *object_pointer, object;
Initialisation object_pointer = & object;
Invoking function call to member of the class: object_pointer->memberfunction();
For example;
class emp

{
private:

int empno;
char name[15];
float salary;

public:

void readdata();
void printdata();
};
void main()
{

emp obj, *ptr;
ptr = &obj;
ptr-> readdata();
ptr->printdata();
}
In the above program, *ptr is a pointer to class ’emp’ and ‘obj’ is class ’emp’ type. The pointer is initialised with the address of object ‘obj’ of class ’emp’ type. Then the pointer ‘ptr’ is used to invoke function call to the member function ‘readdata()’ and ‘printdata()’using member access operator ->.



Discussion

No Comment Found