Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Created December 28, 2018 14:58
Show Gist options
  • Save miladvafaeifard/87346ec4d84ac1a5e3af41bb46209f59 to your computer and use it in GitHub Desktop.
Save miladvafaeifard/87346ec4d84ac1a5e3af41bb46209f59 to your computer and use it in GitHub Desktop.
Abstract class in c++
#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