// file: lecture_27/myprog.h // // system include files // #include #include #include // define a simple class // class Toaster { protected: // define some data // char* name_d; char* model_d; long len_d; long width_d; float weight_d; // define some simple functions // public: // default constructor // Toaster() { // initialize data // name_d = (char*)NULL; model_d = (char*)NULL; len_d = (long)27; width_d = (long)0; weight_d = (float)0; // display some information about the object // // fprintf(stdout, "Default Constructor: len_d is located at %u\n", &len_d); // fprintf(stdout, "Default Constructor: width_d is located at %u\n", &width_d); } // constructor with an argument // Toaster(char* name_a) { // initialize data // name_d = new char[strlen(name_a)]; strcpy(name_d, name_a); model_d = (char*)NULL; len_d = (long)27; width_d = (long)0; weight_d = (float)0; // display some information about the object // //fprintf(stdout, "Constructor with arg: name_d = %s\n", name_d); } // default destructor // ~Toaster() { //fprintf(stdout, "Default Destructor: cleaning up memory!\n"); if (name_d != (char*)NULL) { delete [] name_d; name_d = (char*)NULL; } if (model_d != (char*)NULL) { delete [] model_d; model_d = (char*)NULL; } } // implement some inline functions // long get_len() { return len_d; } //long get_len(char* str = (char*)NULL) { // fprintf(stdout, "... here I am ...\n"); //Toaster tmp; //return (long)-1; //} long get_width() { return width_d; } float get_weight() { return weight_d; } // define some useful public functions // bool display(FILE* fp, char* object_name = (char*)NULL); bool display(Toaster& tst) { tst.display(stdout, (char*)"tst"); return true; } bool set_values(long len, long width, float weight); // demonstrate an overloaded function // bool compare(Toaster arg1); bool compare(Toaster* arg1); bool compare(Toaster& arg1); // add some operator overloads // bool add(Toaster& arg1, Toaster& arg2); Toaster operator + (Toaster const &obj) { fprintf(stdout, "hello world...\n"); Toaster tmp; tmp.len_d = len_d + obj.len_d; return tmp; } Toaster operator * (Toaster const &obj) { fprintf(stdout, "multiplication... hello world...\n"); Toaster tmp; tmp.len_d = len_d * obj.len_d; return tmp; } Toaster operator / (Toaster const &obj) { fprintf(stdout, "division... hello world...\n"); Toaster tmp; tmp.len_d = len_d + obj.len_d; return tmp; } Toaster operator - (Toaster const &obj) { Toaster tmp; tmp.len_d = len_d - obj.len_d; return tmp; } private: // print the data of manufacturing // //bool display(FILE* fp); // end of class };