-
Notifications
You must be signed in to change notification settings - Fork 171
Initial implementation of Inheritance and Polymorphic functions #2801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from lpython import i32 | ||
|
||
class Animal: | ||
def __init__(self:"Animal"): | ||
self.species: str = "Generic Animal" | ||
self.age: i32 = 0 | ||
self.is_domestic: bool = True | ||
|
||
class Dog(Animal): | ||
def __init__(self:"Dog", name:str, age:i32): | ||
super().__init__() | ||
self.species: str = "Dog" | ||
self.name: str = name | ||
self.age: i32 = age | ||
|
||
class Cat(Animal): | ||
def __init__(self:"Cat", name: str, age: i32): | ||
super().__init__() | ||
self.species: str = "Cat" | ||
self.name:str = name | ||
self.age: i32 = age | ||
|
||
def main(): | ||
dog: Dog = Dog("Buddy", 5) | ||
cat: Cat = Cat("Whiskers", 3) | ||
op1: str = str(dog.name+" is a "+str(dog.age)+"-year-old "+dog.species+".") | ||
print(op1) | ||
assert op1 == "Buddy is a 5-year-old Dog." | ||
print(dog.is_domestic) | ||
assert dog.is_domestic == True | ||
op2: str = str(cat.name+ " is a "+ str(cat.age)+ "-year-old "+ cat.species+ ".") | ||
print(op2) | ||
assert op2 == "Whiskers is a 3-year-old Cat." | ||
print(cat.is_domestic) | ||
assert cat.is_domestic == True | ||
|
||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from lpython import i32 | ||
|
||
class Base(): | ||
def __init__(self:"Base"): | ||
self.x : i32 = 10 | ||
|
||
def get_x(self:"Base")->i32: | ||
print(self.x) | ||
return self.x | ||
|
||
#Testing polymorphic fn calls | ||
def get_x_static(d: Base)->i32: | ||
print(d.x) | ||
return d.x | ||
|
||
class Derived(Base): | ||
def __init__(self: "Derived"): | ||
super().__init__() | ||
self.y : i32 = 20 | ||
|
||
def get_y(self:"Derived")->i32: | ||
print(self.y) | ||
return self.y | ||
|
||
|
||
def main(): | ||
d : Derived = Derived() | ||
x : i32 = get_x_static(d) | ||
assert x == 10 | ||
# Testing parent method call using der obj | ||
x = d.get_x() | ||
assert x == 10 | ||
y: i32 = d.get_y() | ||
assert y == 20 | ||
|
||
main() |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.