面向对象的考试题
#include <iostream>
#include <ctime>
using namespace std;
class Distance {
private:
int feet, inch;
public:
Distance() {
feet = 0;
inch = 0;
}
Distance(int input_inch) {
feet = input_inch / 12;
inch = input_inch % 12;
}
Distance(int input_feet, int input_inch) {
feet = input_feet;
feet += (input_inch / 12);
inch = input_inch % 12;
}
friend ostream& operator<<(ostream& out, Distance& D);
friend istream& operator>>(istream& out, Distance& D);
Distance operator/(const int& n) const;
public:
void setFeet(int input_feet) {
feet = input_feet;
}
void setInch(int input_inchs) {
feet += (input_inchs / 12);
inch = input_inchs % 12;
}
int getFeet() {
return feet;
}
int getFeet() const{
return feet;
}
int getInch() {
return inch;
}
int getInch() const{
return inch;
}
Distance operator+(const Distance& d) {
int new_feet = this->feet+d.feet;
int new_inchs = this->inch + d.inch;
Distance ret(new_feet, new_inchs);
return ret;
}
Distance operator+(const Distance& d) const{
int new_feet = this->feet + d.feet;
int new_inchs = this->inch + d.inch;
Distance ret(new_feet, new_inchs);
return ret;
}
Distance operator+=(const Distance& d) {
this->feet += d.feet;
this->inch += d.inch;
this->feet += this->inch / 12;
this->inch %= 12;
return *this;
}
bool operator==(const Distance& d) const {
if (this->feet == d.feet && this->inch == d.inch) {
return true;
}
return false;
}
bool operator!=(const Distance& d) const {
if (this->feet != d.feet || this->inch != d.inch) {
return true;
}
return false;
}
bool operator>=(const Distance& d) const {
if (this->feet > d.feet) {
return true;
}
else if(this->feet == d.feet && this->inch >= d.inch){
return true;
}
return false;
}
bool operator>(const Distance& d) const {
if (this->feet > d.feet) {
return true;
}
else if (this->feet == d.feet && this->inch > d.inch) {
return true;
}
return false;
}
bool operator<=(const Distance& d) const {
if (this->feet < d.feet) {
return true;
}
else if (this->feet == d.feet && this->inch <= d.inch) {
return true;
}
return false;
}
bool operator<(const Distance& d) const {
if (this->feet < d.feet) {
return true;
}
else if (this->feet == d.feet && this->inch < d.inch) {
return true;
}
return false;
}
};
ostream& operator<<(ostream& out, Distance& D)
{
cout << D.feet << " feet and " << D.inch << " inch" ;
return out;
}
ostream& operator<<(ostream& out, const Distance& D)
{
cout << D.getFeet() << " feet and " << D.getInch() << " inch";
return out;
}
istream& operator>>(istream& in, Distance& D) {
in >> D.feet>> D.inch;
return in;
}
Distance Distance::operator / (const int& n) const
{
int totalInches = 12 * feet + inch;
return Distance(totalInches / n);
}
Distance computeAverageDistance(Distance arr[], int size) {
int ret_feet = 0,ret_inchs=0;
for (int i = 0; i < size; i++) {
ret_feet += arr[i].getFeet();
ret_inchs+= arr[i].getInch();
}
ret_feet /= size;
ret_inchs /= size;
ret_feet += ret_inchs / 12;
ret_inchs %= 12;
Distance ret(ret_feet, ret_inchs);
return ret;
}
int main()
{
Distance d1;
const Distance d2(57);
Distance d3(4, 18);
cout << "d1 is " << d1 << endl;
cout << "d2 is " << d2 << endl;
cout << "d3 is " << d3 << endl;
cout << endl;
d1.setFeet(21);
d1.setInch(45);
cout << "d1 is " << d1.getFeet() << " feet and " << d1.getInch() << " inch" << endl;
cout << "d2 is " << d2.getFeet() << " feet and " << d2.getInch() << " inch" << endl;
cout << "d3 is " << d3.getFeet() << " feet and " << d3.getInch() << " inch" << endl;
cout << endl;
const Distance d4 = d1 + d2;
const Distance d5 = d2 + d1;
cout << "d4 which is d1 + d2 is " << d4 << endl;
cout << "d5 which is d2 + d1 is also same " << d5 << endl;
d1 += d5;
cout << "d1 += d5 results to d1 = " << d1 << endl;
cout << endl;
cout << "The division operation " << d1 << " divided by 4 results to " << d1 / 4 << endl;
cout << endl;
if (d2 == d2)
cout << d2 << " and " << d2 << " are equal." << endl;
if (d4 != d1)
cout << d4 << " and " << d1 << " are not equal." << endl;
if (d4 > d2)
cout << d4 << " is greater than " << d2 << endl;
if (d5 >= d2)
cout << d5 << " is greater than or equal to " << d2 << endl;
if (d3 < d5)
cout << d3 << " is less than " << d5 << endl;
if (d2 <= d4)
cout << d2 << " is less than or equal to " << d4 << endl;
cout << endl;
//Create an array of distance objects
srand(time(0));
int size = 5 + rand() % 5;
Distance* arr = new Distance[size];
for (int i = 0; i < size; i++)
{
cout << "Enter a distance: ";
cin >> arr[i];
}
//Print the elements of the array
for (int i = 0; i < size; i++)
cout << "The element at index " << i << " is " << arr[i] << endl;
cout << endl;
//Print the average distance (This calculation must use integer division)
Distance avg = computeAverageDistance(arr, size);
cout << "The average of all the elements of the array is " << avg << endl;
delete[] arr;
system("Pause");
return 0;
}
在这里插入代码片