Skip to content

Instantly share code, notes, and snippets.

@rockvivek
Created June 8, 2022 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockvivek/5b08352a8d8fe87be925564bacf9fb11 to your computer and use it in GitHub Desktop.
Save rockvivek/5b08352a8d8fe87be925564bacf9fb11 to your computer and use it in GitHub Desktop.
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