/////////////////////////////////////////////////////////////// // MapAssignment.cpp - define map assignment operator // // Exam1:Q9 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 05 // /////////////////////////////////////////////////////////////// #include #include #include #include class A { public: A(const std::string& str) : s(str) {} std::string getString() const { return s; } private: std::string s; }; class B : public A { public: typedef std::list SList; typedef std::map< std::string, SList > BMap; B() : pM(new BMap), A("") {} // answer E1:Q9 ////////////////////////////// B(const B& b) : A(b.getString()) { pM = new BMap(b.getMap()); } // end /////////////////////////////////////// BMap& getMap() { return (*pM); } const BMap& getMap() const { return *pM; } // answer E2:Q5 ////////////////////////////// B& operator=(const B& b) { if(this == &b) return *this; A::operator=(b); *pM = *(b.pM); return *this; } // end /////////////////////////////////////// int size() { return pM->size(); } private: BMap* pM; }; // void showB(const B& b) { B::BMap::iterator it; B::BMap bmap = b.getMap(); for(it=bmap.begin(); it!=bmap.end(); ++it) { std::cout << "\n " << it->first.c_str() << " : "; B::SList::iterator jt; for(jt=it->second.begin(); jt!=it->second.end(); ++jt) std::cout << jt->c_str() << " "; } } void main() { B b; B::BMap& bmap = b.getMap(); B::SList mammals; mammals.push_back("whale"); mammals.push_back("man"); mammals.push_back("ox"); B::SList reptiles; reptiles.push_back("snake"); reptiles.push_back("alligator"); std::pair< std::string, std::list > p; p.first = "mammals"; p.second = mammals; bmap.insert(p); bmap[std::string("reptiles")] = reptiles; B copy = b; showB(b); B anotherCopy; anotherCopy = copy; showB(b); std::cout << "\n\n"; }