max/min of an array of OBJECTS

I am writing a code using class ( objects ) which will display a menu consisting of 6 options like :

1. create an array of "n" elements objects
2. display of the array
3. save information in a FILE ( using OOP)
4. load information
5. find the maximum element & find the minimum element
6. find the sum of the elements


I find it difficult to adjust the method finding the max and min for objects
, your reference and advice will be high appreciated,

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159



class array
{
	public:
		void setN(){cout<<"Enter N ::: "; cin>>this->N;}
		
		void setV(){cout<<"Enter value ::: "; cin>>value;}
		void setVA(int value){ this->value=value;}
		
		const int getV()const{return this->value;}
		const int getN()const{return this->N;}
		
		array(int value=NULL){this->value=value; this->N=NULL;}
		array(int value, int N){this->value=value;}
		
		array(const array&Obj){this->value=Obj.value; this->N=Obj.N;}
		
	~array(){}
	
		friend ostream &operator<<(ostream&out, array&Obj);
		
	    friend void create(array&);
	    
	    friend void display(array&);
	    
	    friend void save(array&);
	    
	    friend int menu(array&);
	    
	    friend int max(array&);
	    
	    friend int min(array&);
	    
	    friend int sum(array&);
	   
	    
	protected:
		
	int value;
	int N;
};

		 ostream &operator<<(ostream&out, array&Obj)
		{
			out<<"Array ="<<Obj.value;
			return out;
		}
		

 
		void create(array&X)
		{

         for(int i=0; i<X.getN(); i++)
          {
 	       X.setV();
          }
		}
		
		
		void display(array&X)
		{

         for(int i=0; i<X.getN(); i++)
         {
 	       cout<<"\n A "<<"["<<i<<"]"<<X.getV();
         }
		}
        
        int max( array&X )
        {
        	
        int max=INT_MIN;
        	
        	  for(int i=1; i<X.getN(); i++)
         {
 	        if( max < (X.getV) max=(X.getV);
         }
         return max;
		}
        /*
        void save(array&X)
        {
          string FN;
  
  cout<<" Enter File name = "; cin >> FN;

  FN+=".txt";
  
  fstream F;
  
   F.open(FN.c_str(),  ios::out );
   
   if( F.fail() ) cerr<<" Error Fstream FILE "<<endl; 
   else cout<<" OK Fstream FILE "<<endl;
   
   F >> display(X); // not working

   F.close();	
		}*/
int menu(array&A)
{
	int n=1;
	system("cls");
	
	cout<<" \n\n\t\t ***** MENU FILE MANAGEMENT *****"<<endl;
	cout<<" \t\t *                             *"<<endl;
 	cout<<" \t\t *     1. Create array of objects        *"<<endl;
 	cout<<" \t\t *     2. Display array of objects      *"<<endl;
 	cout<<" \t\t *     3. Save file  *"<<endl;
 	cout<<" \t\t *     4. Load file     *"<<endl;
 	cout<<" \t\t *     5. Sum of array     *"<<endl;
 	cout<<" \t\t *     6. Maximum value     *"<<endl;
  	cout<<" \t\t *     ESC. Back               *"<<endl;
  	cout<<" \t\t *                             *"<<endl;
  	cout<<" \t\t *******************************"<<endl;
  		switch(_getch())
 	{
 	   case 49: system("cls"); create(A); system("pause"); 
	   case 50: system("cls"); display(A); system("pause"); 

	   case 27: n=1;
	}
	  return 0;
 }
 		
int main()
{

arrayA[10];


A[0].setN();
menu(*A);
/*
create(*A);
display(*A);
save(*A);

   
 /*  
should be used further for load method. 

   F.open("2021.txt", ios::in  );
     
   if( F.fail() ) cerr<<" Error Fstream FILE "<<endl; 
   else cout<<" OK Fstream FILE "<<endl;
   
   
   F << ;
   F.close();
*/
 
 
 
	return 0;
}
Last edited on
Your class array contains two int members. Two integers is not what C++ calls "array".


C++ Standard Library offers easy method to locate min and max elements from range:
http://www.cplusplus.com/reference/algorithm/minmax_element/
Last edited on
create an array of "n" elements objects


Based upon using an array of objects - as opposed to an object that holds an array - then consider for a starter:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <fstream>
#include <iostream>
using namespace std;

class Array {
public:
	Array(int val = 0) : value(val) {}
	Array(const Array& Obj) : value(Obj.value) {}

	void setV() { cout << "Enter value: "; cin >> value; }
	void setVA(int val) { value = val; }

	const int getV() const { return value; }

protected:
	int value {};
};

ostream& operator<<(ostream& out, const Array& Obj)
{
	return out << "Array =" << Obj.getV();
}

void create(Array X[], int n)
{
	for (int i = 0; i < n; ++i)
		X[i].setV();
}

void display(const Array X[], int n)
{
	for (int i = 0; i < n; ++i)
		cout << "\nA " << "[" << i << "]" << X[i].getV();
}

int max(const Array X[], int n)
{
	int max = X[0].getV();

	for (int i = 1; i < n; ++i)
		if (max < X[i].getV())
			max = X[i].getV();

		return max;
}

int sum(const Array X[], int n)
{
	int sum {};

	for (int i = 0; i < n; ++i)
		sum += X[i].getV();

	return sum;
}

void menu(Array A[], int n)
{
	//system("cls");
	int opt {};

	do {
		cout << " \n\n\t\t ******* MENU FILE MANAGEMENT *******\n";
		cout << " \t\t *                                  *\n ";
		cout << " \t\t *     1. Create array of objects   *\n";
		cout << " \t\t *     2. Display array of objects  *\n";
		cout << " \t\t *     3. Save file                 *\n";
		cout << " \t\t *     4. Load file                 *\n";
		cout << " \t\t *     5. Sum of array              *\n";
		cout << " \t\t *     6. Maximum value             *\n";
		cout << " \t\t *     0. Exit                      *\n";
		cout << " \t\t *                                  *\n";
		cout << " \t\t ************************************\n";
		cout << " \t\t Enter option number: ";

		cin >> opt;

		switch (opt)
		{
			case 0:
				break;

			case 1:
				//system("cls");
				create(A, n);
				//system("pause");
				break;

			case 2:
				//system("cls");
				display(A, n);
				//system("pause");
				break;

			case 5:
				cout << "The sum of values is " << sum(A, n) << '\n';
				break;

			case 6:
				cout << "The maximum value is " << max(A, n) << '\n';
				break;

			default:
				cout << "Invalid option\n";
		}
	} while (opt != 0);
}

int main()
{
	int N {};

	cout << "Enter N: ";
	cin >> N;

	Array A[10];

	menu(A, N);
}

@seeplus, thank you so much indeed!
@seeplus,

hope you can help me again :)

