C++ examples for Class:Member Function
Create member square() function for Measure
#include <iostream> using namespace std; class Measure/*w w w . j a v a 2 s .c om*/ { private: int feet; float inches; public: Measure() : feet(0), inches(0.0) { } Measure(int ft, float in) : feet(ft), inches(in) { } void showdist() { cout << feet << "\'-" << inches << '\"'; } float square(); //member function }; float Measure::square() //return square of { //this Measure float fltfeet = feet + inches/12; //convert to float float feetsqrd = fltfeet * fltfeet; //find the square return feetsqrd; //return square feet } int main() { Measure dist(3, 6.0); //two-arg constructor (3'-6") float sqft; sqft = dist.square(); //return square of dist and square cout << "\nMeasure = "; dist.showdist(); cout << "\nSquare = " << sqft << " square feet\n"; return 0; }