#include<iostream> // Include library
usingnamespace std; // Use standard name space
class A{ // Class object named a
public: // Public (could have just made this a struct)
char* s; // Contains a C-style string, named S
A(char* P):s(P) { } // Constructor, assigned whatever passed in to "s"
}; // End of class
int main(){ // Start of main
A* a1=new A("object1"); // Create class called a1 on heap. s set to "object1"
A* a2=new A("object2"); // Create class called a2 on heap. s set to "object2"
delete a1; // Delete memory allocated for a1
cout<<a2->s; // Print out contents of "s" from a2 ("object2")
cout<<endl; // Print new line, flush buffers
system("pause"); // System pause, assuming halt of program so it doesn't close
return 0; // Return 0, common for success
}
There's a glaring memory leak. Anything allocated with new should be deallocated with delete. In this case, it's the a2 object.