Created
December 28, 2018 14:58
-
-
Save miladvafaeifard/87346ec4d84ac1a5e3af41bb46209f59 to your computer and use it in GitHub Desktop.
Abstract class in c++
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 <iostream> | |
class Animal { | |
public: | |
Animal(std::string name): _name(name){} | |
~Animal(){ | |
_name = ""; | |
std::cout << "deleted animal\n"; | |
} | |
std::string getName(){ return _name; } | |
virtual void speak() = 0; | |
private: | |
std::string _name; | |
}; | |
class Cat: public Animal { | |
public: | |
Cat(std::string name): Animal(name) {} | |
void speak() override { | |
std::cout << "Cat is speaking" << this->getName() << std::endl; | |
} | |
}; | |
int main() | |
{ | |
Cat cat = Cat("Meow"); | |
cat.speak(); | |
//Cat cat = new Cat("Mewo"); | |
//cat->speak(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment