Skip to main content
I've modified the question a bit.
Source Link

I know this ismay be a silly question, but... it is it really necessary to add the name of the parameter name (int u<parameter_name>), instead of just (int) in a function definition, in of an Operatoroperator function to overload “++”"++" ( post-increment)  ? or would it be optional? example: ejemplo

example:

I know this is a silly question, but... it is really necessary to add the name of the parameter (int u), instead of just (int) in a function definition, in an Operator function to overload “++” ( post-increment)? or would it be optional? example: ejemplo:

I know this may be a silly question, but... is it really necessary to add the parameter name (int <parameter_name>), instead of just (int) in a definition of an operator function to overload "++" ( post-increment)  ? or would it be optional?

example:

Source Link

Syntax to Overload the Post-Increment Operator ++ as a Nonmember Function

I know this is a silly question, but... it is really necessary to add the name of the parameter (int u), instead of just (int) in a function definition, in an Operator function to overload “++” ( post-increment)? or would it be optional? example: ejemplo:

/* function prototype */
friend Cylinder operator++(Cylinder&, int);

/* function definition */
Cylinder operator++(Cylinder& n, int u)
{
    Cylinder temp = n;

    (n.radius)++;
    (n.height)++;

    return (temp);
}

and here it is the same except that it does not have the name of the parameter in the definition function (I only ask, because I am curious, that even though it does not have it, the program still works, and also if it would be good practice to put it or not?):

Example:

/* function prototype */
friend Cylinder operator++(Cylinder&, int);

/* function definition */
Cylinder operator++(Cylinder& n, int)
{
    Cylinder temp = n;

    (n.radius)++;
    (n.height)++;

    return (temp);
}

Full program: main.cpp

#include <iostream>
#include <cstdlib>

#include "Cylinder.h"

using namespace std;


int
main(void)
{
    Cylinder Cylinder1(5.0, 10.0); //radius = 5.0 and height = 10.0
    Cylinder Cylinder2(3.5, 20.0); //radius = 3.5 and height 20.0
    Cylinder Cylinder3;
    Cylinder Cylinder4;
    Cylinder Cylinder5;
    
    cout << "Cylinder1 --- " << Cylinder1 << endl;
    cout << "Cylinder2 --- " << Cylinder2 << endl;


    Cylinder3 = Cylinder1 + Cylinder2;
    cout << "Cylinder1 + Cylinder2 = " << "Cylinder3 --- " << Cylinder3 << endl;

    Cylinder4 = Cylinder1 - Cylinder2;
    cout << "Cylinder1 - Cylinder2 = " << "Cylinder4 --- " << Cylinder4 << endl;

    Cylinder5 = Cylinder1 * Cylinder2;
    cout << "Cylinder1 * Cylinder2 = " << "Cylinder5 --- " << Cylinder5 << endl;

    cout << "Compare Cylinder1 > Cylinder2 --- " << endl;
    if (Cylinder1 > Cylinder2)
        cout << "Cylinder1 is greater than Cylinder2." << endl;
    else
        cout << "Cylinder1 is less than or equal to Cylinder2." << endl << endl;
    
    Cylinder1++;
    cout << "Cylinder1++ --- " << endl;
    cout << "New dimension of Cylinder1: " << Cylinder1 << endl;

    Cylinder4 = Cylinder3++;
    cout << "Cylinder4 = Cylinder3++ --- " << endl;
    cout << "New dimension of Cylinder3: " << Cylinder3 << endl;
    cout << "New dimension of Cylinder4: " << Cylinder4 << endl;

    cout << "Cylinder4 != Cylinder3 --- Cylinder4 is " 
         << (Cylinder4 != Cylinder3 ? "not equal to Cylinder3 " : "equal to Cylinder3") << endl;
    cout << "Cylinder4 >= Cylinder3 --- Cylinder4 is " 
         << (Cylinder4 >= Cylinder3 ? "greater or equal to Cylinder3 " : "less than Cylinder3 ") << endl;
    cout << "Cylinder4 == Cylinder3 --- Cylinder4 is " 
         << (Cylinder4 == Cylinder3 ? "equal to Cylinder3 " : "not equal to Cylinder3") << endl;
    cout << "Cylinder4 < Cylinder3 --- Cylinder4 is " 
         << (Cylinder4 < Cylinder3 ? "less than Cylinder3 " : "greater than or equal to Cylinder3") << endl;
    cout << "Cylinder4 <= Cylinder3 --- Cylinder4 is " 
         << (Cylinder4 <= Cylinder3 ? "less than or equal to Cylinder3 " : "greater than Cylinder3") << endl;
    exit(EXIT_SUCCESS);
}

