Accessing member functions by pointer - C++ Class

C++ examples for Class:Class Pointer

Description

Accessing member functions by pointer

Demo Code

#include <iostream>
using namespace std;
class Measure/*from w  w w . j  a  v  a 2s  .c o  m*/
{
   private:
   int feet;
   float inches;
   public:
   void getdist()        //get length from user
   {
      cout << "\nEnter feet: ";  cin >> feet;
      cout << "Enter inches: ";  cin >> inches;
   }
   void showdist()
   { cout << feet << "\'-" << inches << '\"'; }
};
int main()
{
   Measure dist;           //define a named Measure object
   dist.getdist();          //access object members
   dist.showdist();         //   with dot operator
   Measure* distptr;       //pointer to Measure
   distptr = new Measure;  //points to new Measure object
   distptr->getdist();      //access object members
   distptr->showdist();     //   with -> operator
   cout << endl;
   return 0;
}

Result


Related Tutorials