-
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.
Enabled accessing base class members from derived class
- Loading branch information
Showing
3 changed files
with
56 additions
and
17 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 |
---|---|---|
@@ -1,18 +1,32 @@ | ||
from lpython import i32 | ||
|
||
class Base: | ||
def __init__(self:"Base"): | ||
self.x_A : i32 = 10 | ||
class Animal: | ||
def __init__(self:"Animal"): | ||
self.species: str = "Generic Animal" | ||
self.age: i32 = 0 | ||
|
||
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 | ||
|
||
class Derived(Base): | ||
def __init__(self:"Derived") : | ||
super().__init__() | ||
self.y_B : i32 = 6 | ||
def get_x_A(self:"Derived"): | ||
print(self.x_A) | ||
def main(): | ||
d : Derived = Derived() | ||
print(d.x_A) | ||
print(d.y_B) | ||
d.get_x_A() | ||
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." | ||
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." | ||
|
||
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