C++ Methods (class member functions)

Static

class X
{
   int mem;
   public: static void printHi();

};

 

Can invoke without creating instance of class

X.printHi();

 

Overloading

Having multiple methods of same name, but different number or type of parameters.

Class with method overloading

class date
{
  
     int day, //internal storage scheme
                  month, //this is private to the class
         
year;

          char *message; //string message reflecting date

   public:
     void new_date(int,int,int); //method to change the date

     void new_date(char *); //method to change the date

          char* give_date(); //report date

          date(int); //constructor to set day only

     ~date(); //destructor

};

Now define the methods

date::~date()
{
     delete message; //return allocated memory to free memory

}

date::date(int d)
{
   day = d;

}

void date::new_date(int d, int m, int y)
{
   day = d;
   month=m;
   year=y;
}

void date::new_date(char* d)
{
   message = d;
}

void date::give_date()
{
   char *buf= new char[80];

   sprintf(buf, "%s %d, %d", mname[month],day,year);


   return buf;
}