Function templates: So I created a function template......

And it works! :) The problem Im having right now is operator overloading on the second part of the code. My teacher wrote what to do but Im not understanding. Its already past due so Im not in a rush. I rather learn it slowly than quickly finishing something without understanding it. Dont help me on the last part. Im pretty sure I can handle that :)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>

using namespace std;

template <class C>

C getMaxTemplate(C list[], C size){
    C result = list[0];

	for (int i = 0 ; i < size ; i ++)
	{
		if (list[i] > result)
			result = list[i];
	}

	return result;
    
    
}
// (a) write a function template to return the max element in a given list
// hint: the list will have elements of generic type
//...

class studentType
{
private:
	string name;
	double gpa;
public:
	studentType(string n = "default name", double g = -1.0)
	{
		name = n;
		gpa = g;
	}

	// (b) overload the > operator as a member function. 
	// HINT: a student is > from another student if his/her gpa is higher
	//...

	string getName() 
	{
		return name;
	}

	double getGpa()
	{
		return gpa;
	}

};


int main()
{
	int numbers[6] = {11, 33, 444, 55, 66 -100};
	studentType students[3] = {studentType("john", 3.01), 
		studentType("alice", 3.9), 
		studentType("liz", 2.5)};

	cout << getMaxTemplate(numbers, 3) << endl;

	// (c) call the getMaxTemplate function using students list 
	// and print the name and gpa of the student with the highest gpa
	//...
	
 

	
	return 0;
}
1
2
3
4
5
6
class studentType{
public:
   bool operator>(const studentType &b) const{
       //compare *this and b
   }
};
So I tried using the word "this" in the code but it keeps saying Error expression must have a class type.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 #include <iostream>
#include <string>

using namespace std;

template <class C>

C getMaxTemplate(C list[], C size){
    C result = list[0];

	for (int i = 0 ; i < size ; i ++)
	{
		if (list[i] > result)
			result = list[i];
	}

	return result;
    
    
}
// (a) write a function template to return the max element in a given list
// hint: the list will have elements of generic type
//...

class studentType  
{
private:
	string name;
	double gpa;
public:
	studentType(string n = "default name", double gpa = -1.0)
	{
		name = n;
		this.gpa = gpa;
	}

	// (b) overload the > operator as a member function. 
	// HINT: a student is > from another student if his/her gpa is higher
	//...
	
	bool operator>(studentType& s2)
	{
	    // we have here:
	    // name (also called: this.name
	    // gpa (also called: this.gpa
	    // s2.name
	    // s2.gpa
	    
	    if (this.gpa>s2.gpa)
	        return true;
	    else
	        return false;
	}

	string getName() 
	{
		return name;
	}

	double getGpa()
	{
		return gpa;
	}

};


int main()
{
	int numbers[6] = {11, 33, 444, 55, 66 -100};
	studentType students[3] = {studentType("john", 3.01), 
		studentType("alice", 3.9), 
		studentType("liz", 2.5)};

	cout << getMaxTemplate(numbers, 3) << endl;
	
	
	
	// (c) call the getMaxTemplate function using students list 
	// and print the name and gpa of the student with the highest gpa
	//...
	
 

	
	return 0;
}
Last edited on
this is a pointer in C++ (a mistake that was made years ago) so you need to write this->gpa instead of this.gpa

Or, don't write this at all and just write gpa
Last edited on
OMG!! Thank you. What a trip this was.
Topic archived. No new replies allowed.