Class Distance with private attributes Feet (int) and Inches (float). Also, define public member functions:
Set_distance( )
Display_distance( ) – to display the values of class attributes
Include a main ( ) program that creates an instance (object) of type Distance. Ask the user to enter the value of Distance variables and call the appropriate functions to set the object value and then display the object value.
Define the class member functions outside the class using scope resolution operator (::).
Task 7-01
Define a constructor for the class Distance, that should initialize the Distance attributes (feet, inches) to 0.
define an overloaded constructor that initializes the object to certain values.
e.g, Distance d1, d2(10,2) initialize d1 to 0 and values of d2 to 10, 2
Task 7-02
Make changes to the Set_distance( ) function and class constructor so that the values of inches never exceed 11. e.g
obj.Set_distance (10,12) - the value of obj members will be feet: 11, inches: 0
#include <iostream>
using namespace std;
class Distance {
int feet;
float inches;
public:
void set_diatance(int ft, float inch);
void display_distance() const;
};
void Distance::set_diatance(int ft, float inch) {
feet = ft;
inches = inch;
}
void Distance::display_distance() const {
cout << feet << "\' " << inches << "\"" << endl;
}
int main() {
Distance d1;
int ft;
float inch;
cout << "Enter of distance (feet, inches): ";
cin >> ft >> inch;
d1.set_diatance(ft, inch);
cout << endl << "Your distance is ";
d1.display_distance();
return 0;
}
7-01
#include <iostream>
using namespace std;
class Distance {
int feet;
float inches;
public:
Distance();
Distance(int ft, float inch);
void set_diatance(int ft, float inch);
void display_distance() const;
};
Distance::Distance() {
feet = 0;
inches = 0.0;
}
Distance::Distance(int ft, float inch) {
feet = ft;
inches = inch;
}
void Distance::set_diatance(int ft, float inch) {
feet = ft;
inches = inch;
}
void Distance::display_distance() const {
cout << feet << "\' " << inches << "\"" << endl;
}
int main() {
Distance d1;
Distance d2(10, 2);
cout << "d1 = ";
d1.display_distance();
cout << endl << "d2 = ";
d2.display_distance();
return 0;
}
7-02
#include <iostream>
using namespace std;
class Distance {
int feet;
float inches;
void normalize();
public:
Distance();
Distance(int ft, float inch);
void set_diatance(int ft, float inch);
void display_distance() const;
};
void Distance::normalize() {
while (inches >= 12.0) {
inches -= 12.0;
feet += 1;
}
}
Distance::Distance() {
feet = 0;
inches = 0.0;
}
Distance::Distance(int ft, float inch) {
feet = ft;
inches = inch;
normalize();
}
void Distance::set_diatance(int ft, float inch) {
feet = ft;
inches = inch;
normalize();
}
void Distance::display_distance() const {
cout << feet << "\' " << inches << "\"" << endl;
}
int main() {
int ft;
float inch;
cout << "Enter of distance (feet, inches): ";
cin >> ft >> inch;
Distance d1(ft, inch);
cout << endl << "Your distance is ";
d1.display_distance();
cout << "Enter another distance (feet, inches): ";
cin >> ft >> inch;
d1.set_diatance(ft, inch);
cout << endl << "Your distance is ";
d1.display_distance();
return 0;
}
Comments
Leave a comment