C++ const Constant Measure objects

Description

C++ const Constant Measure objects

#include <iostream>
using namespace std;
class Measure// w w  w .j av a 2  s.  co  m
{
   private:
   int feet;
   float inches;
   public:                        //2-arg constructor
   Measure(int ft, float in) : feet(ft), inches(in)
   {  }
   void getdist()              //user input; non-const func
   {
      cout << "\nEnter feet: ";  cin >> feet;
      cout << "Enter inches: ";  cin >> inches;
   }
   void showdist() const       ; 
   const func{ cout << feet << "\'-" << inches << '\"'; }
};
int main()
{
   const Measure football(300, 0);
   // football.getdist();            //error: getdist() not const
   cout << "football = ";
   football.showdist();           //OK
   cout << endl;
   return 0;
}



PreviousNext

Related