// file: foo.cc // // system include files // #include #include // define a simple class // class Toaster { protected: // define some data // char* str; long len_d; long width_d; float weight_d; // define some simple functions // public: Toaster() { str = new char[10]; len_d = (long)0; width_d = (long)0; weight_d = (float)0.0; fprintf(stdout, "default constructor: no args\n"); } Toaster(float weight_a, long len_a = 27) { len_d = len_a; width_d = (long)0; weight_d = (float)weight_a; fprintf(stdout, "default constructor: weight/len = %f %d\n", weight_d, len_d); } ~Toaster() { delete [] str; fprintf(stdout, "... deallocating memory ...\n"); } long get_len() { return len_d; } long get_width() { return width_d; } float get_weight() { return weight_d; } // define some functions // bool display(FILE* fp, char*); 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); private: // end of class }; // define a main program // int main(int argc, char** argv) { // declare an object // Toaster kitchenaide(27.0); // Toaster bosch; // set some values // // kitchenaide.set_values(99.0); // kitchenaide.display(stdout, (char*)"kitchenaide"); // print some stuff // // bosch.display(stdout, (char*)"bosch"); // exit gracefully // return(0); } // method: display() // bool Toaster::display(FILE* fp, char* str) { // print the contents // fprintf(fp, "object name: %s\n", str); fprintf(fp, "length = %d\n", len_d); fprintf(fp, "width = %d\n", width_d); fprintf(fp, "weight = %f\n", weight_d); // exit gracefully // return true; } // method: set_values() // bool Toaster::set_values(long len_a, long width_a, float weight_a) { // assign values // len_d = len_a; width_d = width_a; weight_d = weight_a; // exit gracefully // return true; }