Function in a Class Template

I have this class templates And This UML.

I have to write this function
+operator=(source: Array<ElemType, SIZE>): Array<ElemType, SIZE> but I do not know how to start the declaration / or start the function.
NOTE: I have to return a template but I do not know how to do it, Can somebody show me how to start it.


UML
1
2
3
4
5
6
7
8
9
Array<ElemType, SIZE>
-elements: ElemType[SIZE]
+Array()
+Array(source: Array<ElemType, SIZE>)
+operator=(source: Array<ElemType, SIZE>): Array<ElemType, SIZE>
+operator==(other: Array<ElemType, SIZE>): Boolean
+operator!=(other: Array<ElemType, SIZE>): Boolean
<<L-value>>+operator[](index: Integer): ElemType
<<R-value>>+operator[](index: Integer): ElemType



CLASS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef CLASS_TEMPLATE_H
#define CLASS_TEMPLATE_H
#include <iostream>
using namespace std;

namespace JuanSanchez
{
	// This is the template of the Class Array
	template<typename T = double, int SIZE = 5> 
	class Array
	{
	   public:
          // Constructor of the array class
		  Array();
	   private:
		  T elements[SIZE]; 
	};

	// declaration constructor class array with a template that has no parameter
	template<> class Array<> { public: Array(); };

	// declaration constructor class array with a template that has two parameters
	template<> class Array<double, 10> { public: Array(); };

	// Constructor with template non-type argument or the typename is not the same as what
	// is declared.
	template<typename T, int SIZE> 
	Array<T, SIZE>::Array()
	{
		T elements[SIZE] = {0};
        cout << "Template with non-type argument or typename is not same as what is declared  " << elements[4] << endl;
	}

	// Constructor with Explicit specialization default arguments
	Array<>::Array() 
	{
	   cout << "Explicit specialization default arguments" << endl;
	}

	// constructor with Explicit specialization 
	Array<double, 10>::Array() 
	{
	   cout << "Explicit specialization <double, 10>" << endl;
	}

	

}
#endif 
There are two ways to do it:
1. Make a friend function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<typename T = double, int SIZE = 5> 
class Array
{
    public:
        Array();
        friend Array<T,SIZE> operator+ (Array<T,SIZE> lhs, Array<T,SIZE> rhs);
    private:
        T elements[SIZE]; 
};

template <typename T, int SIZE>
Array<T,SIZE> operator+ ( Array<T,SIZE> lhs, Array<T,SIZE> rhs )
{
    Array<T,SIZE> result;
    for (int i = 0; i < SIZE; ++i)
        result.elements[i] = lhs.elements[i] + rhs.elements[i];
    return result;
}


2. Make a member function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<typename T = double, int SIZE = 5>
class Array
{
    public:
        Array();
        Array<T,SIZE> operator+ (Array<T,SIZE> rhs);
    private:
        T elements[SIZE];
};

template<typename T, int SIZE>
Array<T,SIZE> Array<T,SIZE>::operator+(Array<T,SIZE> rhs)
{
    Array<T,SIZE> result;
    for (int i = 0; i < SIZE; ++i)
        result.elements[i] = this->elements[i] + rhs.elements[i];
    return result;
}
Whoops, wrong operator!

Okay, just do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template<typename T = double, int SIZE = 5>
class Array
{
    public:
        Array();
        Array<T,SIZE> operator=(Array<T,SIZE> rhs);
    private:
        T elements[SIZE];
};

template<typename T, int SIZE>
Array<T,SIZE> Array<T,SIZE>::operator=(Array<T,SIZE> rhs)
{
    for (int i = 0; i < SIZE; ++i)
        this->elements[i] = rhs.elements[i];
    return *this;
}
Last edited on
I've never actually done UML conversions before so this is interesting. This is what I got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template<typename ElemType = double, int SIZE = 5>
class Array
{
private:
    ElemType elements[SIZE];
public:
    Array()
    {
        for (int i = 0; i < SIZE; ++i) elements[i] = ElemType(0);
    }
    Array(Array<ElemType,SIZE>& source)
    {
        for (int i = 0; i < SIZE; ++i) elements[i] = source.elements[i];
    }
    Array<ElemType,SIZE>& operator=(Array<ElemType,SIZE>& rhs)
    {
        for (int i = 0; i < SIZE; ++i) elements[i] = rhs.elements[i];
        return *this;
    }
    bool operator==(Array<ElemType,SIZE>& rhs)
    {
        for (int i = 0; i < SIZE; ++i)
            if (elements[i] != rhs.elements[i])
                return false;
        return true;
    }
    bool operator!=(Array<ElemType,SIZE>& rhs)
    {
        return !(*this==rhs);
    }
    ElemType& operator[](int index)
    {
        return elements[index];
    }
};
@Stewbond: You need to observe const-correctness.
Topic archived. No new replies allowed.