Created
March 7, 2023 06:48
-
-
Save iarash84/46a63831b45516d039356591c7c92f3e to your computer and use it in GitHub Desktop.
virtual function exmaple
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
class Shape { | |
public: | |
virtual void draw() { | |
std::cout << "Drawing a shape\n"; | |
} | |
}; | |
class Circle : public Shape { | |
public: | |
void draw() override { | |
std::cout << "Drawing a circle\n"; | |
} | |
}; | |
int main() { | |
Shape* shapePtr = new Circle(); | |
shapePtr->draw(); // Output: "Drawing a circle" | |
delete shapePtr; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment