should friend functions be represented in UML diagrams? - c++

Also, how exactly are overloaded operator member functions best formatted in a UML diagram?
Here is my class:
class matrix
{
friend ostream& operator << (ostream&, const matrix&);
friend bool operator == (const matrix &, const matrix &);
friend matrix operator - (const matrix &, const matrix &);
private:
int size;
int range;
int array[10][10];
public:
matrix(int);
matrix(int, int);
bool operator != (const matrix &) const;
matrix operator + (const matrix &) const;
const matrix & operator = (const matrix &);
};
and here is what I have of my UML diagram so far:

By placing the stereotype <<friend>> in front of the operation in the UML class diagram.
You will have to do it this way:
<<friend>> ostream& operator << (ostream&, const matrix&)
<<friend>> bool operator == (const matrix &, const matrix &)
<<friend>> matrix operator - (const matrix &, const matrix &)

Related

Is it possible to implement move operator+/operator-/operator* for class of Matrix?

I am thinking on how can i define a class of real matrices NxN with the operations Add (Subtract) and Multiply. I am looking for Efficient Memory Usage.
class Matrix {
private:
std::size_t _size_n;
double **_pMatrix;
public:
Matrix(const size_t n);
~Matrix();
double &operator()(size_t, const size_t);
double operator()(size_t, const size_t) const;
size_t size_n() const { return _size_n; }
};
std::ostream &operator<<(std::ostream &, const Matrix &);
Matrix operator+(const Matrix&, const Matrix&);
Matrix operator-(const Matrix&, const Matrix&);
Matrix operator*(const Matrix&, const Matrix&);
Yes you can have additional overloads
Matrix/*&&*/ operator+(const Matrix&, Matrix&&);
Matrix/*&&*/ operator+(Matrix&&, const Matrix&);
Matrix/*&&*/ operator+(Matrix&&, Matrix&&);
To reuse memory of one of the temporary.
They can all be implemented with Matrix& operator += (Matrix&, const Matrix&)
by changing the order as + is symmetrical. operator - would require dedicated code.
Another way to optimize memory is to use expression templates instead of computing directly the result.
It has its drawback about lifetime issue (especially with auto) though.

invalid initialization of reference of type for class WholeNumbers

I am trying to write out a class called WholeNumber. I am not finished writing it, I like to try to compile frequently so I can keep the errors low and manageable. When I go to compile the class as it is I get this error:
fibonacci.cpp: In member function ‘std::__cxx11::list& WholeNumber::operator=(WholeNumber&)’:
fibonacci.cpp:41:14: error: invalid initialization of reference of type ‘std::__cxx11::list&’ from expression of type ‘WholeNumber’
return *this;
I've tried looking up the error to see if I could find the answers to my problem, but I could only find that you can't initialize a non-const reference from a const reference. I don't understand what that could be referring to my code. What am I missing?
#include <iostream>
#include "fibonacci.h" // for fibonacci() prototype
#include <list>
#include <vector>
using namespace std;
class WholeNumber
{
private:
list <int> digits;
public:
//default constructor
WholeNumber() { }
//non-default constructor
WholeNumber(unsigned int number) {number = 0;}
//copy constructor
WholeNumber(const WholeNumber & rhs) { }
//destructor
~WholeNumber() { }
friend ostream & operator << (ostream & out, const list <int> * l);
istream & operator >> (istream & in);
WholeNumber & operator +=(const WholeNumber & rhs);
list <int> & operator = (WholeNumber & rhs)
{
this->digits = rhs.digits;
return *this;
}
};
The returned type of the copy assignment operator is list <int> &
list <int> & operator = (WholeNumber & rhs)
{
this->digits = rhs.digits;
return *this;
}
but an object of the type WholeNumber (*this) is returned and there is no implicit conversion from one type to another.
Maybe you mean the following
WholeNumber & operator = ( const WholeNumber & rhs )
{
this->digits = rhs.digits;
return *this;
}
Also these operators
friend ostream & operator << (ostream & out, const list <int> * l);
istream & operator >> (istream & in);
are invalid. For example the first operator should be declared like
friend ostream & operator << (ostream & out, const WholeNumber & );
And the second operator also should be a friend function like
friend istream & operator >> ( istream & in, WholeNumber & );

friend operator confusion

I'm new to using .h/.cpp files and I'm trying to convert my .cpp to .h and .cpp and I'm running into this issue. It's erroring and I'm not sure how to resolve it. In Primitives.h I have:
friend Matrix operator+(const Matrix&, const Matrix&);
and in Primitives.cpp I had:
friend Matrix operator+ (const double b, const Matrix& a)
But thus far my attempts to change the format have not worked. Any tips?
You should do it like this:
// header
class Matrix
{
public:
friend Matrix operator+(const Matrix&, const Matrix&);
};
// source
Matrix operator+(const Matrix&, const Matrix&) { ... }
For short operators it may be more convenient to define the operator directly in the header file:
// header
class Matrix
{
public:
friend Matrix operator+(const Matrix&, const Matrix&) {
// code here
}
};
You should not use friend in implementation (.cpp file). You should use it at declaration (in .h file) only.
Try this
///your.h
class Matrix
{
//your class
friend Matrix operator+ (const double b, const Matrix& a);
};
////your.cpp
Matrix operator+ (const double b, const Matrix& a)
{
//your code
return yourobject;
}

Object as array index

I have the following class:
class MyInteger
{
private:
__int64 numero;
static __int64 int64Pow(__int64, __int64);
public:
// It doesn't matter how these methods are implemented
friend class MyInteger;
MyInteger(void);
MyInteger(const MyInteger&);
MyInteger(const __int64&);
~MyInteger(void);
static MyInteger const minValue;
static MyInteger const maxValue;
MyInteger& operator = (const MyInteger&);
MyInteger operator + (const MyInteger&) const;
MyInteger operator - (const MyInteger&) const;
MyInteger operator * (const MyInteger&) const;
MyInteger operator / (const MyInteger&) const;
MyInteger& operator += (const MyInteger&);
MyInteger& operator -= (const MyInteger&);
MyInteger& operator *= (const MyInteger&);
MyInteger& operator /= (const MyInteger&);
MyInteger operator % (const MyInteger&) const;
MyInteger& operator %= (const MyInteger&);
MyInteger& operator ++ ();
MyInteger operator ++ (int);
MyInteger& operator -- ();
MyInteger operator -- (int);
bool operator == (const MyInteger&) const;
bool operator != (const MyInteger&) const;
bool operator > (const MyInteger&) const;
bool operator < (const MyInteger&) const;
bool operator >= (const MyInteger&) const;
bool operator <= (const MyInteger&) const;
int toStdInt() const
{
return (int)numero;
}
float toStdFloat() const;
double toStdDouble() const;
char toStdChar() const;
short toStdShortInt() const;
long toStdLong() const;
long long toStdLongLong() const;
unsigned int toStdUInt() const;
__int64 toStdInt64() const;
unsigned __int64 toStdUInt64() const;
unsigned long long int toStdULongLong() const;
long double toStdULongDouble() const;
template<class Type>
Type& operator[](Type* sz)
{
return sz[toStdULongLong()];
}
};
template<class Type>
Type* operator+(const Type* o1, const MyInteger& o2)
{
return ((o1) + (o2.toStdInt()));
}
I'd like to use this class to access array elements like this:
MyInteger myInt(1);
int* intPtr = (int*)malloc(sizeof(int) * N);
intPtr[myInt] = 1;
I thought that the function
template<class Type>
Type* operator+(const Type* o1, const MyInteger& o2)
{
return ((o1) + (o2.toStdInt()));
}
could solve my problem, because as this post reports (Type of array index in C++) "The expression E1[E2] is identical (by definition) to *((E1)+(E2))", but I get the C2677 error ('[' operator: no global operator found which takes type 'MyInteger' (or there is no acceptable conversion))
Can someone clarify me this situation?
Thanks
You may be able to do that by overriding the cast to int of your MyInteger class in a way similar to:
class MyInteger {
...
operator int() const
{
return toStdInt(); /** Your class as an array index (int) */
}
...
}

c++ template and its element type

This is my template matrix class:
template<typename T>
class Matrix
{
public:
....
Matrix<T> operator / (const T &num);
}
However, in my Pixel class, I didn't define the Pixel/Pixel operator at all!
Why in this case, the compiler still compiles?
Pixel class
#ifndef MYRGB_H
#define MYRGB_H
#include <iostream>
using namespace std;
class Pixel
{
public:
// Constructors
Pixel();
Pixel(const int r, const int g, const int b);
Pixel(const Pixel &value);
~Pixel();
// Assignment operator
const Pixel& operator = (const Pixel &value);
// Logical operator
bool operator == (const Pixel &value);
bool operator != (const Pixel &value);
// Calculation operators
Pixel operator + (const Pixel &value);
Pixel operator - (const Pixel &value);
Pixel operator * (const Pixel &value);
Pixel operator * (const int &num);
Pixel operator / (const int &num);
// IO-stream operators
friend istream &operator >> (istream& input, Pixel &value);
friend ostream &operator << (ostream& output, const Pixel &value);
private:
int red;
int green;
int blue;
};
#endif
C++ templates are instantiated at the point you use them, and this happens for the Matrix<T>::operator/(const T&), too. This means the compiler will allow Matrix<Pixel>, unless you ever invoke the division operator.
1) You didn't provide body of Matrix operator, so it is possible that Pixel/Pixel operator isn't needed.
2) Afaik, template methods do not generate compile errors unless you call them somewhere in your code. NO idea if this is standard or not, but some versions of MSVC behave this way. Do
Matrix m;
Pixel p;
m = m/p
somewhere in your code, and see what happens.