My first post and a class

Hi, I'm am working on a code to overloaded the addition, multiplication, and subtraction operators. I want to do this in my class, which I guess would be my header file. Later in my main file I want to use the overloaded operators to add, multiply, and subtract two polynomials. I'm not asking for answers. I just want to tips and I'm heading in the right direction. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#ifndef CAKE
#define CAKE
 
class Cake
{
//define private member functions
private:
int coef[10]; // array of coefficients
int deg; 
 
//define public member functions
public:
Cake ();
Cake operator +(const Cake &) const; // overloaded addition operator
Cake operator *(const Cake &) const; // overloaded multiplication operator
Cake operator -(const Cake &) const; // overloaded subtraction operator
};

#endif


I'm so proud of my little class. It was just little cake now it's a overloaded cake.

P.S I'm doing it baby steps, so I can recall it if I need it. I'm just asking for tips on the Class. I didn't start my main file yet. I want to wait until this is done.
Last edited on
closed account (zb0S216C)
Your class is fine, except for one minor detail: your operators should return references to Cake and shouldn't be constant.

Wazzak
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#ifndef CAKE
#define CAKE
 
class Cake
{
//define private member functions
private:
int coef[10]; // array of coefficients
int deg [10]; 
 
//define public member functions
public:
Cake ();
Cake operator +( Cake ); // overloaded addition operator
Cake operator *( Cake ); // overloaded multiplication operator
Cake operator +( Cake ); // overloaded subtraction operator
void enterTerms; 
return Polynomial;
};

#endif 


Is this better? I'm a little confused, by taking the const out. When I look at examples of overloading it's usually like my orginal. Can you explain why you suggested I take it out?

Thanks Framwork :)
closed account (zb0S216C)
I think you misinterpreted my post :) Allow me to elaborate what I mean with this example:

1
2
3
4
5
class Cake
{
    public:
        Cake &operator + ( Cake & );
};

I've added the & operator before the operator keyword. This will return a reference to *this. Now, if you want to return a new instance of Cake, you would omit the & operator.

Within the parameter list, I added another & operator. This now means that the operator takes a reference to an instantiation of Cake. If I omitted the & operator, a copy of the passed instantiation of Cake would be made and the changes would not take effect.

If you placed the const qualifier after the parameter list, you would restrict the operator from making changes to the data members.

You should also note that non-bitwise operators (such as +, -, /, etc) should not modify the neither left-hand or right-hand operand, but return a copy of the two operands added together. Only bitwise operators should modify the operands.

Wazzak
Your class is fine, except for one minor detail: your operators should return references to Cake and shouldn't be constant.


There is no such requirement, returning an Rvalue is completely legal. And of course those operators should be const, or do you think 5+2 should be illegal because 5 is a constant value? Also, you "corrected" him into removing the const specifier from the argument, which actually belongs there because adding a const object should be legal with any sane use of the + operator.
Last edited on
closed account (zb0S216C)
hanst99 wrote:
There is no such requirement, returning an Rvalue is completely legal. (sic)

Never said it wasn't. The difference between an R/LValue is that LValues must refer to an object. RValues can be anything (within reason).

hanst99 wrote:
And of course those operators should be const (sic)

Doesn't make a difference either way. We don't know for sure what exactly is going on inside those operators. As a result, we don't know if those const qualifiers are being dropped.

hanst99 wrote:
Also, you "corrected" him into removing the const specifier from the argument, which actually belongs there because adding a const object should be legal with any sane use of the + operator. (sic)

I wasn't referring that the const qualifier within the parameter list, but it appears as though he changed it anyway.

Wazzak
Topic archived. No new replies allowed.