Monday, June 26, 2017

Ch6: Swift Rectangle class example

import Foundation

class Rectangle {
    var length = 0
 var width = 0

 init(length: Int, width: Int) { // Constructor
  self.length = length
  self.width = width
 }
 var Area: Int{
  return length*width
 }

  func getArea() -> Int {
   return length*width
  }
}
class Circle {
    var radius = 0.0

 init(radius: Double) { // Constructor
  self.radius = radius

 }
 var Area: Double{
  return 3.14*(radius*radius)
 }

  func getArea() -> Double {
  return 3.14*(radius*radius)
  }
}

let rect1 = Rectangle(length: 10, width: 10)
print(rect1.Area)              // access the property
print(rect1.getArea())


let circ1=Circle(radius:12.0)
print(circ1.Area)
print(circ1.getArea())

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.