counting class objects

Hi.I need help with my code . well i'm required to :

Add to the class EECE230Student a private static variable which is used to count the number of objects generated. For this purpose, you need to perform the following:

a. Declare and initialize the variable and add a reader to read this variable.

b. Modify the constructors as needed and add a destructor.

c. Add also a copy constructor which uses a call by reference.



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
  #include <iostream>
using namespace std;

class EECE230Student  {

	int id;
	int section;
	int quiz1;
	int quiz2;
	int finalExam;
	static int count;

public:

	EECE230Student();
	EECE230Student(int id, int section);

	void set(static int counter ){
		counter = count;
	}
	void set(int iden, int sect);
	void set(int quizOne, int quizTwo, int finalTest);


	int get_id();
	int get_section();
	int get_quiz1();
	int get_quiz2();
	int get_finalExam();

	double quizAverage();
	double finalGrade();

	void displayGrade();

};

void bubble(EECE230Student a[],int size );

void print (EECE230Student a[],int size );

double classAverage (EECE230Student a[]);

int main(){

	EECE230Student ast[4];

	ast[0].set(1,1);
	ast[0].set(10,20,30);

	ast[1].set(2,1);
	ast[1].set(40,50,60);

	ast[2].set(3,1);
	ast[2].set(70,80,90);

	ast[3].set(4,1);
	ast[3].set(35,55,75);

	for(int i=0; i<4 ; i++){

		ast[i].displayGrade();

	}

	
	bubble(ast,4);
	print(ast,4);

	cout << "class average :" << classAverage(ast) << endl; 
	
	system("pause");
	return 0;
}

EECE230Student::EECE230Student(){

	quiz1=0;
	quiz2=0;
	finalExam=0;
	
}
EECE230Student::EECE230Student(int id, int section){
	id=0;
	section=0;
}

void EECE230Student::set(int iden, int sect){
	
	id=iden;
	section=sect;
}

void EECE230Student::set(int quizOne, int quizTwo, int finalTest){

	quiz1=quizOne;
	quiz2=quizTwo;
	finalExam=finalTest;
}

int EECE230Student::get_id(){

	return id;
}
int EECE230Student::get_section(){

	return section;
}
int EECE230Student::get_quiz1(){

	if(quiz1 < 0 && quiz1 > 100)
		
			return false;

	return quiz1;
}
int EECE230Student::get_quiz2(){
	
	if(quiz2 < 0 && quiz2 > 100)

			return false;

	return quiz2;
}
int EECE230Student::get_finalExam(){

	if(finalExam < 0 && finalExam > 100 )
		
			return false;

	return finalExam;
}
double EECE230Student::quizAverage(){

	return (quiz1+quiz2)/2.0;
}
double EECE230Student::finalGrade(){

	return (quizAverage()+finalExam)/2.0;
}

void EECE230Student::displayGrade(){
	
	cout << " id: " << id << " final grade : " << finalGrade() << endl;

}



void bubble(EECE230Student a[],int size ){
	
	for(int i=0; i<4 ;i++ ) 
		for(int j = i+1 ; j<size ; j++ ) 
			if(a[i].finalGrade() > a[j].finalGrade())
				swap(a[i],a[j]);	
}

void print (EECE230Student a[],int size)
{
	for(int k=0; k<size;k++)
		cout<< a[k].finalGrade() << "  " ;

}

double classAverage (EECE230Student a[]){
	double sum=0;
	double average;
	
	for(int i=0; i<4; i++ ){
		sum = sum + a[i].finalGrade();
	}

	average = sum / 4.0;

	return average;
}
What exactly do you need?
Keeping count of number of objects? I Then check this out:
- Initialize count =0 outside the class not even in the ctor.
-In the ctor, include count++;

Aceix.
Topic archived. No new replies allowed.