User Tools

Site Tools


c_-_c_arrays:array_of_objects_using_new_operator

C - C++ Arrays - Array of objects using new operator

#include<iostream>
#include<string>
using namespace std;
 
class forgetcode
{
public:
  string name;
 
  forgetcode()
  {
    cout<<"\nConstructor";
  }
 
  void display()
  {
    cout<<name;
  }
 
  void getstring(string st)
  {
    name=st;
  }
};
 
 
int main()
{
  forgetcode *f;
 
  try
  {
    f=new forgetcode[3];
  }
  catch (bad_alloc xa) 
  { 
    cout << "Allocation Failure\n"; 
    return 1; 
  } 
 
  f[0].getstring("c");
  f[1].getstring("c++");
  f[2].getstring("java");
 
  for(int i=0;i<3;i++)
  {
    cout<<"\nString"<<i<<":";
    f[i].display();
  }
 
  return 0;
}
c_-_c_arrays/array_of_objects_using_new_operator.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki