dunamic constructor with example

Example: dynamic constructor in c++

//dynamic constructor and application
//code by Soumyadeep Ghosh
//insta : @soumyadepp
//linked in: https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/

#include <bits/stdc++.h>

using namespace std;

class Array
{
  int *a;
  int size;
  public:
    Array(int n)
    {
      a=new int[n]; //dynamic constructor allocating memory for n integers
      size=n;
    }
  void getarr()
  {
    for(int i=0;i<size;i++)
    {
      cin>>a[i];
    }
  }
  void putarr()
  {
    for(int i = 0; i < size ; i++ )
    {
      cout<<a[i]<<" "<<;
    }
    cout<<endl;
  }
};


int main()
{
  Array a(10);   //array of 10 integers
  a.getarr();  //to accept 10 integers
  a.putarr();  //to display 10 integers
  return 0;
}

//thank you!

Tags:

Cpp Example