Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My friend and I are working on a class called rectangle.
We have a member function that returns a square (object of our class)
of an area equivalent to the average area of 2 squares.
so (area of square 1 + area of square 2)/ 2
.the side of the returned square must be rounded to the nearest ones. We were working on this and upon debugging we realized that const at the end of our function prevents us from altering values.
I hope I have provided enough information.
My question is what should our approach be if we cannot modify values inside this function? please note that we are not to create new functions simply code the implementation of the class. Also there are other functions but I'm only displaying these because they pertain most to my question and the rest seem to be fine.

C++
class square
{
public: 
    square(int s);
    int plus(square sq) const;
    square avgSquare(square sq) const; // this the function we are trying to fix
    ~square(); // default constructor
 
private:
    int side;
};
 
//_________class implementation below___________

// parameterized constructor with default parameter 

square::square(int s=0) 
{
    side = s;
}

// the function below seems fine( area is another function that works)

int square::plus(square sq) const
{
    int plus = sq.area() + sq.area();
    int area1 = sq.area();
    int area2 = sq.area();
    plus = area1 + area2;
    return plus;
}
 
//ok folks so below is the function we are trying to fix.
//I have written how we filled it in the "what have you tried" section

square square::avgSquare(square sq) const
{
}


What I have tried:

we first coded how to get the average. And there is a bit of disagreement. I will show the two ways we think this should be done. But we were unaware about the restrictions placed on us by const

C++
square square::avgSquare(square sq) const
{
    //way one
    double Asq = round(plus(sq)/2);
    return Asq;
    //way two
    double Asq = round(sq.plus()/2);
    return Asq;
  
    return sq.plus()/2;
}
Posted
Updated 29-Nov-17 21:30pm
v2

Have you tried to compile this? Way two will not compile because you did not pass it an argument.

There are several different ways avgSquare could be implemented. Here is one way as a static method.

C++
static double square::avgSquare( square & s1, square & s2 )
{
    int areasum = s1.area() + s2.area();
    double asq = areasum / 2.0;
    return asq;
}
 
Share this answer
 
v3
If you can make a function const, you should do it. You should also pass parameters that are not basic types by const reference if they are not modified instead of passing them by value.

But there is a problem with your example:
The function avgSquare is defined as returning a square but your implementation returns a double or an int.

A possible solution might be:
C++
// Implementation for this function not shown
int area() const;

int avgSquare(const square& s1, const square& s2) const
{
    return (s1.area() + s2.area()) / 2;
}

// An optional additional implementation
int avgSquare(const square& other) const
{
    return avgSquare(*this, other);
}
If you need to return a double just divide by 2.0:
double avgSquare(const square& s1, const square& s2) const
{
    return (s1.area() + s2.area()) / 2.0;
}

There are only a few simple operations so that there is no need to use other member functions like plus().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900