so my functions for saving and loading the FILE are as below :
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
void save(const Array X[], int n){
	
  /*string FN;
  
  cout<<" Enter File name = "; cin >> FN;

  FN+=".txt";
  */
 
  F.open("deep.txt",  ios::out );
   
   if( F.fail() ) cerr<<" Error Fstream FILE "<<endl; 
   else cout<<" OK Fstream FILE "<<endl;
   
   	for (int i = 0; i < n; i++)
		F <<' '<< X[i].getV();
   
   F.close();
    
		}
		
void load(const Array X[], int n)
{

   F.open("deep.txt", ios::in );
  
     if( F.fail() ) cerr<<" Error Fstream FILE "<<endl; 
   else cout<<" OK Fstream FILE "<<endl;
   
  	for (int i = 0; i < n; i++)
		F <<' '<< X[i].getV();
		 
  F.close();
}

It works fine for saving, however when loading it says OK Fstream FILE but I also expect to press on option 2 which is "Display..." and see values which were previous saved into the "deep.txt" FILE, yet values are "0";
In load,
 
F <<' '<< X[i].getV();


.getV(), just returns the value - it doesn't set it. You need to use .setVA(). Also you use >> for stream extraction, not << which is for stream insertion. Something like (not tried):

1
2
3
4
5
6
for (int i = 0; i < n; ++i) {
    int val {};

    F >> val;
    X[i].setVA(val);
}

@seeplus, I still can't handle it

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <fstream>
#include <iostream>
using namespace std;

fstream F;


class Array {
	
public:
	
	void setV() { cout << "Enter value ::: "; cin >> value; }
	
	void setVA(int value) { this->value = value; }

	const int getV() const { return this->value; }
	
	Array(int value = NULL) : value(value) {}
	
	Array(const Array& Obj) : value(Obj.value) {}
	
    ~Array(){}
    
    friend ostream &operator<<(ostream&out, Array&Obj);
    
    friend void display(const Array X[], int n);
    
    friend void create(Array X[], int n);
    
    friend void load(const Array X[], int n);
    
    friend void save (const Array X[], int n);
    
protected:
	
