Created
June 8, 2022 10:55
-
-
Save rockvivek/5b08352a8d8fe87be925564bacf9fb11 to your computer and use it in GitHub Desktop.
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
protocol Shape { | |
func calculateArea() -> Double | |
} | |
class Rectangle: Shape{ | |
let height: Double | |
let width: Double | |
init(height: Double, width: Double) { | |
self.height = height | |
self.width = width | |
} | |
func calculateArea() -> Double{ | |
return width * height | |
} | |
} | |
class Circle: Shape{ | |
let radius: Double | |
init(radius: Double) { | |
self.radius = radius | |
} | |
func calculateArea() -> Double{ | |
return 3.14 * 3.14 * radius | |
} | |
} | |
class MeasureArea { | |
func area(obj: Shape) -> Double{ | |
return obj.calculateArea() | |
} | |
} | |
let measureAreaObj = MeasureArea() | |
let rectObj = Rectangle(height: 5.0, width: 7.0) | |
let circleObj = Circle(radius: 10) | |
print("Area of rect is: \(measureAreaObj.area(obj: rectObj))") | |
print("Area of circle is: \(measureAreaObj.area(obj: circleObj))") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment