-1

Trying to develop operator == to compare two balls where two balls are considered equal if they have the same radius and operator > to compare two balls. To see if one ball has a bigger radius than another one, for let's say ball x is > than another ball y. += to add the volume of the right-side-operand to the volume of the left-side-operand. It is like to melt two metal balls to make one metal ball. The new ball's radius is cube root of (r1^3 + r2^3). Wish to use pow() function to calculate the cube value and cube root value. operator + to add the two balls together and return a new ball. The size of the new ball is the sum of the size of the two operands connected by the +.

In the main() function, couldn't add ball m(10) with ball n(20) to create another ball d, like 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
Exiler
  • 27
  • 13
  • 1
    You have three problems: 1) how to calculate the volume of a sphere, 2) how to calculate the area of a sphere, 3), how to compare two numbers. Tackle these problems *separately,* and if you get stuck on one of them you can come here for help. – Beta Jul 13 '17 at 01:33
  • @Beta I just got an idea on three of them and made a change for `a = (4 * 3.14 * radius * radius);` and Iearned division would help to compare them. And do you think my class `ball` is over-defined? – Exiler Jul 13 '17 at 01:50
  • No, it doesn't look overdefined to me. – Beta Jul 13 '17 at 01:52
  • @Beta Now, how can I pass the radius of `y` to the function `volume()` to calculate another object and compare? – Exiler Jul 13 '17 at 02:01
  • @Aspersum You shouldn't do so. Just have a piece of logic which creates two balls, and calling their `volume()` and `surface_area()` then calculate the ratio. This is how you actually reuse your `ball` for different situations. – Adrian Shum Jul 13 '17 at 02:35
  • beware of your naming too. `surfacearea` is better named `surfaceArea` or `surface_area` (depending on the naming style you adopt). `getl` / `setl` should be named with something like `get_radius` / `set_radius` – Adrian Shum Jul 13 '17 at 02:40
  • If however, the methods are called several times and will be called in other cpp files, you should add the methods inside the class. – Jisu Hong Jul 13 '17 at 02:53
  • @RayHong Not necessary. There are other way to make code resuable. It can be a simple function. When you are adding a method to a class, it means that it is a behavior that class should provide. It is more a design decision instead of simply "called several times" – Adrian Shum Jul 13 '17 at 03:08
  • It can be a simple function, but the function has to be copied everytime in order to be reused in every cpp files. Instead if it is included in the class as a class function, you don't have to do that. – Jisu Hong Jul 13 '17 at 04:33
  • @RayHong I guess you may want to learn more C++ before claiming so. Function can be declared in header and used every where. You do not need to make it a "class function" for that – Adrian Shum Jul 13 '17 at 05:05
  • @Exile You have edited your question and changed almost everything you asked. You should raise a new question instead of keep modifying your question for something new. – Adrian Shum Jul 24 '17 at 01:44
  • @AdrianShum I thought it is still with that two objects I'm working with. – Exiler Jul 24 '17 at 02:06
  • @Exile Stackoverflow is not a place that you get help to solve your problem. It is a Q&A site that you ask programming question, people answer, and Q&A are kept as reference to other people which faced similar problem. So no one care if it is the same homework that you are doing. As long as it is a different question, you should ask in new question. – Adrian Shum Jul 24 '17 at 02:11

3 Answers3

0

If you are only looking for (x_volume/ y_volume)% and (x_surfacearea/y_surfacearea)%

I suggest doing :

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

because other constants like (4.0/3.0)* 3.14 in volume and 3.14 in surface area cancel out.

If in case y changes,

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;
}    
Jisu Hong
  • 724
  • 1
  • 7
  • 22
  • -1 for several reasons: 1. Bad naming. `y` means nothing. Should be named to provide idea of `radius of other ball` 2. It is not a good idea to intrude the `ball` class with something that it shouldn't be aware of. If later the question ask you to do something like: compare the Surface-to-Vol ratio of two balls, then you are going to add yet another method in `ball`, which is obviously not perferrable – Adrian Shum Jul 13 '17 at 02:38
  • 1. why does "y" mean nothing when it directly helps understanding of the asker? I am not the one who named it "y" 2. If the methods are going to be called several times it is *preferrable to add in the class and you know why. If not, you are right. However, you and I both cannot assume. Although, I agree in life, "It is not a good idea to intrude the ball(s) with something that it shouldn't be aware of" – Jisu Hong Jul 13 '17 at 02:51
  • 1
    1. In OP's code, `y` is a ball. You are not even aligning with his meaning (which you are using it as `radius of y`) 2. I have added a comment to question to explain why. This specific case is actually a bad idea to add as method – Adrian Shum Jul 13 '17 at 03:11
  • 1. I understand now, thanks 2.Whether it is a bad idea or not is not important and it is beyond our consideration because he asked how to define it in his class. – Jisu Hong Jul 13 '17 at 04:40
  • His question does not ask for "defining a new function in the class to calculate the ratio". You may clarify with him though, instead of just considering other interpretation of question as wrong – Adrian Shum Jul 13 '17 at 04:51
  • Yeah you too... – Jisu Hong Jul 13 '17 at 05:02
  • Well, I simply said this design is a bad idea, I haven't said your answer is violating the question. – Adrian Shum Jul 13 '17 at 05:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149067/discussion-between-ray-hong-and-adrian-shum). – Jisu Hong Jul 13 '17 at 05:16
0

You thought too much. It is simply a little tweak in your main logic:

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

If in case you really need to do it in a method(which is quite ridiculous imho), you should just pass the other ball in and calculate:

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

And your main logic become

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

Edited for your updated question:

The reason why + operator does not work for you is because you didn't overloaded the operator right.

If you want it to be member of ball, the + operator should only takes one argument.

i.e. looks like:

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

Or, don't make it member of ball:

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

Refer to Overloaded Addition assignment operator in C++ for two /more than two objects? for more detailed description on how to overload addition operator.

There are quite a lot of other problems in your code too

  • You should have passed balls to methods by reference instead of by value
  • You should have considered adding const in various places
  • You += logic is totally wrong. Pay attention to what you quoted:

    += to add the volume of the right-side-operand to the volume of the left-side-operand. It is like to melt two metal balls to make one metal ball. The new ball's radius is cube root of (r1^3 + r2^3)

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • He was not looking for this. He specifially asked "The ??? should be calculated using the values returned by the functions defined in the class. " – Jisu Hong Jul 13 '17 at 04:36
  • It is calculated (x.volume / y.volume) and using the values returned by functions defined in the class (volume()) – Adrian Shum Jul 13 '17 at 04:44
  • " In my class, I need help calculating and comparing the volume and surface-area one with the other in their respective functions. " – Jisu Hong Jul 13 '17 at 04:45
  • If you are taking it so literally: please tell me where the "comparison" is happening in your answer? – Adrian Shum Jul 13 '17 at 04:55
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
Exiler
  • 27
  • 13