	int value;
	int max(const Array X[], int n)
{
	int max = X[0].getV();

	for (int i = 1; i < n; i++)
		if (max < X[i].getV())
			max = X[i].getV();

		return max;
}

int min(const Array X[], int n)
{
	int min = X[0].getV();

	for (int i = 1; i < n; i++)
		if (min > X[i].getV())
			min = X[i].getV();

		return min;
}


int sum(const Array X[], int n)
{
	int sum = NULL;

	for (int i = 0; i < n; ++i)
		sum += X[i].getV();

	return sum;
}
public: 

void menu(Array A[], int n)
{
	system("cls");
	int option;

	do {
		cout << " \n\n\t\t ******* MENU FILE MANAGEMENT *******\n";
		cout << " \t\t *                                  *\n ";
		cout << " \t\t *     1. Create array of objects   *\n";
		cout << " \t\t *     2. Display array of objects  *\n";
		cout << " \t\t *     3. Save file                 *\n";
		cout << " \t\t *     4. Load file                 *\n";
		cout << " \t\t *     5. Sum of array              *\n";
		cout << " \t\t *     6. Maximum value             *\n";
		cout << " \t\t *     7. Minimum value             *\n";
		cout << " \t\t *     0. Exit                      *\n";
		cout << " \t\t *                                  *\n";
		cout << " \t\t ************************************\n";
		cout << " \n\n\t\t\t Enter option number: ";

		cin >> option;

		switch (option)
		{
			case 0: exit(0);

			case 1: system("cls"); create(A, n); system("pause"); break;

			case 2:	system("cls"); display(A, n); system("pause"); break;
			
			case 3:	system("cls"); save(A, n); system("pause"); break;
			
			case 4:	system("cls"); load(A, n); system("pause"); break;

			case 5: system("cls"); cout << "The sum of values is " << sum(A, n) << '\n'; break;

			case 6: system("cls"); cout << "The maximum value is " << max(A, n) << '\n'; break;
			
			case 7: system("cls"); cout << "The minimum value is " << min(A, n) << '\n'; break;

			default: cout << "Invalid option\n";
		}
	} while (option != 0);
}

};

ostream& operator<<(ostream& out, const Array& Obj)
{
	return out << "Array =" << Obj.getV();
}

istream& operator<<(istream& in, Array& Obj)
{
	cout<< "Enter Array =";
	return in  >> Obj.value;
}


void display(const Array X[], int n)
{
	for (int i = 0; i < n; i++)
		cout << "\nA " << "[" << i << "]" << X[i].getV();
}


void create(Array X[], int n)
{
	
	for (int i = 0; i < n; i++)
		X[i].setV();
}




void save(const Array X[], int n){
 
  F.open("deep.txt",  ios::out );
   
   if( F.fail() ) cerr<<" Error Fstream FILE "<<endl; 
   else cout<<" OK Fstream FILE "<<endl;
   
   	for (int i = 0; i < n; i++)
		F <<' '<< X[i].getV();
   
   F.close();
    
		}
		
void load(const Array X[], int n)
{

   F.open("deep.txt", ios::in );
  
     if( F.fail() ) cerr<<" Error Fstream FILE "<<endl; 
   else cout<<" OK Fstream FILE "<<endl;
   
	for (int i = 0; i < n; i++)
{
	int val[10];
    F >> val;
    X[i].setVA(val);	}
    
     F.close();
}
		 
  F.close();
}

int main()
{
	int N; 
	
	cout << "Enter number of the elements ::: "; cin >> N;
	
	Array A[10],menu;

	menu.menu(A, N);
}


I am trying to overload >> operator as follows
1
2
3
4
5
istream& operator<<(istream& in, Array& Obj)
{
	cout<< "Enter Array =";
	return in  >> Obj.value;
}


but when i run the code it says that value is protected/private and doesn't have access.
How on earth it can't have access since it is a friend function
istream should overload operator>> not operator<< which is for ostream
1
2
3
4
5
istream& operator>>(istream& in, Array& Obj)
{
	cout<< "Enter Array =";
	return in  >> Obj.value;
}


my bad, miswritten, but that error still occurs saying ERROR int value is protected and ERROR within context "Obj. [error] value;"
Because operator>> is a standalone function and tries to access the protected members of class Array. Only members of Array can do that (and classes derived from Array).

Does Array have a public member that writes to Array::value? Yes. Use it:
1
2
3
4
5
6
7
istream& operator>>( istream& in, Array& Obj )
{
  cout<< "Enter Array =";
  int x {};
  if ( in >> x ) Obj.setVA( x );
  return in;
}


Note: You have two standalone functions:
1
2
ostream& operator<< ( ostream&, const Array& );
istream& operator>> ( istream&, Array& );

One of them is declared friend of class Array. What does that mean?
@keskiverto , I was trying to access private/protected zone using a friend function calling it from main, in my specific case overloading the << and >> operators...
thanks for help and explanation guys
Topic archived. No new replies allowed.