实现Distance类 c++ oop

面向对象的考试题

#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;
}


在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清欢_小铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值