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)

paper of size A0 has dimensions 1189 mmx 841 mm .Each subsequent size A(n) is defined as A(n-1) cut in half parallel to its shorter sides .Thus paper of size A1 would have dimensions 841 mm X 594 mm .Write a program to calculate the print paper sizes A0,A1,A2,.........A8.

constructor with default arguments