I have to make a program where I use hierarchy using classes in Python. I have been trying to figure out why my child class Rectangle can't get the values from the parent class Quadrilateral. When I try to
print("Width:",r.get_width())
print("Area:", r.get_area())
print("Perimeter:",r.get_perimeter())
it either says I am missing parameters or it has no attribute '_Quadrilateral__width'
Can anyone explain why this is happening? Thanks
Parent
class Quadrilateral(Polygon):
def __init__(self, width, height):
super().__init__(width, height)
def get_width(self):
""" Returns width"""
return self.__width
def get_height(self):
"""Returns height"""
return self.__height
def set_width(self, width):
"""Sets the width"""
if width <= 0:
raise ValueError('Width must be positive.')
self.__width = width
def set_heigth(self, height):
"""Sets the height"""
if height <= 0:
raise ValueError('Height must be positive')
self.__height = height
Child
try: Quadrilateral
except: from Quadrilateral import Quadrilateral
class Rectangle(Quadrilateral):
def __init__(self, width, height):
super().__init__(width, height)
def get_area(self, width, height):
"""returns the area of a Rectangle"""
return self.__width * self.__height
def get_perimeter(self, width, height):
"""returns the perimeter of a Rectangle"""
return 2 * (self.__width + self.__height)
if __name__ == '__main__':
r = Rectangle(2,4)
print("Width:",r.get_width())
print("Area:", r.get_area())
print("Perimeter:",r.get_perimeter())
I want to make a rectangle with width of 2 and length of 4 to test it but it won't let me. I have a higher class. Which might be a problem
class Polygon:
"""Polygon Parent class"""
def __init__(self, width, height):
self.__width = width
self.__height = height
I want to have the Polygon class have all my function headers but its giving me syntax errors. Any chance I can have them there without having to write out all of what each function does?