add a enumeration program
also fix the name of the squared function. its not a square root, you're squaring the number.
This commit is contained in:
parent
cf27c686c2
commit
16504e70a4
6 changed files with 72 additions and 14 deletions
55
src/enumerations.cc
Normal file
55
src/enumerations.cc
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include <iostream>
|
||||
|
||||
std::string generate_crewmate_string(std::string type);
|
||||
|
||||
enum class CrewmateType {
|
||||
Sus,
|
||||
NotSus,
|
||||
Imposter
|
||||
};
|
||||
|
||||
class Crewmate {
|
||||
public:
|
||||
Crewmate(CrewmateType type)
|
||||
{
|
||||
this->type = type;
|
||||
}
|
||||
|
||||
CrewmateType type;
|
||||
|
||||
// check if the type variable equals any of the values in the CrewmateType enumerator.
|
||||
void print_type()
|
||||
{
|
||||
switch (this->type) {
|
||||
case CrewmateType::Sus:
|
||||
std::cout << generate_crewmate_string("Sus") << std::endl;
|
||||
break;
|
||||
case CrewmateType::NotSus:
|
||||
std::cout << generate_crewmate_string("NotSus") << std::endl;
|
||||
break;
|
||||
case CrewmateType::Imposter:
|
||||
std::cout << generate_crewmate_string("Imposter") << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::string generate_crewmate_string(std::string type)
|
||||
{
|
||||
return "Your crewmate type is " + type;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Crewmate sussy = Crewmate(CrewmateType::Sus);
|
||||
Crewmate notSussy = Crewmate(CrewmateType::NotSus);
|
||||
Crewmate imposter = Crewmate(CrewmateType::Imposter);
|
||||
|
||||
// create an array of crewmate pointers.
|
||||
Crewmate* crewmates[3] = { &sussy, ¬Sussy, &imposter };
|
||||
|
||||
// loop through each of them to print their type.
|
||||
for (Crewmate* crewmate : crewmates) {
|
||||
crewmate->print_type();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
std::cout << "hello" << std::endl;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "math.hh"
|
||||
double Math::sqrt(double x) {
|
||||
double Math::square(double x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class Math {
|
||||
public:
|
||||
double static sqrt(double x);
|
||||
double static square(double x);
|
||||
};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
executable('hello', 'hello.cc')
|
||||
executable('sqrt', ['math.cc', 'sqrt.cc'])
|
||||
executable('inheritance', 'inheritance.cc')
|
||||
executable('enumerations', 'enumerations.cc')
|
||||
|
|
18
src/sqrt.cc
18
src/sqrt.cc
|
@ -1,15 +1,15 @@
|
|||
#include <iostream>
|
||||
#include "math.hh"
|
||||
#include <iostream>
|
||||
|
||||
double sqrt(double x);
|
||||
void print_sqrt(double x);
|
||||
void print_squared(double x);
|
||||
|
||||
int main() {
|
||||
print_sqrt(21.420);
|
||||
print_sqrt(420);
|
||||
int main()
|
||||
{
|
||||
print_squared(21.420);
|
||||
print_squared(420);
|
||||
}
|
||||
|
||||
void print_sqrt(double x) {
|
||||
std::cout << "the square root of " << x << " is " << Math::sqrt(x) << std::endl;
|
||||
void print_squared(double x)
|
||||
{
|
||||
std::cout << "the squared of " << x << " is " << Math::square(x) << std::endl;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue