-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_lesson7.cpp
476 lines (395 loc) · 8.94 KB
/
class_lesson7.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//Note-1:
/*We can use functions to access the data in a class.
Functions that access and/or modify data values in classes are called mutators.
Now let's add the setName function (functions in classes are also called methods) to our Student class. Recall the default for members in a class is private.
We want the access functions to be public.
So we add the keyword "public" and all members listed after it are accessible:*/
class Student
{
string name;
int id;
int gradDate;
public:
void setName(string name);
};
/* getDataValue.
Get functions return the data, so get functions have the variable type as a return variable.*/
//Note-2:
//A complete example :
class Student
{
string name;
int id;
int gradDate;
public:
void setName(string nameIn);
void setId(int idIn);
void setGradDate(int dateIn);
string getName();
int getId();
int getGradDate();
void print();
};
void Student::setName(string nameIn)
{
name = nameIn;
}
void Student::setId(int idIn)
{
id = idIn;
}
void Student::setGradDate(int gradDateIn)
{
gradDate = gradDateIn;
}
void Student::print()
{
cout<<name<<" "<<id<<" "<<gradDate;
}
string Student::getName()
{
return name;
}
int Student::getId()
{
return id;
}
int Student::getGradDate()
{
return gradDate;
}
//Note-3:
//better example
//main.cpp
/*Goal: practice creating and using classes.
**Create a class called Cat.
**Create the following members:
**private members: name, breed, age
**public members: setName, setBreed,setAge
**getName, getBreed, getAge, printInfo*/
#include"main.hpp"
int main()
{
Cat cat1,cat2;
cat1.setName("Kimmy");
cat2.setName("Bobby");
cat1.setBreed("calico");
cat2.setBreed("main coon");
cat1.setAge(4);
cat2.setAge(1);
cat1.printInfo();
cout<<"\n";
cat2.printInfo();
cout<<"\n\n";
//Alternate printing method
cout<<cat1.getName()<<" "<<cat1.getBreed()<<" "<<cat1.getAge()<<"\n";
cout<<cat2.getName()<<" "<<cat2.getBreed()<<" "<<cat2.getAge();
return 0;
}
//main.hpp
/*Header file for main.cpp
**Create a class called Cat.
**Create the following members:
**private members: name, breed, age
**public members: setName, setBreed,setAge
**getName, getBreed, getAge, printInfo*/
#include<iostream>
using namespace std;
class Cat
{
string name;
string breed;
int age;
public:
void setName(string nameIn);
void setBreed(string breedIn);
void setAge(int ageIn);
string getName();
string getBreed();
int getAge();
void printInfo();
};
void Cat::setName(string nameIn)
{
name = nameIn;
}
void Cat::setBreed(string breedIn)
{
breed = breedIn;
}
void Cat::setAge(int ageIn)
{
age = ageIn;
}
string Cat::getName()
{
return name;
}
string Cat::getBreed()
{
return breed;
}
int Cat::getAge()
{
return age;
}
void Cat::printInfo()
{
cout<<name<<" "<<breed<<" "<<age;
}
//Note-4:
/* A constructor is special function that is executed whenever we create a new instance of the class.
It is used to set initial values of data members of the class.
For example, in our Cats class we may want to have an initial value for the age of a cat and it's breed.
If we set initial values, we do not need to require the program or user set every value.
*/
/*
public:
Cats(); //declaring the constructor
void setName(string nameIn);
void setBreed(string breedIn);
void setAge(int ageIn);
string getName();
string getBreed();
int getAge();
void printInfo();
};
//defining the constructor
Cats::Cats()
*/
//Note-5:
/*
Destructors are special class functions that are called whenever an object goes out of scope.
Just like a constructor, a destructor is called automatically.
Destructors cannot:
- return a value
- accept parameters
Destructors must have the same name as the class.
Declaring a destructor:
~className()
*/
//Note-6
//Helper Functions
//Example
//main.cpp
#include "main.hpp"
int main()
{
Gameboard game1;
game1.setGameSpace(0,0,'x');
game1.setGameSpace(0,1,'x');
game1.setGameSpace(0,2,'x');
game1.setGameSpace(0,3,'y');
game1.setGameSpace(1,0,'x');
game1.setGameSpace(2,0,'x');
game1.setGameSpace(3,0,'x');
game1.setGameSpace(3,1,'x');
game1.setGameSpace(3,2,'x');
game1.setGameSpace(3,3,'x');
if(game1.fourInRow() == 1)
{
cout<<"X got four in a row!\n\n";
}
else
{
cout<<"X did not get four in a row :(\n\n";
}
game1.printInfo();
return 0;
}
//main.hpp
#include <iostream>
#include <iomanip>
using namespace std;
class Gameboard
{
char gameSpace[4][4];
public:
Gameboard();
void setGameSpace(int row,int column, char value);
char getGameSpace(int row,int column);
int fourInRow();
void printInfo();
};
Gameboard::Gameboard()
{
for(int i=0;i<4; i++)
for(int j=0;j<4; j++)
{
gameSpace[i][j] = '-';
}
}
void Gameboard::setGameSpace(int row,int column,char value)
{
gameSpace[row][column] = value;
}
char Gameboard::getGameSpace(int row,int column)
{
return gameSpace[row][column];
}
int Gameboard::fourInRow()
{
int count;
for(int i=0;i<4; i++)
{
count = 0;
for(int j=0;j<4; j++)
{
if(gameSpace[i][j]=='x')
{
count++;
//cout<<"count = "<<count;
}
}
if(count == 4)
return 1;
}
return 0;
}
void Gameboard::printInfo()
{
cout<<std::setw(5);
cout<<"\n";
for(int i=0;i<4; i++)
{
for(int j=0;j<4; j++)
{
cout<<gameSpace[i][j];
}
cout<<"\n";
}
}
//Note-7:
/*User defined objects can be used just as any other object is used.
For example, we can use a user defined object in an array.
#include "main.hpp"
int main()
{
const int SIZE= 3;
//Create a course of students
Student course1[SIZE];
//Each array element is a Student type
//so it has access to the members of Student
--> course1[0].setId(1000);
*/
//Note-8:
//this Pointer
/* C++ has a pointer called 'this'.
'this' returns its own address.
There are a few cases where 'this' might be necessary,
but often using it is considered a stylistic preference. */
//Use 'this' to compare areas
//The class functions
int compareWithThis(Shape shape)
{
//return the area of the calling shape
return this->Area() > shape.Area();
}
//Using the class function in a program
//In this case sh1.Area() is being compared to sh2.Area()
if(sh1.compareWithThis(sh2)) {
cout << "\nShape2 is smaller than Shape1" <<endl;
}
//perform the exact same function and not use 'this'.
//'this' is not necessary to compare shapes
int compare(Shape shapeIn)
{
return Area() > shapeIn.Area();
}
//use the class function:
if(sh1.compare(sh2))
{
cout << "\nShape2 is smaller than Shape1" <<endl;
}
//Complete example
#include <iostream>
using namespace std;
class Shape {
public:
// Constructor definition
Shape(int l = 2, int w = 2)
{
length = l;
width = w;
}
double Area()
{
return length * width;
}
//Use 'this' to compare areas
int compareWithThis(Shape shape)
{
return this->Area() > shape.Area();
}
//'this' is not necessary to compare shapes
int compare(Shape shapeIn)
{
return Area() > shapeIn.Area();
}
private:
int length; // Length of a box
int width;
};
int main(void)
{
Shape sh1(4, 4); // Declare shape1
Shape sh2(2, 6); // Declare shape2
if(sh1.compare(sh2))
{
cout << "\nShape2 is smaller than Shape1" <<endl;
}
else
{
cout << "\nShape2 is equal to or larger than Shape1" <<endl;
}
if(sh1.compareWithThis(sh2)) {
cout << "\nShape2 is smaller than Shape1" <<endl;
}
else
{
cout << "Shape2 is equal to or larger than Shape1" <<endl;
}
return 0;
}
//Note-9
/*
C++ allows class constructors to accept parameters. These parameters will set the values of class members when the object is created.
Let's look at an example of a Constructor with Parameters.*/
//main.cpp
#include "main.hpp"
int main()
{
//an instance of Patient is
//instanciated with a name
Patient p1("Tammy Smith");
cout<<p1.getName();
return 0;
}
//main.hpp
#include<iostream>
#include<string>
using namespace std;
class Patient
{
private:
string name;
public:
//The constructor accepts a name parameter
Patient(string input);
void setName(string input);
string getName();
};
Patient::Patient(string input)
{
//when an object is created
//the name must be added as a parameter
name = input;
}
void Patient::setName(string input)
{
name = input;
}
string Patient::getName()
{
return name;
}