Tuesday, January 1, 2013

OOP concept : As Objective C is Object oriented.

OOP(Object oriented programming) is way to construct software which is made of objects(Single or many). Object is like entity which differentiate from other types. eg. Car, Human etc.
In Object oriented programming Data is center point around which function are there this is exactly opposite to procedural programming.

In Objective calling function on object is called as sending a message

Some terms used in OOP
1.Class:- It is blueprint of object. actually its a structure which represent the Object. Contain required data member and function of object.
2.Object/ Instance:- Its an existing entity  in memory having value of objects .(In objective c one more parameter is there for each object is hidden pointer to its class)

OOP in objective C
anything after @ is compiler descriptive
1.@interface:-
It is used to declare a data member (i.e. of that object) and functions(i.e. features provided by object).
 eg:-
//starting of class
@interface Rectangle : NSObject
{//in this curly bracket add data members of objects
   int x;
   int y;
   int width;
   int height;
   int clr;
}
//here outside of curly bracket add functions/ features of objects
-(void) Draw;
-(void) setColor:(int) clr;

@end
//end of class

below is normal declaration of method in objective c
-(return type) funcname :(passing arg1 type) variablename;


-(return type) funcname :(passing arg1 type) variable2name (passing arg2 type) variablename;


-(return type) part1funcname :(passing arg1 type) variable1name  part2funcname :(passing arg2 type) variable2name part3funcname :(passing arg3 type) variable3name;

Objective C uses infix notation in which method and its arguments are intertwined.
eg:-
[rect setColor:clr];
[objectname fumctionname:arg];
[objectname part1fumctionname:arg1 part2fumctionname:arg2];

Imp note:-
When calling a method , do not add colon at the end of the it when it is without parameter.
Some times compiler will not give error as this is run time language.

2.@implementation
It is the place where class's method defined.
@ implementation Rectangle
//implementation starts here
-(void) draw
{
//write a code for drawing rectangle
}

-(void) setColor:(int) clr
{
//define function here
}
@end
//implementation ends here

You can define extra functions in the implementation which are not declared in the @interface .
These functions are private function not visible outside of class.

Like this pointer in c++ , In objective C for every function there is one hidden parameter  named self even for static method also (which is not in c++). But in static method it will not have instance variables.

3.Object Creation:-
Allocating memory to object and initialization of object.
NSObject is base class for almost every class in Objective . its is the super most class like Object in java.
Normally uses alloc and init method of the NSObject to allocate and initialize the Object;
eg.
Rectangle *rect=[[Rectangle alloc] init];

In object-oriented programming, the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
[source :- http://en.wikipedia.org/wiki/Open/closed_principle]


0 comments:

Post a Comment

About

Powered by Blogger.