1: // Filename: PTR_EX2.CPP 2: // Purpose: Illustrates pointers to double, char, bool, enum 3: // and struct types. 5: #include <iostream> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates pointers to various types." 11: << "\nStudy the source code and the output simultaneously.\n\n"; 13: double* dPtr; // Pointer to double 14: char* cPtr; // Pointer to char 15: bool* bPtr; // Pointer to bool 17: enum DayType { SUN, MON, TUE, WED, THU, FRI, SAT }; 18: DayType* dayPtr; // Pointer to enum 20: struct BankAccount 21: { 22: int idNum; 23: char kind; 24: double balance; 25: }; 26: BankAccount* baPtr; // Pointer to struct 28: double cost = 19.95; // These are just ordinary variables, 29: char grade = 'B'; // each one of a different type, and 30: bool valid = true; // each one initialized at the time 31: DayType day = WED; // of declaration. 32: BankAccount myStash = {123, 'C', 199.99}; 34: dPtr = &cost; // Here we make the pointers point 35: cPtr = &grade; // at the "ordinary" variables. 36: bPtr = &valid; 37: dayPtr = &day; 38: baPtr = &myStash; 40: // Now display the values: 41: cout << *dPtr << endl; 42: cout << *cPtr << endl; 43: cout << ((*bPtr == true) ? "true" : "false") << endl << endl; 44: if (*dayPtr == WED) 45: { 46: // Because this notation ... C++ supplies this 47: // is so cumbersome ... alternate notation 48: // vvvvvvvvvvv vvvvvvvvvvvv 49: cout << (*baPtr).idNum << " " << baPtr->idNum << endl; 50: cout << (*baPtr).kind << " " << baPtr->kind << endl; 51: cout << (*baPtr).balance << " " << baPtr->balance << endl; 52: } 53: cout << endl; 55: return 0; 56: }