C++ Pointer Accessing member functions by pointer

Description

C++ Pointer Accessing member functions by pointer

#include <iostream>
using namespace std;
class Measure/*ww  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;
}



PreviousNext

Related