#include <iostream.h> #include <string.h> class Product{ public: Product() : name(NULL) {} Product(char* str); const Product& operator=(const Product& ); void Print() { cout << "Product: " << name << endl; } const char* getProduct() const { return name; } private: char * name; }; Product :: Product(char* str){ name = new char[strlen (str)+1]; strcpy (name, str); } const Product& Product :: operator=(const Product& p){ delete [] name; name = new char[strlen(p.getProduct())+1]; strcpy(name, p.getProduct()); return *this; // This will allow assignments to be chained } int main(){ Product p1("Hammer"), p2("Shovel"), p3; p3 = p2; // p2 and p3 will have equivalent name strings p2.Print(); p3.Print(); p3 = p2 = p1; // p1, p2, and p3 will have equivalent name strings p1.Print(); p2.Print(); p3.Print(); return 0; } |