Explanation: In this we point data member of type char which is allocated memory dynamically by new operator and when we create dynamic memory within the constructor of class this is known as dynamic constructor.
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Explanation: In this program we have created array of object dynamically. The first object is ptr, second is ptr and so on . For each object creation default constructor is called and for each object memory is allocated to pointer type variable by new operator.
Explanation: In this integer type pointer variable is declared in class which is assigned memory dynamically when the constructor is called. When we create object obj1, the default constructor is called and memory is assigned dynamically to pointer type variable and initialized with value 0. And similarly when obj2 is created parameterized constructor is called and memory is assigned dynamically.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Constructor Overloading in C++
- Constructor Delegation in C++
- Copy Constructor in C++
- Can a constructor be private in C++ ?
- What is conversion constructor in C++?
- Advanced C++ | Virtual Constructor
- When should we write our own copy constructor?
- When is copy constructor called?
- Difference Between Constructor and Destructor in C++
- Passing a vector to constructor in C++
- How to create a List with Constructor in C++ STL
- Order of Constructor/ Destructor Call in C++
- Is body of a Default Constructor blank in C++?
- Copy constructor vs assignment operator in C++
- Why copy constructor argument should be const in C++?
- Is it possible to call constructor and destructor explicitly?
- C++ default constructor | Built-in types
- Advanced C++ | Virtual Copy Constructor
- Calling virtual methods in constructor/destructor in C++
- Does C++ compiler create default constructor when we write our own?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.