Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Wednesday, August 21, 2024

C++ program for member function to input and output

Objective:  Create a 'DISTANCE' class with : -

  • feet and inches as data members
  • member function to input distance
  • member function to output distance
  
Code:

#include <iostream>
using namespace std;
class DISTANCE {
private:
    int feet;
    float inches;
public:
    // Member function to input distance
    void inputDistance() {
        cout << "Enter feet: ";
        cin >> feet;
        cout << "Enter inches: ";
        cin >> inches;
    }
    // Member function to output distance
    void outputDistance() const {
        cout << "Distance: " << feet << " feet " << inches << " inches" << endl;
    }
};
int main() {
    // Create an object of the DISTANCE class
    DISTANCE dist;
    // Input distance using the inputDistance member function
    dist.inputDistance();
    // Output distance using the outputDistance member function
    dist.outputDistance();

    return 0;
}


Output:




No comments:

Post a Comment