Skip to content

Instantly share code, notes, and snippets.

@KSXGitHub
Last active January 14, 2018 08:32
Show Gist options
  • Save KSXGitHub/da5b5854259389c08a2f04018bcda0a0 to your computer and use it in GitHub Desktop.
Save KSXGitHub/da5b5854259389c08a2f04018bcda0a0 to your computer and use it in GitHub Desktop.
Class-based OOP without "class"
// 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