Compare commits
2 commits
b1ac6cfb47
...
3405208295
Author | SHA1 | Date | |
---|---|---|---|
3405208295 | |||
c27c685712 |
6 changed files with 191 additions and 1 deletions
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug",
|
||||
"program": "${command:cmake.launchTargetPath}",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}",
|
||||
}
|
||||
]
|
||||
}
|
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
|
@ -1,4 +1,8 @@
|
|||
{
|
||||
"editor.detectIndentation": false,
|
||||
"editor.insertSpaces": false
|
||||
"editor.insertSpaces": false,
|
||||
"cmake.configureEnvironment": {
|
||||
"CC": "clang",
|
||||
"CXX": "clang++"
|
||||
},
|
||||
}
|
||||
|
|
|
@ -5,3 +5,6 @@ add_executable(sqrt sqrt.cc)
|
|||
target_link_libraries(sqrt PRIVATE Math)
|
||||
add_executable(inheritance inheritance.cc)
|
||||
add_executable(enumerations enumerations.cc)
|
||||
add_executable(namespaces namespaces.cc)
|
||||
add_executable(FunctionArguments FunctionArguments.cc)
|
||||
add_executable(exceptions exceptions.cc)
|
||||
|
|
31
src/FunctionArguments.cc
Normal file
31
src/FunctionArguments.cc
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
void sus(std::string susValue, std::string& susReference)
|
||||
{
|
||||
susValue = "sus?";
|
||||
susReference = "sussy?";
|
||||
|
||||
std::cout << "sus(std::string susValue, std::string& susReference) has ran" << std::endl;
|
||||
}
|
||||
|
||||
void print_string(std::string nameOf, std::string& string)
|
||||
{
|
||||
std::cout << "The value of " << nameOf << " is " << string << std::endl;
|
||||
}
|
||||
|
||||
void print_sussy_strings(std::string& sus1, std::string& sus2)
|
||||
{
|
||||
print_string("sus1", sus1);
|
||||
print_string("sus2", sus2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string sus1 = "this is the first sussy string";
|
||||
std::string sus2 = "this is the second sussy string";
|
||||
|
||||
print_sussy_strings(sus1, sus2);
|
||||
sus(sus1, sus2);
|
||||
print_sussy_strings(sus1, sus2);
|
||||
}
|
122
src/exceptions.cc
Normal file
122
src/exceptions.cc
Normal file
|
@ -0,0 +1,122 @@
|
|||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Crewmate {
|
||||
enum class Type {
|
||||
Imposter,
|
||||
Crewmate
|
||||
};
|
||||
class NotImposterException : public std::exception {
|
||||
private:
|
||||
const char* message;
|
||||
|
||||
public:
|
||||
NotImposterException(const char* msg)
|
||||
: message(msg)
|
||||
{
|
||||
}
|
||||
const char* what() const noexcept override
|
||||
{
|
||||
return message;
|
||||
}
|
||||
};
|
||||
class NotCrewmateException : public std::exception {
|
||||
private:
|
||||
const char* message;
|
||||
|
||||
public:
|
||||
NotCrewmateException(const char* msg)
|
||||
: message(msg)
|
||||
{
|
||||
}
|
||||
|
||||
const char* what() const noexcept override
|
||||
{
|
||||
return message;
|
||||
}
|
||||
};
|
||||
class Crewmate {
|
||||
public:
|
||||
Crewmate(Type type)
|
||||
{
|
||||
this->type = type;
|
||||
}
|
||||
|
||||
void is_imposter()
|
||||
{
|
||||
switch (this->type) {
|
||||
case Type::Imposter:
|
||||
std::cout << "this crewmate is an imposter. sussy." << std::endl;
|
||||
break;
|
||||
case Type::Crewmate:
|
||||
throw NotImposterException("this crewmate is not an imposter. not sussy.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
void is_crewmate()
|
||||
{
|
||||
switch (this->type) {
|
||||
case Type::Imposter:
|
||||
throw NotCrewmateException("this crewmate is not a crewmate. sussy.");
|
||||
break;
|
||||
case Type::Crewmate:
|
||||
std::cout << "this crewmate is a crewmate. not sussy." << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void invariant_testing()
|
||||
{
|
||||
switch (this->type) {
|
||||
|
||||
case Type::Imposter:
|
||||
throw NotCrewmateException("this crewmate is not a crewmate. sussy.");
|
||||
case Type::Crewmate:
|
||||
throw NotImposterException("this crewmate is not an imposter. not sussy.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Type type;
|
||||
};
|
||||
}
|
||||
|
||||
void exception_handler(const std::exception& exception, const std::string prefix = "Exception: ")
|
||||
{
|
||||
std::cerr << prefix << exception.what() << std::endl;
|
||||
}
|
||||
|
||||
void invariant_test(Crewmate::Crewmate& crewmate)
|
||||
{
|
||||
try {
|
||||
crewmate.invariant_testing();
|
||||
} catch (const Crewmate::NotImposterException& err) {
|
||||
exception_handler(err, "Imposter Exception: ");
|
||||
} catch (const Crewmate::NotCrewmateException& err) {
|
||||
exception_handler(err, "Crewmate Exception: ");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Crewmate::Crewmate imposter(Crewmate::Type::Imposter);
|
||||
Crewmate::Crewmate crewmate(Crewmate::Type::Crewmate);
|
||||
|
||||
try {
|
||||
imposter.is_imposter();
|
||||
imposter.is_crewmate();
|
||||
} catch (const Crewmate::NotCrewmateException& err) {
|
||||
exception_handler(err);
|
||||
}
|
||||
|
||||
try {
|
||||
crewmate.is_crewmate();
|
||||
crewmate.is_imposter();
|
||||
} catch (const Crewmate::NotImposterException& err) {
|
||||
exception_handler(err);
|
||||
}
|
||||
|
||||
invariant_test(imposter);
|
||||
invariant_test(crewmate);
|
||||
}
|
14
src/namespaces.cc
Normal file
14
src/namespaces.cc
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
|
||||
namespace Imposter {
|
||||
class Namespace {
|
||||
public:
|
||||
static void sussy_function() {
|
||||
std::cout << "This is a sus function." << std::endl;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int main() {
|
||||
Imposter::Namespace::sussy_function();
|
||||
}
|
Loading…
Reference in a new issue