Hello Friends,
So far we have seen what is iOS , why to choose iOS developer as a profession, objective c overview also we made some new friends from objective c world i.e objective c syntaxes. So our journey has already started and we are enjoying the the path to destination .
As we are now familiar with the required syntax of objective c, I think now it's time to create our first class . We will be discussing about how to create classes , how to proceed for the class creation, how to decide the structure of class etc. So let's start...!!
Before going for coding , I would like to share my experience about dealing with classes. Like most of us, I have completed my Engineering in Computer Science. But being honest, till the end completion of four years of my engineering , I didn't know how to relate classes and objects in our real life scenarios. All I used to do is only study some popular example from the books and use them everywhere. So when I started learning OOP , it was very hard for to understand the use of classes and objects in real IT industry working. I used to think like how can we build a softwares or applications by just creating their blueprint on paper?? How can I describe a real life object in terms of coding? I have faced all such and many more doubts, so I wanted to clarify them as somebody may find it helpful at the earlier stage .
When we proceed for converting real life objects into programming , always sit back for few minutes and think about all the possible features that object can have in real life. For example , if we have to represent Football in terms of coding. Start by writing down all the possible feature a football can have. Few of them can be color, size,manufacturer company, weight etc and the functions it can have will consists bouncing behaviour that is a football bounces.
Now we are clear about the properties and functionalities of the football object, we will proceed to create its object. Always create a class to represent such real life entity (Football in our case). So we will create a class whose name will be Football . We know that class is a collection of properties and functions, so we will create variables and constants of all the features/properties of Football and finally we will describe it's behaviour by creating a method called bounce.
You can see this thinking process in the above figure.
I hope you have got some idea about how to proceed for designing class structure.Now lets dive into coding . For better understanding we will create Student class and its objects.
Now the very first step we will do is , write down all the possible features and functions of a student.
The list will look somewhat like this:
Properties:
1. Name
2. Roll Number
3. Address
4. Divison
Functions:
1. Do Homework
2. Write an examination
3. Play
We are now all set with the blueprint, so we can proceed for the coding.
First we will design the class.
Class declaration:
#import <Foundation/Foundation.h>//we will see about this in coming posts.For now you can ignore it
@interface Student : NSObject
{
//Properties
int rollNumber;
NSString *name;
NSString *address;
}
//Functions (more appropriately Methods)
-(void)writeExamination;
-(void)doHomework;
-(void)play;
@end
This is just the class design, yet we will have to write its implementation.
Class implementation:
@implementation Student
//This is like constructor in other languages.
- (instancetype)initWithName:(NSString*)studentName rollNumber:(int)studentRollNumber andAddress:(NSString*)studentAddress
{
self = [super init];
if (self) {
rollNumber = studentRollNumber;
name = studentName;
address = studentAddress;
}
return self;
}
-(void)writeExamination{
//your logic here
}
-(void)doHomework{
//your logic here
}
-(void)play{
//your logic here
}
@end
The implementation will explain how exactly the methods are going to work. For example we can provide steps for writing an examination in writeExamination method.
We are ready with the blueprint and the implementation , so its time to use it with the help of an Object as we know class is nothing without its object.
int main(int argc, const char * argv[]) {
//Here we are creating object of the student class
Student *jenna = [[Student alloc]initWithName:@"Jenna" rollNumber:1 andAddress:@"Pune"];
Student *john = [[Student alloc]initWithName:@"John" rollNumber:2 andAddress:@"Pune"];
/*
alloc - is like malloc in C++, it allocates memory for object.
init - this method initialises the properties of a class with their default values.
*/
[jenna doHomework];//Method calling with the help of object
[jenna play];
[john writeExamination];
[john doHomework];
[john play];
return 0;
}
I have written comments wherever necessary. Yes, there are few things here which you will not find in any other programming language like alloc , NSObject , we are going to see all these in the coming posts. As this post was specifically for understanding classes and objects in action, so I have not explained those things here .
See you in the next post, till then Happy Coding..!!
HELLO Vishal Nice blog...I like to read such types of informative blogs...
ReplyDeleteKeep posting..
Thank you Suraj. Its my pleasure to know this.Will try to put more contents soon.Happy coding..!!!
ReplyDelete