C-language implementation of the C + + class encapsulation is actually using the structure as a C + + class attribute, with the global function (this function is the parameter of this structure body pointer and other parameters) as a class method or behavior.
struct time{
int Hour;
int Min;
int Sec;
};
void SetTime (struct time *time, int hour, int min, int sec)
{
Time->hour = Hour;
Time->min = Min;
Time->sec = Sec;
}
void print (struct time *time)
{
printf ("%d:%d:%d\n", Time->hour, Time->min, time->sec);
}
int main ()
{
struct time AA;
SetTime (&AA, 12, 45, 54);
Print (&AA);
return 0;
}
The above C-language code and the following C + + code function is a meaning:
Class time
{
Private
int Hour;
int Min;
int Sec;
Public
void settime (int hour, int min, int sec);
void print ();
};
void Time::settime (int hour, int min, int sec)
{
This->hour = Hour;
This->min = Min;
This->sec = Sec;
}
void time::p rint ()
{
cout << this->hour << ":" << this->min << ":" << this->sec << Endl;
}
int main ()
{
Time time;
Time. SetTime (12, 45, 57);
Time.print ();
return 0;
}