13 Lecture

CS304

Midterm & Final Term Short Notes

POINTER TO OBJECTS

In C++, a pointer to an object is a variable that stores the memory address of an object. By using pointers to objects, we can dynamically allocate memory for objects, pass objects to functions by reference, and manipulate objects indirectly. We


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a pointer to an object in C++? a) A variable that stores the value of an object b) A variable that stores the memory address of an object c) A variable that stores the size of an object d) A variable that stores the name of an object Answer: b) A variable that stores the memory address of an object. What is the syntax to declare a pointer to an object in C++? a) int ptr; b) obj pointer; c) obj *ptr; d) obj -> pointer; Answer: c) obj *ptr; How is the value of an object pointed to by a pointer accessed in C++? a) Using the * operator b) Using the & operator c) Using the -> operator d) Using the . operator Answer: a) Using the * operator. What is the purpose of using pointers to objects in C++? a) To dynamically allocate memory for objects b) To pass objects to functions by reference c) To manipulate objects indirectly d) All of the above Answer: d) All of the above. Can a pointer to an object be null in C++? a) Yes b) No Answer: a) Yes. What is the difference between a pointer to an object and a reference to an object in C++? a) A pointer can be null, while a reference cannot. b) A pointer can be reassigned to point to a different object, while a reference cannot. c) A pointer requires the * operator to access the object's value, while a reference does not. d) All of the above. Answer: d) All of the above. How is memory allocated for an object pointed to by a pointer in C++? a) Using the new operator b) Using the delete operator c) Using the malloc function d) Using the free function Answer: a) Using the new operator. What is the purpose of the -> operator in C++? a) To access a member of a class or structure pointed to by a pointer b) To declare a pointer to an object c) To declare a reference to an object d) None of the above Answer: a) To access a member of a class or structure pointed to by a pointer. How can a pointer to an object be passed to a function in C++? a) By value b) By reference c) By const reference d) All of the above Answer: b) By reference. What is a dangling pointer in C++? a) A pointer that points to a null object b) A pointer that points to an object that has been deleted or deallocated c) A pointer that points to a new object d) A pointer that points to an object that has not been initialized Answer: b) A pointer that points to an object that has been deleted or deallocated.


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a pointer to an object in C++? Answer: A pointer to an object is a variable that stores the memory address of an object in C++. How is a pointer to an object declared in C++? Answer: A pointer to an object is declared using the * operator, followed by the object type and the pointer variable name. How is the value of an object pointed to by a pointer accessed in C++? Answer: The value of an object pointed to by a pointer is accessed using the * operator. How is memory allocated for an object pointed to by a pointer in C++? Answer: Memory is allocated for an object pointed to by a pointer using the new operator. What is the purpose of using pointers to objects in C++? Answer: Pointers to objects are used in C++ to dynamically allocate memory for objects, pass objects to functions by reference, and manipulate objects indirectly. What is the difference between a pointer to an object and a reference to an object in C++? Answer: A pointer to an object can be null and can be reassigned to point to a different object, while a reference to an object cannot be null and cannot be reassigned. How can a pointer to an object be passed to a function in C++? Answer: A pointer to an object can be passed to a function in C++ by reference. How is a member of a class or structure pointed to by a pointer accessed in C++? Answer: A member of a class or structure pointed to by a pointer is accessed using the -> operator. What is a dangling pointer in C++? Answer: A dangling pointer in C++ is a pointer that points to an object that has been deleted or deallocated. How can a memory leak occur in C++ when using pointers to objects? Answer: A memory leak can occur in C++ when using pointers to objects if memory is dynamically allocated using the new operator and is not deallocated using the delete operator.

In C++, a pointer to an object is a variable that stores the memory address of an object. The pointer allows indirect access to the object and can be used to manipulate the object's data and behaviors. To declare a pointer to an object, the * operator is used, followed by the object type and the pointer variable name. For example, a pointer to an integer object can be declared as "int *ptr". To access the value of an object pointed to by a pointer, the * operator is used. For example, to access the value of the integer object pointed to by "ptr", the expression "*ptr" is used. Memory for an object pointed to by a pointer is allocated using the new operator, which dynamically allocates memory for an object and returns a pointer to the allocated memory. To deallocate memory for an object pointed to by a pointer, the delete operator is used. Pointers to objects are commonly used to dynamically allocate memory for objects and to pass objects to functions by reference. By passing objects by reference, the function can directly manipulate the object's data and behaviors. When passing a pointer to an object to a function, the function declaration includes a reference parameter. For example, a function that takes a pointer to an integer object as a parameter can be declared as "void myFunction(int *ptr)". To access a member of a class or structure pointed to by a pointer, the -> operator is used. For example, to access the member variable "x" of an object pointed to by "ptr", the expression "ptr->x" is used. It's important to note that a memory leak can occur when using pointers to objects if memory is not properly deallocated using the delete operator. Additionally, dangling pointers can occur when a pointer points to an object that has been deleted or deallocated. To avoid these issues, it's important to use pointers to objects responsibly and to properly manage memory allocation and deallocation.