Question

Swift programming language Write a class Pet with two properties and two methods of your choice....

Swift programming language

Write a class Pet with two properties and two methods of your choice. Then write a subclass Cat which inherits from Pet. Cat should override at least one method from Pet, and should have at least one extra method and one extra property unique to Cat.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Pet
{
   var Noise: String
   var color: String
  
   init(Noise: String, color: String)
   {
       print(" super class constructor")
       self.Noise = Noise
       self.color = color
   }
   func setNoise(Noise:String)
   {
   self.Noise=Noise
   }
  
   func display()
   {
      
       print("Noise = " + self.Noise)
       print("color = " + self.color)
   }
}

class Cat : Pet
{
   var legs: Int = 0
   convenience init(Noise: String, color: String,legs: Int)
   {
       self.init(Noise: Noise, color: color)
       self.legs = legs
   }
  
   override func display()
   {
       super.display()
       print("Number of legs = ", self.legs)
   }
   func printnoise()
   {
   print("Noise:"+super.Noise)
   }
}

var c1 = Cat(Noise: "MEW MEW",
                   color: "RED",
                   legs: 4)
c1.display()
c1.printnoise()

Add a comment
Know the answer?
Add Answer to:
Swift programming language Write a class Pet with two properties and two methods of your choice....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT