Instantiation of Objects
Introduction
Instantiation creates an object from a class using default or parameterized constructors.
Definition
Instantiation: creating an object instance from a class.
C++ Code Example
class Point { int x,y; public: Point(int a,int b):x(a),y(b){} };
int main(){
Point p1(1,2); // instantiation
Point p2 = p1;
}Important Questions
Short Questions
- What is instantiation?
- Ways to instantiate?
- Stack vs heap instantiation?
- What is object creation?
Long Questions
- Explain instantiation with example.
- Discuss stack vs heap objects.
MCQ Practice
1. Instantiation means:
- Creating object
- Deleting object
- Compiling class
- Linking
Answer: Creating object.
2. Stack object is created by:
- ClassName obj;
- new ClassName
- malloc
- None
Answer: ClassName obj;.
3. Heap object is created by:
- new
- ClassName obj;
- malloc only
- None
Answer: new.
4. Copy constructor is used in:
- Object copying
- Deletion
- Compilation
- None
Answer: Object copying.
5. Instantiation calls:
- Constructor
- Destructor
- main
- None
Answer: Constructor.
Quick Revision
- Instantiate via constructor.
- Stack/heap both possible.