-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer Class And Time class.cpp
More file actions
216 lines (206 loc) · 5.27 KB
/
Player Class And Time class.cpp
File metadata and controls
216 lines (206 loc) · 5.27 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<iostream>
using namespace std;
class time
{
int hour, min, sec;
public:
time(int a = 1, int b = 1, int c = 1)
{
cout << "\nIn Time Constructor\n ";
hour = a;
min = b;
sec = c;
}
void display()
{
cout << "\nIn Time Display()\n ";
cout<< hour<<":" << min << ":" << sec <<endl;
}
~time()
{
cout << "\nTIME Destructor called\n\n";
}
////Implement Setter getter and other utility functions here
};
class player
{
int Id;// //> .
char name;// //> .
int size;// //> non-static and non-constant data members
int *Scores;// //> .
float Average;// //> .
static int count; //static data members
const char gender;//Constant data member
class time *join; //creating an object of time class to implement composition
public:
player(int = 0, char = 'a', int s = 2, char = 'M', time * = NULL, int * =
NULL);//Default parameterized constructor
//player();//default constructor discuss during lecture
////Copy constructor//discuss during lecture
player(const player&);
// ....... Utility Functions ........
player& calAverage(void);
player& print(void);
//...... Setter or Mutator Functions ......
void setId(int);
void setName(char);
void setsize(int);
void setScores(int *);//interesting
// ..... Accessor or Getter functions .......
int getID(void) const;
char getName(void) const;
float getAverage(void);
int getsize(void) const;
//How to write getscores function ?????
static void showcount() // static function
{
//cout << name;
cout << "\nValue of count" << count;
}
~player(); //Destructor
};
//////////////////////// . . . define class functions out of line/scope . . . ////////
int player::count = 0;//assigning value to static data member of class
player::player(int i, char n, int s, char g, time *t, int *arr) : Id(i), name(n), size(s), gender(g)//Constant data member and composition must
//need intilizer with constructor
{
cout << "\nInside parameterized Constructor : \n";
join = new time;
if (t != NULL) {
*join = *t;
}
if (arr == NULL)
{
Scores = new int[size];
cout << "Enter values of " << size << " player : ";
for (int i = 0; i < size; i++)
{
cin >> Scores[i];
}
}
else
{
Scores = new int[size];
cout << "Enter values of " << size << " students : ";
for (int i = 0; i < size; i++)
{
cout << "\nEnter " << i + 1 << " Value : ";
cin >> Scores[i];
}
}
player::calAverage();//calculating average in constructor
count++;
}
///Defination of copy constructor
////Copy constructor
player::player(const player & p) :gender(p.gender)
{
cout << "\nIn Copy Constructor\n";/**/
this->Id = p.Id;
this->size = p.size;
this->Scores = new int[this->size];
cout << "\nEnter " << size << " Values for scores";
for (int i = 0; i < this->size; i++)
{
cout << "\nEnter " << i + 1 << " Value : ";
cin >> this->Scores[i];
}
this->join = new time;
*join = *(p.join);
player::calAverage();
count++;
}
// ....... Utility Functions ........
player& player::calAverage(void)
{
cout << "\nInside CalculateAverage() Function\n";
int s = 0;
for (int i = 0; i < size; i++)
{
s += Scores[i];
}
this->Average = float(s) / size;
return *this;
}
player& player::print()
{
cout << "\n................................";
cout << "\nInside print() function";
cout << "\nName of player is : " << name;
cout << "\nID of player is : " << Id;
cout << "\nTotal matches played are : " << size;
cout << "\nScores of player is: ";
for (int i = 0; i < size; i++)
{
cout << Scores[i] << " ";
}
cout << endl;
cout << "\nAverage of player is: " << this->Average;
cout << "\n................................\n";
cout<<"\n Display Time : ";
cout<<join<<endl;
//join.display();///called display function of composition
return *this;
}
//...... Setter or Mutator Functions ......
void player::setId(int i)
{
cout << "\nInside setId() function";
Id = i;
}
void player::setName(char c)
{
cout << "\nInside setName() function";
//name = c;
}
void player::setsize(int s)
{
cout << "\nInside setsize() function";
this->size = s;
}
void player::setScores(int *arr)
{
cout << "\nInside setScores() function";
delete[] Scores;
Scores = new int[size];
for (int i = 0; i < size; i++)
{
Scores[i] = arr[i];
}
}
// ..... Accessor or Getter functions .......
int player::getID(void) const
{
cout << "\nInside getId() function\n";
return this->Id;
}
char player::getName(void) const
{
cout << "\nInside getName() function\n";
return name;
}
float player::getAverage()///an interesting fact inside function
{
cout << "\nInside getAverage() function\n";
player::calAverage();
return Average;
}
int player::getsize(void) const
{
return (*this).size;
}
//Definition of Destructor
player::~player() //Destructor
{
cout << "\nInside Destructor that Delete Dynamic Memory\n";
delete[] Scores;
delete[] join;
count--;
}
int main()
{
class time t1(2, 2, 2);
t1.display();
player p1(2,'N',3,'F', 5,3,10 );///object of player for implementing composition
p1.print();
}