/******************************* OVLCONST.CPP ********************************** Example showing overloading the constructor of a class.          1998-09-15 *
*                                                                   Agner Fog *
*                                                                             *
* In C++, several functions can have the same name if the type or number of   *
* parameters are different. The compiler automatically chooses the version of *
* the function that fits the type of parameters best when calling a function  *
* with that name.                                                             *
* (Overloading of a simple function is demonstrated in the file OVERLOAD.CPP) *
*                                                                             *
* The overload mechanism is often used for overloading the constructor of     *
* a class, as shown in this example. An object of the class can be created    *
* with any of the constructors, depending on the parameters used when         *
* creating the object.                                                        *
******************************************************************************/
         
#include <iostream.h>#include <conio.h>#include <stdlib.h>
class commodity {   public:   commodity (char * newname, float newprice);   commodity (char * newname);   commodity (int number);   commodity (void);                // default constructor   commodity (commodity & parent);  // copy constructor   void show (void);   float price;   private:   char name[40];};// constructor with string and float parameterscommodity::commodity (char * newname, float newprice) {   strcpy (name, newname);   price = newprice;}// constructor with string parametercommodity::commodity (char * newname) {   strcpy (name, newname);   price = 99.99;}// constructor with integer parametercommodity::commodity (int number) {   // convert number to string   itoa (number, name, 10);   price = 99.99;}// default constructor has no parametercommodity::commodity (void) {   strcpy (name, "Unnamed commodity");   price = 0;}// copy constructor. This constructor makes a copy of another object 'parent'// of the same class. It takes a reference to the parent object as parameter.// The copy constructor is called automatically whenever an object is copied.commodity::commodity (commodity & parent) {   strcpy(name, parent.name);   price = parent.price;}// public member function showvoid commodity::show (void) {   cout << "\n" << name << " cost " << price;}void main () {   // create with constructor commodity(char*, float)   commodity strawberry("Strawberries", 10.25);   // create with constructor commodity(char*)   commodity raspberry("Raspberries");   // create with constructor commodity(int)   commodity blueberry(12046);   // create with default constructor   commodity other;   // create with copy constructor   commodity MoreStrawberries = strawberry;   // show contents:   strawberry.show();   raspberry.show();   blueberry.show();   other.show();   MoreStrawberries.show();   getch();}
