-
Notifications
You must be signed in to change notification settings - Fork 0
/
p7_Classes class.cpp
303 lines (278 loc) · 6.74 KB
/
p7_Classes class.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
//
// Classes class.cpp
// P7
//
// Created by Maria Palafox on 6/4/18.
// Copyright © 2018 Maria Palafox. All rights reserved.
//
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
/*
no memory until object created with that class as type= initialize objects (new)
two ways to return class person:
pass by reference for small or large:
const Person& older2(const person &a, const person &b)
{
if(a.getAge() > b.getApge()) ....
}
const is optional is making a copy.
-dont pass by reference to local object, the local objects destructor called with function terminates
not existing out of scope.. just return the object passed itself or pointer to object
RETURNING A POINTER TO AN OBJECT:
bob.intro() calls object of class . member
if it were a pointer
bob -> intro()
****2 ways to return...
[DELETE]
delete: deletes object the pointer points too....so
person *teen = makePerson2("al", 19);
teen-> intro();
delete teen; //oject deleted
(teen = nullptr;)
teen = makePerson1(...) //this is legal because pointer not deleted yet
//delete again;//pointer deleted..this is really baD!!!!
[on test # of new == # of deletes, set teen to nullptr when you delete object that teen points to]
HOW NEW WORKS
person *a = makePerson2("a1", 17);
when dynamically allocating with new, memory in heap reserved...
[IF IN CLASS YOU HAVE DYNAMICALLY ALLOCATED ARRAY], you would have to use destructor to delete all objects in array.
for loop to go thru and delete
*/
// class Class{
// public:
// Class(string name) {
// enrollment = 0;
// }
// bool addStudent(string name, int age)
// {
// Students[enrollment] = new Person(name, age);
// //Students is an array of pointers initializes to Person object (dynamic)
// enrollment++;
// return true;
// }
// ~Class()
// {
// for(int i = 0; i < enrollment; i++)
// delete students[i];
// } //delete every student allocated dynamically...with new...
//
//private:
// Person* Students[MAX_STUDENTS];
// int enrollment;
// //an array of pointers that point to dynamically allocated object of type Person
//
//
// }
//
////RETURNING A REFERRENCE TO A CONST OBJECT
//class Person;
//const Person& older2(const string& a, const string& b);
//class Person {
//public:
// Person(string name, int age){
// m_name = name;
// m_age = age;
// }
// int getAge() const {
// return m_age;
// }
//
//private:
// string m_name;
// int m_age;
//
//};
//
//const Person& older2(const Person& a, const Person& b){
// if(a.getAge() > b.getAge())
// return a;
// else
// return b;
//}
//
//int main(){
// Person a("Josie", 20);
// Person b("Joe", 18);
//
// const Person& c = older2(a,b);
// //above, c is an alias to a
//
//
// Person c = older2(a,b);
// //c is not an alias to a, initialized with a values
//
// const Person* c = &older2(a,b);
// //this works but im not sure why the (&) is in front of function that returns a reference type
// cout<< a.getAge();
//
//}
//
//
//CODE TRACING EXAMPLES:
#include <iostream>
using namespace std;
class A {
int foo = 0;
public:
int& getFoo() { //cant have const here because it doesnt know whether the value set to return will be an alias or not and if alias then it could change it
return foo;
}
void printFoo() const {
cout << foo;
}
};
int main() {
A a;
//int bar = a.getFoo();
int& bar = a.getFoo();
//now bar is a alias for foo, so if you increase barr it increases foo
//int bar = a.getFoo(), this is not an alias
//like ** pointers to pointers...&& ref of ref
cout << bar;
++bar;
a.printFoo();
}
//Output:
//00
//
//
//#include <string>
//#include <iostream>
//using namespace std;
//
//class Person
//{
// //private default
// string first_name;
// string last_name;
// int m_age;
//public:
// void set_name(string first, string second);
//
// void set_age(int m_age);
// string get_name() const;
// string get_last_name() const;
// int get_age() const;
//};
//
//
//void Person::set_name(string first, string second) {
// first_name = first;
// last_name = second;
//}
//
//void Person::set_age(int age)
//{
// m_age = age;
//}
//
//string Person::get_name() const {
// return first_name + " " + last_name;
//}
//
//string Person::get_last_name() const {
// return last_name;
//}
//
//int Person::get_age() const {
// return m_age;
//}
//
//Person have_baby(Person parent1, Person parent2) {
// Person child;
//
// //function call within function call
// child.set_name("Cade", parent1.get_name() + parent2.get_name());
// //parent1.get_last_name()...extra member functions that do stuff
// child.set_age(0);
// return child;
// //returning the object before it is destroyed since local variable..if you returned a reference to this object you would be UNDEFINed Line cross
//}
//
//int main() {
// Person mommy; //make object mommy, if there was a default constructor it would initialize mommy rn
// mommy.set_name("Vickie", "Topmiller"); //mommy name = vickie topmiller
// Person daddy;
// daddy.set_name("Mike", "Mallett");
// Person baby = have_baby(daddy, mommy);
// cout << baby.get_age() << " " << baby.get_name() << endl;
//}
//
//
//Output:
//0 Cade Mike MallettVickie Topmiller
//
//
//
//
//#include <iostream>
//#include <string>
//using namespace std;
//
//class Enemy {
// int secret;
//public:
// Enemy() {
// secret = -1;
// cout << "You've made an Enemy!" << endl;
// }
// Enemy(int i) {
// secret = i;
// cout << "You've made Enemy " << i << endl;
// }
// ~Enemy() {
// cout << "Destroying the enemy" << secret << endl;
// }
// void be_evil(int i) {
// cout << "I'm evil " << i << endl;
// }
// int get_secret() const {
// return secret;
// }
//};
//
//
////const pass by ref..save memory
//void foo(const Enemy& thisGuy) {
// Enemy thatGuy;
// //default constructor called...no ()
// thatGuy.be_evil(thisGuy.get_secret());
// //member function call as member function parameter..inception
//}
//
//int main() {
// Enemy booper;
// //default called
// Enemy bopper(3);
// //constructor with 1 parameter called
// foo(bopper);
// cout << "Done!" << endl;
//}
//
//
////
////
////Output
////
////You've made an Enemy!
////You've made Enemy 3
////You've made an Enemy!
////I'm evil 3
////Destroying the enemy -1 //destroying in inverse order they are created.. 1st destroy thatGuy when leaving foo
////Done!//end of main
////Destroying the enemy 3 //prints last 2 things made.. bopper(3)
////Destroying the enemy -1//prints booper()
////
////
//
//
//
//
//
//
//
//
//
//