Q3: Write a program to perform basic to class conversion [Class name: “conversion” with data members: feet and inches (both int)] Convert basic type(int) height in terms of total number of inches into equivalent feet and inches (class type) and display output on screen.
#include <iostream>
class conversion {
public:
conversion ();
conversion(int);
void display() const;
private:
int feet;
int inches;
};
conversion::conversion() : feet(0), inches(0)
{}
conversion::conversion(int x) : feet(x/12), inches(x%12)
{}
void conversion::display() const
{
std::cout << "feet: " << feet << " inches: " << inches;
}
int main() {
conversion distance;
int x;
std::cout << "Enter a distance in inches: ";
std::cin >> x;
distance = x;
std::cout << "It is equal ";
distance.display();
return 0;
}
Comments
Leave a comment