-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2801 from tanay-man/inheritance
Initial implementation of Inheritance and Polymorphic functions
- Loading branch information
Showing
9 changed files
with
207 additions
and
34 deletions.
There are no files selected for viewing
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
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
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 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 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 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 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 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 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.