Followers

Sunday, 7 September 2014

Classes

In the previous post we have explained how structures and classes are different from each other. Let's have a recap,

Classes and structures shares the similar definition syntax, which looks like this












What differentiate them ?

Structure: are values which are always copied when they are passed around.
Classes: use reference counting. It means more than one variable can reference a class instance.

Lets define a class with some properties, 















you might have noticed that we have done the initialization, which is not required in case of Structures. 









Inheritance: 

Inheritance feature allows a class to have a same behaviour as the parent class also behaviour can be  extended as per the need. Let's see how we can achieve this in Swift,





















Let's add 'isMorePowerful' method to our derived class 'Monitor', it will decide who is powerful among two monitor's based on the level assigned to them. 

Let's create two Monitor's with different level assigned to them, 

 Class Function:












That's it for now. I'll be updating this soon...