Last active
August 29, 2015 14:02
-
-
Save thomasfedb/93ed9f6ba8b904ccecc4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ~/Projects/c++test # clang++ -std=c++11 test.cpp | |
| test.cpp:52:18: error: variable type 'AbstractPerson' is an abstract class | |
| AbstractPerson x = Teacher("Thomas"); | |
| ^ | |
| test.cpp:19:20: note: unimplemented pure virtual method '_getName' in 'AbstractPerson' | |
| virtual string _getName() = 0; | |
| ^ | |
| test.cpp:20:18: note: unimplemented pure virtual method '_isTeacher' in 'AbstractPerson' | |
| virtual bool _isTeacher() = 0; | |
| ^ | |
| 1 error generated. |
This file contains hidden or 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
| #include <string> | |
| #include <iostream> | |
| using namespace std; | |
| class AbstractPerson { | |
| public: | |
| string getName() { | |
| return this->_getName(); | |
| } | |
| bool isTeacher() { | |
| return this->_isTeacher(); | |
| } | |
| private: | |
| virtual string _getName() = 0; | |
| virtual bool _isTeacher() = 0; | |
| }; | |
| class Person : public AbstractPerson { | |
| public: | |
| Person(string name) : name(name) {}; | |
| private: | |
| string name; | |
| string _getName() { | |
| return this->name; | |
| } | |
| bool _isTeacher() { | |
| return false; | |
| } | |
| private: | |
| }; | |
| class Teacher : public Person { | |
| public: | |
| Teacher(string name) : Person(name) {}; | |
| private: | |
| bool _isTeacher() { | |
| return true; | |
| } | |
| }; | |
| int main() { | |
| AbstractPerson x = Teacher("Thomas"); | |
| if (x.isTeacher()) { | |
| cout << x.getName() << " is a teacher." << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment