#include <string> #include <list> #include <iostream> #define MAX 100 // Item 2: Prefer consts, enums, and inlines to #defines class Shape{ public: Shape(){} ~Shape(){} // Item 7: Declare destructors virtual in polymorphic base classes virtual void Draw(); // ... }; class Point: public Shape{ public: Point() {} // Item 4: Make sure that objects are initialized before they're used. Comment: So we must initialize the members in the constructor. Point(int _x, int _y) { x = _x; y = _y; } ~Point(){ std::cout<< "x :=" << x << "y:=" << y << std::endl; }; virtual void Draw() { // ... } private: int x; int y; // .... }; class Picture { public: Picture( std::string name, // const std::string& name, comment: user defined object. ( passed by value --> passed by reference ), use "const" if it won't be changed std::list<Shape*> pic ); private: std::string theName; std::list<Shape*> thePic; }; Picture::Picture( std::string name, // Item 4: Make sure that objects are initialized before they're used. Comment: Use the member initialization list instead of the assignments. std::list<Shape*> pic) // :theName( name ), thePic( pic ) { theName = name; thePic = pic; // ISSUE: Comment : the list store the pointers. } int main() { Picture A( "string",pic ); // "string" to std::string( "string" ) is implicit type conversion. // There is one way to prevent the implicit type conversion. Declare the constructors explicit. Refer exp2. Picture B = A; // Item 5: Know what functions C++ silently writes and calls. } // exp2 //class C { //public: // explicit C(int x){}; // not a default constructor //}; //void fun( C c ) //{ // // ... //} //fun( 1 ); // error. //fun( C(1) ); // work. // exp3 //class Empty{}; // it's essentially the same as if you'd written this: // //class Empty { //public: // Empty() { ... } // default constructor // Empty(const Empty& rhs) { ... } // copy constructor // ~Empty() { ... } // destructor — see below // Empty& operator=(const Empty& rhs) { ... } // copy assignment operator //}; // These functions are generated only if they are invoked. Like // Empty e1; // default constructor and destructor // Empty e2(e1); or Empty e2 = e1 // copy constructor // e2 = e1; // copy assignment operator // Item 6: Explicitly disallow the use of compiler-generated functions you do not want // Solution: declare the corresponding member functions private and give no implementations. or class HomeForSale { public: ... private: ... HomeForSale(const HomeForSale&); // declarations only HomeForSale& operator=(const HomeForSale&); }; //class Uncopyable { //protected: // allow construction // Uncopyable() {} // and destruction of // ~Uncopyable() {} // derived objects... //private: // Uncopyable(const Uncopyable&); // ...but prevent copying // Uncopyable& operator=(const Uncopyable&); //}; // //class HomeForSale: private Uncopyable { // class no longer // ... // declares copy ctor or //}; // copy assign. operator |