forked from matkatmusic/PFMCPP_Project6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (97 loc) · 4.51 KB
/
Copy pathmain.cpp
File metadata and controls
120 lines (97 loc) · 4.51 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
/*
Project 6: Part 2 / 2
Video: Chapter 3 Part 3
Create a branch named Part2
References
1) convert the pointer usage (except for 'const char*') to reference types or
const reference types **>>> WHERE POSSIBLE <<<**
Not every pointer can be converted.
hint: There is no reference equivalent to nullptr.
if a pointer (including nullptr) is being returned anywhere, don't try to convert it to a reference.
You have to ask yourself if each pointer can be converted to a (const) reference.
Think carefully when making your changes.
2) revise the 'else' statement in main() that handles when `smaller` is a nullptr.
there is only one reason for `compare` to return nullptr.
Update this std::cout expression to specify why nullptr was returned.
3) After you finish, click the [run] button. Clear up any errors or warnings as best you can.
*/
#include <iostream>
#include <string>
struct T
{
T(int v, const char* n) : value(v), name(n) {}
int value;
std::string name;
};
struct FindSmaller
{
T* compare(T& a, T& b) //5, pass references to two different T objects
{
if( a.value < b.value ) return &a; // still need to RETURN a pointer
if( a.value > b.value ) return &b;
return nullptr; // return nullptr if a.value and b.value equal
}
};
struct U
{
float uVariableA { 0 }, uVariableB { 0 };
float uMemberFunction(const float& updatedValue) // this reference can be const because function doesn't change the input
{
std::cout << "U's uVariableA value: " << uVariableA << std::endl;
uVariableA = updatedValue;
std::cout << "U's uVariableA updated value: " << uVariableA<< std::endl;
while( std::abs(uVariableB - uVariableA) > 0.001f )
{
uVariableB += 0.1f;
}
std::cout << "U's uVariableB updated value: " << uVariableB << std::endl;
return uVariableB * uVariableA;
}
};
struct DoingSomething
{
static float updatingFunction(U& that, const float& updatedValue) // float reference can be const, function doesn't change the input
{
std::cout << "U's uVariableA value: " << that.uVariableA << std::endl; // now uVariables are MEMBERS of that
that.uVariableA = updatedValue;
std::cout << "U's uVariableA updated value: " << that.uVariableA<< std::endl;
while( std::abs(that.uVariableB - that.uVariableA) > 0.001f )
{
/*
write something that makes the distance between that->uVariableB and that->uVariableA get smaller
*/
that.uVariableB += 0.1f;
}
std::cout << "U's uVariableB updated value: " << that.uVariableB << std::endl;
return that.uVariableB * that.uVariableA;
}
};
/*
MAKE SURE YOU ARE NOT ON THE MASTER BRANCH
Commit your changes by clicking on the Source Control panel on the left, entering a message, and click [Commit and push].
If you didn't already:
Make a pull request after you make your first commit
pin the pull request link and this repl.it link to our DM thread in a single message.
send me a DM to review your pull request when the project is ready for review.
Wait for my code review.
*/
int main()
{
T tFirst(12, "tNameFirst");
T tSecond(11, "tNameSecond");
FindSmaller f;
auto* smaller = f.compare(tFirst, tSecond); // smaller still must be pointer because we don't know result
if(smaller != nullptr)
{
std::cout << "the smaller one is << " << smaller->name << std::endl;
}
else
{
std::cout << "The two values must be equal." << std::endl; // only reason for smaller being nullptr
}
U uFirst;
float updatedValue = 5.f;
std::cout << "[static func] uFirst's multiplied values: " << DoingSomething::updatingFunction(uFirst, updatedValue) << std::endl;
U uSecond;
std::cout << "[member func] uSecond's multiplied values: " << uSecond.uMemberFunction( updatedValue ) << std::endl;
}