-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from kenluobo/add_virtual_method_test_case
Add virtual method test case
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "utils/test.hpp" | ||
#include <cstdint> | ||
#include <string> | ||
|
||
//================================================================== | ||
enum class AnimalCategory : std::uint8_t { | ||
LandAnimal = 0, | ||
WaterAnimal, | ||
Amphibian | ||
}; | ||
|
||
static std::string convertAnimalCategoryToString(AnimalCategory AC) { | ||
switch (AC) { | ||
case AnimalCategory::LandAnimal: | ||
return "LandAnimal"; | ||
case AnimalCategory::WaterAnimal: | ||
return "WaterAnimal"; | ||
case AnimalCategory::Amphibian: | ||
return "Amphibian"; | ||
} | ||
} | ||
|
||
//================================================================== | ||
class Animal { | ||
public: | ||
virtual AnimalCategory getCategory() = 0; | ||
virtual std::string getCategoryAsString() = 0; | ||
|
||
private: | ||
AnimalCategory AC; | ||
}; | ||
|
||
//================================================================== | ||
class Human : public Animal { | ||
public: | ||
virtual AnimalCategory getCategory() { return AC; } | ||
virtual std::string getCategoryAsString() { | ||
return convertAnimalCategoryToString(AC); | ||
} | ||
virtual ~Human() = default; | ||
|
||
private: | ||
AnimalCategory AC = AnimalCategory::LandAnimal; | ||
}; | ||
|
||
class Employee : public Human { | ||
public: | ||
virtual ~Employee() = default; | ||
}; | ||
|
||
//================================================================== | ||
class Fish : public Animal { | ||
public: | ||
virtual AnimalCategory getCategory() { return AC; } | ||
virtual std::string getCategoryAsString() { | ||
return convertAnimalCategoryToString(AC); | ||
} | ||
|
||
private: | ||
AnimalCategory AC = AnimalCategory::WaterAnimal; | ||
}; | ||
|
||
//================================================================== | ||
void test_virtual_method() { | ||
// todo | ||
Human h; | ||
out("Human ", h.getCategoryAsString()); | ||
|
||
Employee e; | ||
out("Employee ", e.getCategoryAsString()); | ||
|
||
Fish f; | ||
out("Fish ", f.getCategoryAsString()); | ||
|
||
Employee *p_e = &e; | ||
out("Employee Pointer ", p_e->getCategoryAsString()); | ||
out("Human Pointer ", static_cast<Human *>(p_e)->getCategoryAsString()); | ||
} | ||
|
||
//================================================================== | ||
void test() { TEST(test_virtual_method); } | ||
|
||
//================================================================== | ||
int main() { test(); } |