Mega Code Archive

 
Categories / C++ / Language
 

Use new to allocate memory for a class and check error

#include <iostream> #include <cstring> using namespace std; class phone {   char name[40];   char number[14]; public:   void store(char *n, char *num);   void show(); }; void phone::store(char *n, char *num) {   strcpy(name, n);   strcpy(number, num); } void phone::show() {   cout << name << ": " << number;   cout << endl; } int main() {   phone *p;   p = new phone;   if(!p) {     cout << "Allocation error.";     return 1;   }   p->store("Joe", "999 555-4444");   p->show();   delete p;   return 0; }