fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class B{
  5. int x;
  6. int y;
  7. public:
  8. B(){
  9. x=y=0;
  10. cout<<"Constructor1"<<endl;
  11. }
  12. B(int i){
  13. x=i;
  14. y=0;
  15. cout<<"Constructor2"<<endl;
  16. }
  17. B(int i,int j){
  18. x=i;
  19. y=j;
  20. cout<<"Constructor3"<<endl;
  21. }
  22. ~B(){cout<<"Destructor"<<endl;}
  23. void print(){
  24. cout<<"x="<<x<<",y="<<y<<endl;
  25. }
  26. };
  27.  
  28. int main() {
  29. // your code goes here
  30. B * ptr;
  31. ptr=new B[3];
  32. ptr[0]=B();
  33. ptr[1]=B(5);
  34. ptr[2]=B(2,3);
  35. for(int i=0;i<3;i++){
  36. ptr[i].print();
  37. }
  38. delete[]ptr;
  39. }
Success #stdin #stdout 0s 4276KB
stdin
Standard input is empty
stdout
Constructor1
Constructor1
Constructor1
Constructor1
Destructor
Constructor2
Destructor
Constructor3
Destructor
x=0,y=0
x=5,y=0
x=2,y=3
Destructor
Destructor
Destructor