Reader small image

You're reading from  Object???Oriented Programming with Swift 2

Product typeBook
Published inJan 2016
Reading LevelIntermediate
Publisher
ISBN-139781785885693
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Gaston C. Hillar
Gaston C. Hillar
author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar

Right arrow

Recognizing variables and constants to create properties


We know the information required for each of the shapes to achieve our goals. Now, we have to design the classes to include the necessary properties that provide the required data to each instance. We have to make sure that each class has the necessary variables that encapsulate all the data required by the objects to perform all the tasks based on our application domain.

Let's start with the RegularHexagon class. It is necessary to know the length of a side for each instance of this class—that is, for each regular hexagon object. Thus, we need an encapsulated variable that allows each instance of the RegularHexagon class to specify the value for the length of a side.

Tip

The variables defined in a class to encapsulate the data for each instance of the class in Swift are known as properties. Each instance has its own independent value for the properties defined in the class. The properties allow us to define the characteristics for an instance of the class. In other programming languages, these variables defined in a class are known as either attributes or fields.

The RegularHexagon class defines a floating point property named lengthOfSide, whose initial value is equal to 0 for any new instance of the class. After we create an instance of the RegularHexagon class, it is possible to change the value of the lengthOfSide attribute.

Note the usage of Camel case, which is using a lowercase first letter, for class property names. The first letter is lowercase, and then, the first letter for each word that composes the name is capitalized, while the other letters are in lowercase. It is a coding convention in Swift for both variables and properties. For example, we use the name lengthOfSide for the property that stores the value of the length of side.

Imagine that we create two instances of the RegularHexagon class. One of the instances is named regularHexagon1 and the other regularHexagon2. The instance names allow us to access the encapsulated data for each object, and therefore, we can use them to change the values of the exposed properties.

Swift uses a dot (.) to allow us to access the properties of instances. So, regularHexagon1.lengthOfSide provides access to the length of side for the RegularHexagon instance named regularHexagon1, and regularHexagon2.lengthOfSide does the same for the RegularHexagon instance named regularHexagon2.

Tip

Note that the naming convention makes it easy for us to differentiate an instance name—that is, a variable from a class name. Whenever we see the first letter in uppercase or capitalized, it means that we are talking about a class.

We can assign 20 to regularHexagon1.lengthOfSide and 50 to regularHexagon2.lengthOfSide. This way, each RegularHexagon instance will have a different value for the lengthOfSide attribute.

Now, let's move to the Ellipse class. We can define two floating point attributes for this class: semiMajorAxis and semiMinorAxis. Their initial values will also be 0. Then, we can create three instances of the Ellipse class named ellipse1, ellipse2, and ellipse3.

We can assign the values summarized in the following table to the three instances of the Ellipse class:

Instance name

semiMinorAxis value

semiMajorAxis value

ellipse1

210

400

ellipse2

180

300

ellipse3

180

356

This way, ellipse1.semiMinorAxis will be equal to 210, while ellipse3.semiMinorAxis will be equal to 180. The ellipse1 instance represents an ellipse with semiMinorAxis of 210 and semiMajorAxis of 400.

The following table summarizes the floating point properties defined for each of the six classes that we need for our application:

Class name

Properties list

Square

lengthOfSide

EquilateralTriangle

lengthOfSide

Rectangle

width and height

Circle

radius

Ellipse

semiMinorAxis and semiMajorAxis

RegularHexagon

lengthOfSide

Tip

The properties are members of their respective classes. However, properties aren't the only members that classes can have.

Note that three of these classes have the same property: lengthOfSide—specifically, the following three classes: Square, EquilateralTriangle, and RegularHexagon. We will dive deep into what these three classes have in common later and take advantage of object-oriented features to reuse code and simplify our application's maintenance. However, we are just starting our journey, and we will make improvements as we learn additional object-oriented features included in Swift.

The following image shows a UML (Unified Modeling Language) class diagram with the six classes and their properties. This diagram is very easy to understand. The class name appears on the top of the rectangle that identifies each class. A rectangle below the same shape that holds the class name displays all the property names exposed by the class with a plus sign (+) as a prefix. This prefix indicates that what follows it is an attribute name in UML and a property name in Swift.

Previous PageNext Page
You have been reading a chapter from
Object???Oriented Programming with Swift 2
Published in: Jan 2016Publisher: ISBN-13: 9781785885693
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar