illustrate the use of new aand delete operations fordynamic allocation for class object the program uses bad_alloc exception instead of null pointer for handling allocation failure of C++

 illustrate the use of new and delete operations for dynamic allocation for class object the program uses bad_alloc  exception instead of null pointer for handling allocation failure

#include<iostream>

using namespace std;

class sample

{

private:

int data1;

char data2;

public:

void set (int i,char c)

{

data1=i;

data2=c;

}

void display(void)

{

cout<<"Data1="<<data1;

 cout<<"\nData2="<<data2;

}

};

int main(){

sample *s_ptr;

try

{

s_ptr =new sample;

}

catch(bad_alloc ba) 

{

cout<<"Bad Allocation occurred.... the program will be terminate now...";

return 1;

}

s_ptr-> set(25,'A');

s_ptr-> display();

delete s_ptr;

return 1;

}

output--





Comments

Popular posts from this blog

class with array in c++ (memory allocatin & using array in classes)

constructor with default arguments

constructor overloading in c++ programming