Functions and objects, function objects
Example: Write a car class
// Declare the class
@ Interface Car: NSObject
{
Int _ speed; // speed
Int _ wheel; // wheel
}
-(Void) run;
@ End
// Class implementation
@ Implementation Car
-(Void) run {
NSLog (@ "car speed: % I, wheel is % I", _ speed, _ wheel );
}
@ End
// The function does not depend on the class.
Void test1 (Car * _ car ){
_ Car-> _ speed = 120;
_ Car-> _ wheel = 2;
}
// OC classes are all data types and can be used as parameters
Void test2 (Car * _ car ){
Car * car2 = [Car new];
Car2-> _ wheel = 4;
Car2-> _ speed = 200;
_ Car = car2
_ Car-> _ speed = 220;
_ Car-> _ wheel = 6;
}
# Import <Foundation/Foundation. h>
Int main (){
Car * car1 = [Car new];
Car1-> _ speed = 100;
Car1-> _ wheel = 4;
Test2 (car1 );
[Car1 run]; // The result is still 100 4.
Return 0;
}
Differences between functions and objects in java
// Assume that the object is a public class Person {private String name; // the object can have the private int age attribute; // age attribute // This is the method (or function). This is the public void eat () {System. out. println ("eat");} // This is the public boolean isFat (int kg) with the returned value {if (kg = 100) {System. out. println ("Too fat ");}}}
Object and member functions
A class is a data type of c ++. It is an encapsulation body of an attribute (data) set and its operations (Actions). An object is a specific instance of a class, has the attributes of this class; the two are the relationship between commonality and individuality.
When defining a class, it generally includes declaring data members and member functions. member functions specify the behavior attributes of the class. Of course, the constructor is also a member function.
You can understand the first two questions. Finally, this object does not completely belong to this class. It has its own unique data and behavior, so it is an object of A derived class of this class, that is, all attributes of a derived class that has a base class. In the final analysis, an object is an instance of the corresponding class.