1: //pointers2.cpp 3: #include <iostream> 4: using namespace std; 6: int main() 7: { 8: cout << "\nThis program illustrates initialization of pointer " 9: "variables with dynamic\nmemory allocation via the new " 10: "operator, followed by the release of this\nstorage when " 11: "it is no longer needed."; 12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 14: struct ClubMember 15: { 16: string name; 17: int age; 18: double balance; 19: }; 21: //Declaring and initializing pointer variables with dynamic memory 22: int* p_int1 = new int; 23: char* p_char1 = new char; 24: ClubMember* p_cm1 = new ClubMember; 26: cout << "\nHere are the values of some pointer variables " 27: "initialized at declaration time:"; 28: cout << "\nValue of p_int1: " << p_int1; 29: cout << "\nValue of p_char1: " << p_char1; 30: cout << "\nValue of p_cm1: " << p_cm1; 31: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 34: //Declaring pointer variables, followed by dynamic memory assignment 35: int* p_int2; 36: p_int2 = new int; 37: char* p_char2; 38: p_char2 = new char; 39: ClubMember* p_cm2; 40: p_cm2 = new ClubMember; 42: cout << "\nHere are the values of some pointer variables " 43: "assigned after declaration:"; 44: cout << "\nValue of p_int2: " << p_int2; 45: cout << "\nValue of p_char2: " << p_char2; 46: cout << "\nValue of p_cm2: " << p_cm2; 47: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 50: delete p_int1; delete p_char1; delete p_cm1; 51: delete p_int2; delete p_char2; delete p_cm2; 53: cout << "\nHere are the values of the same pointer variables after the" 54: "\ndynamic memory they pointed to has been restored to the heap:"; 55: cout << "\nValue of p_int1: " << p_int1; 56: cout << "\nValue of p_char1: " << p_char1; 57: cout << "\nValue of p_cm1: " << p_cm1; 58: cout << "\nValue of p_int2: " << p_int2; 59: cout << "\nValue of p_char2: " << p_char2; 60: cout << "\nValue of p_cm2: " << p_cm2; 61: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 62: }