#include #include using namespace std; struct Complex { double real, imag; }; enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }; enum Rank { ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING }; struct Card { Rank rank; Suit suit; Card (Suit s, Rank r); void print() const; }; Card::Card (Suit s, Rank r) { suit = s; rank = r; } void Card::print () const { vector suits (4); suits[0] = "Clubs"; suits[1] = "Diamonds"; suits[2] = "Hearts"; suits[3] = "Spades"; vector ranks (14); ranks[1] = "Ace"; ranks[2] = "2"; ranks[3] = "3"; ranks[4] = "4"; ranks[5] = "5"; ranks[6] = "6"; ranks[7] = "7"; ranks[8] = "8"; ranks[9] = "9"; ranks[10] = "10"; ranks[11] = "Jack"; ranks[12] = "Queen"; ranks[13] = "King"; cout << ranks[rank] << " of " << suits[suit] << endl; } class employee; typedef vector employeeList; class employee { public: string name; string title; double salary; employee* supv; // pointer to supervisor employeeList subordinate; // constructors employee () { name = title = ""; supv = NULL; } employee (string N, string T, employee* S) { name = N; title = T; supv = S; } }; int x; int& rx = x; // this makes rx an alias for x int main () { x = 43; rx = 17; cout << rx << ", " << x << endl; int someNumber = 12345; int* ptrSomeNumber = &someNumber; cout << "someNumber = " << someNumber << endl; cout << "ptrSomeNumber = " << ptrSomeNumber << endl; cout << "ptrSomeNumber points to " << *ptrSomeNumber << endl; int* px = &x; while (px != NULL) { cout << "Pointer px points to something\n"; px = NULL; } cout << "Pointer px points to null, nothing, nada!\n"; Card* cardPtr = new Card (DIAMONDS, ACE); (*cardPtr).rank = Rank( (*cardPtr).rank + 1 ); // using parens and * operator cout << "rank = " << (*cardPtr).rank << endl; (*cardPtr).print(); cardPtr->rank = Rank( cardPtr->rank + 1 ); // using -> abbreviation cout << "rank = " << cardPtr->rank << endl; cardPtr->print(); // make a little employee database employee* pAlice = new employee ("Alice", "pres", NULL); // no supervisor (*pAlice).salary = 150000; pAlice->salary = 150000; // the C++ arrow abbreviation employee* pBob = new employee ("Bob", "VP mfg", pAlice); pBob->salary = 103040; employee* pCarol = new employee ("Carol", "VP sales", pAlice); pCarol->salary = 125000; employee* pJoe = new employee ("Joe", "welder", pBob); pJoe->salary = 50000; cout << "Joe the " << pJoe->title << " makes $" << pJoe->salary << " and his boss " << pJoe->supv->name // above is the same as (*(*pJoe).supv).name << " makes $" << pJoe->supv->salary << endl; // Joe gets a new job and a raise pJoe->supv = pCarol; // new boss pJoe->title = "salesman"; // new job pJoe->salary *= 1.1; // 10% raise cout << "Joe the " << pJoe->title << " makes $" << pJoe->salary << " and his boss " << pJoe->supv->name << " makes $" << pJoe->supv->salary << endl; // initialize subordinate member variable pAlice->subordinate = employeeList(2); pAlice->subordinate[0] = pBob; pAlice->subordinate[1] = pCarol; cout << "Alice supervises:" << endl; for (int i = 0; i < pAlice->subordinate.size(); i++) cout << pAlice->subordinate[i]->name << " makes " << pAlice->subordinate[i]->salary << endl; return 0; }