C++ examples for Class:object
Constant Measure objects
#include <iostream> using namespace std; class Measure/*from w w w. j a v a 2 s . c o 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 void f() { 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; }