Last active
January 14, 2018 08:32
-
-
Save KSXGitHub/da5b5854259389c08a2f04018bcda0a0 to your computer and use it in GitHub Desktop.
Class-based OOP without "class"
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
// base class | |
class Animal { | |
private: | |
// private property | |
int age; | |
bool sex; | |
public: | |
// constructor | |
Animal (int age, bool sex) { | |
this->age = age; | |
this->sex = sex; | |
} | |
// public method | |
virtual string greet() { | |
return ''; | |
} | |
int getAge () { | |
return age; | |
} | |
string getSex () { | |
return sex ? "male" : "female"; | |
} | |
}; | |
// subclass: Dog | |
class Dog: public Animal { | |
public: | |
// overriding method | |
string greet() { | |
return "woof"; | |
} | |
// own method | |
string bark() { | |
return this->greet(); | |
} | |
}; | |
// subclass: Human | |
class Human: public Animal { | |
private: | |
// own private property | |
string name; | |
public: | |
// own constructor | |
Human (int age, bool sex, string name) | |
// call super-constructor | |
: Animal(age, sex) | |
{ | |
this->name = name; | |
} | |
// overriding method | |
string greet() { | |
return "Greetings! I'm " + this->name; | |
} | |
string getName() { | |
return name; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment