What is a class and what is an object? How do I define a class? How do I define an object?
First, to define a human
class person{ public: void getweight (); void setweight (); Private: int weight; };
Declare a human with the class keyword. Now just declaring a class without using a class? The system does not allocate memory to the class. Public is a common member method. Private is a privately owned data member. When declaring a class, do not forget the (;) number.
Then we can define an object for a human such as:
Person Jack;
What is the difference between a class and an object?
A class is abstract, and an object is an instance of a class.
Now that we know the difference between a class and an object, how do you access the members and methods of the object? We can use (.) To access
such as Jack.wight; Access members
You can also assign a value to a member Jack.wight = 160;
such as Jack.getweight (); Access method
Instance explaining the data members and methods in the calling class
#include <iostream>using namespacestd;//---------------------------------------------------------------------------classperson{ Public: voidSetweight (intm_weight); voidgetweight ();Private: intWeight;};voidPerson::setweight (intm_weight) {Weight=m_weight;}voidperson::getweight () {cout<<Weight<<Endl;}int_tmain (intARGC, _tchar*argv[]) {Person Jack; Jack.setweight ( the); Jack.getweight (); return 0;}//---------------------------------------------------------------------------
The above is the most basic class of calls.
C + + 's lesson object-oriented (i)