如何调用operator函数添加两个对象?

时间:2017-07-13 01:19:47

标签: c++ class object volume area

尝试开发运算符==来比较两个球,如果它们具有相同的半径,则两个球被认为是相等的,并且运算符>来比较两个球。要查看一个ball是否具有比另一个更大的半径,因为我们说ball x是>比另一个ball y+=将右侧操作数的音量添加到左侧操作数的音量中。它就像融化两个金属球来制作一个金属球。新球的半径是(r1 ^ 3 + r2 ^ 3)的立方根。希望使用pow()函数来计算多维数据集值和多维数据集根值。操作员+将两个球加在一起并返回一个新球。新球的大小是+连接的两个操作数的大小之和。

main()函数中,无法使用ball m(10)添加ball n(20)来创建另一个球d,例如d = m + n。

int main()
{
    //use ball
    ball x; float re;
    //radius of ball y is set to 10
    ball y(10);
    //asks for radius of x?
    cout << "Enter radius for ball x: ";
    cin >> re;
    //sets the radius of x
    x.set_radius(re);

    ball m(10);
    ball n(20);
    ball d;
    d = m + n;

    //cout << "The radius of ball d is " << m.;

    system("pause");
    return 0;
}

//ball.h
{
    class ball
    {
    public:
        //sets the intial raduis to 0
        ball() {
            radius = 0;
        }
        ball(float radii) {
            radius = radii;
        }
        float get_radius() {
            return radius;
        }
        void set_radius(float redly) {
            radius = redly;
        }
        bool operator == (ball x) {
            if (radius == x.radius)
                return true;
            else
                return false;
        }
        bool operator > (ball x) {
            if (radius > x.radius)
                return true;
            else
                return false;
        }
        bool operator += (ball x) {
            radius += x.radius;
        }
ball operator + (ball a, ball b) {
        ball d;
        d += a;
        d += b;
        return d;
    }
    private:
        float radius;
    };
}
#endif

3 个答案:

答案 0 :(得分:0)

如果你只是寻找(x_volume / y_volume)%和(x_surfacearea / y_surfacearea)%

我建议做:

float vol_over_y() {
    float v;
    v = ((radius * radius * radius)/(10*10*10));
    return v;
}
float sa_over_y() {
    float a;
    a = (radius * radius /(10*10));
    return a;
}    

因为其他常数如(4.0 / 3.0)* 3.14的体积和3.14的表面积取消了。

如果y发生变化,

float vol_over_y(float y_rad) {
    float v;
    v = ((radius * radius * radius)/(y_rad*y_rad*y_rad));
    return v;
}
float sa_over_y(float y_rad) {
    float a;
    a = (radius * radius /(y_rad*y_rad));
    return a;
}    

答案 1 :(得分:0)

你想的太多了。这只是你主要逻辑中的一点调整:

ball x;
ball y(10);
// your code to construct x

cout << "The volume of x is " << ( x.volume() / y.volume() )<< "% of the volume of y."<< endl;

// similar for surface area, try it out yourself

如果你真的需要在一个方法中做这件事(这非常荒谬),你应该把另一个球传入并计算:

float volume_ratio_to(const ball& base) {
  return volume() / base.volume();
}

你的主要逻辑变为

cout << "The volume of x is " << x.volume_ratio_to(y) << "% of the volume of y."<< endl;

已针对您更新的问题进行了编辑:

+运算符不适合您的原因是因为您没有对运算符进行过载。

如果您希望它成为ball的成员,+ operator应该只接受一个参数。

即。看起来像:

class ball {
    //....
    ball operator + (ball another);
}

或者,不要让它成为ball

的成员
// outside ball class
ball operator + (ball a, ball b) {....}

有关如何使加法运算符过载的详细说明,请参阅Overloaded Addition assignment operator in C++ for two /more than two objects?

您的代码中还存在很多其他问题

  • 您应该通过引用而不是按值
  • ball传递给方法
  • 您应该考虑在各个地方添加const
  • +=逻辑是完全错误的。注意你引用的内容:
      

    + =将右侧操作数的音量添加到音量   左侧操作数。它就像熔化两个金属球来制造一个   金属球。新球的半径是(r1 ^ 3 + r2 ^ 3)

    的立方根

答案 2 :(得分:0)

//This helps

    int main()
    {  
        //use ball
        ball x; float re;
        ball y(10);
        cout << "Enter radius for ball x: " << endl;
        cin >> re;
        x.setl(re);

        cout << "The volume of x is " << (x.volume()/y.volume())*100 << "% 
of the volume of y."<< endl;
        cout << "The surfacearea of x box is " << 
(x.surface_area()/y.surface_area())*100 << "% of 
the surfacearea of y." << endl;

        system("pause");
        return 0;
    }

//ball.h
#pragma once
#ifndef Ball
#define Ball
namespace bsize
{
    class ball
    {
    public:
        ball(){
            radius = 0;
        }
        ball(float radii) {
            radius = radii;
        }
        float volume() {
            float v;
            v = ((4.0/3.0)* 3.14 * (radius * radius * radius));
            return v;
        }
        float surface_area() {
            float a;
            a = (4 * 3.14 * radius * radius);
            return a;
        }
        float get_radius(){
            return radius;
        }
        void set_radius(float redly) {
            radius = redly;
        }
    private:
        float radius;
    };
}
#endif