Cylinder.cpp

#include <iostream>

#include "Cylinder.h"


Cylinder::Cylinder(void)
{
    this->radius = 1;
    this->height = 1;
}


Cylinder::Cylinder(double _radius, double _height)
{
    this->radius = _radius;
    this->height = _height;
}


const Cylinder&
Cylinder::operator=(const Cylinder &n)
{   
    this->radius = n.radius;
    this->height = n.height;

    return (*this);
}

Cylinder
Cylinder::operator+(const Cylinder& n) const
{
    Cylinder new_n;

    new_n.radius = radius + n.radius;
    new_n.height = height + n.height;
    
    return (new_n);
}


Cylinder
Cylinder::operator-(const Cylinder& n) const
{
    Cylinder new_n;

    new_n.radius = radius - n.radius;
    new_n.height = height - n.height;

    return (new_n);
}


Cylinder
Cylinder::operator*(const Cylinder& n) const
{
    Cylinder new_n;

    new_n.radius = radius * n.radius;
    new_n.height = height * n.height;

    return (new_n);
}


bool
Cylinder::operator==(const Cylinder& n) const
{
    Cylinder new_n;

    return ((new_n.radius == n.radius && 
         new_n.height == n.height)  ? true : false);
}


bool
Cylinder::operator!=(const Cylinder& n) const
{
    Cylinder new_n;

    return ((new_n.radius != n.radius && 
         new_n.height != n.height)  ? true : false);
}


bool
Cylinder::operator>=(const Cylinder& n) const
{
    Cylinder new_n;

    return ((new_n.radius >= n.radius && 
         new_n.height >= n.height)  ? true : false);
}


bool
Cylinder::operator>(const Cylinder& n) const
{
    Cylinder new_n;

    return ((new_n.radius > n.radius &&
         new_n.height > n.height)  ? true : false);
}


bool
Cylinder::operator<(const Cylinder& n) const
{
    Cylinder new_n;

    return ((new_n.radius < n.radius &&
         new_n.height < n.height)  ? true : false);
}


bool
Cylinder::operator<=(const Cylinder& n) const
{
    Cylinder new_n;

    return ((new_n.radius <= n.radius &&
         new_n.height <= n.height)  ? true : false);
}


Cylinder
operator++(Cylinder& n, int)
{
    Cylinder temp = n;

    (n.radius)++;
    (n.height)++;

    return (temp);
}

ostream &operator<<(ostream& out, const Cylinder& n)
{
    out << "Radius: " << n.radius << '\t'
        << "Height: " << n.height << std::endl; 
    return (out);
}

Cylinder.h

#ifndef CYLINDER_H_INCLUDED
#define CYLINDER_H_INCLUDED

using namespace std;


class Cylinder {

    public:
        /* function members */
        Cylinder(void);
        Cylinder(double, double);
        
        const Cylinder& operator=(const Cylinder &);

        Cylinder operator+(const Cylinder&) const;
        Cylinder operator-(const Cylinder&) const;
        Cylinder operator*(const Cylinder&) const;

        bool operator==(const Cylinder&) const;
        bool operator!=(const Cylinder&) const;
        bool operator>=(const Cylinder&) const;
        bool operator>(const Cylinder&) const;
        bool operator<(const Cylinder&) const;
        bool operator<=(const Cylinder&) const;

        friend Cylinder operator++(Cylinder&, int);
        friend ostream &operator<<(ostream& out, const Cylinder&);
        
    private:
        /* data members */
        double radius;
        double height;
};

#endif  /* CYLINDER_H_INCLUDED */