C-style encapsulated data is put together and passed to the behavior in the form of references and pointers
C + + considers encapsulation not exhaustive
1 data and behavior separate externally available interfaces
2 No permission settings
Look at an example of a struct
1 //data.h2 3 //C-style encapsulated data is put together and passed to the behavior in the form of references and pointers4 //C + + considers encapsulation not exhaustive5 //1 data and behavior separate externally available interfaces6 //2 No permission settings7 8 structDate9 {Ten intYear ; One intmonth; A intDay ; - }; - the - voidInit (Date &d); - voidPrint (Date &d); - BOOLIsleapvear (Date &d);
//Data.cpp#include<iostream>#include"data.h"using namespacestd;voidInit (Date &d) {cin>>d.year; CIN>>D.month; CIN>>D.day;}voidPrint (Date &d) {cout<<"year="<<d.year<<"month="<<d.month<<"day="<<d.day<<Endl;}BOOLIsleapvear (Date &d) { if(d.year%4==0|| d.year% -!=0|| d.year% -==0) return true; Else return false;}
//Strut.cpp#include<iostream>#include"data.h"using namespacestd;//2017/1/14 C language-style encapsulation data is put together and passed to the behavior as a reference and pointerintMain () {Date D;//It's time to open up space .Init (d); Print (d); if(Isleapvear (d)) {cout<<"D.year"<<"is a leap year"<<Endl; }Elsecout<<"D.year"<<"is not a leap"<<Endl; return 1;}
And then look at the C + + class
//data.h#ifndef Date_h#defineDate_hnamespacespace{classdate{ Public: voidinit (); voidprint (); BOOLisleapvear (); intgetyear (); intgetday (); intgetmonth (); Private: intYear ; intmonth; intDay ; };}#endif
1//Data.cpp23 #include <iostream>4 #include"Data.h"5UsingNamespaceStd6NamespaceSpace7{8voidDate:: Init ()9{Ten cin>>YearCin>>MonthCin>>Day13}14IntDate::getyear ()15{16ReturnYear17}18IntDate:: GetMonth ()19{20ReturnMonth21st}22IntDate:: Getday ()23{24ReturnDay25}26boolDate:: Isleapvear ()27{28if (year%4==0| | year%100!=0| | year%400==0)29ReturnTrue;30Else31ReturnFalse; (): Print () {cout<<"year:" << year<<"month" <<month<<"Day" <<day<<Endl; Notoginseng }
1//Main.cpp23 #include <iostream>4 #include"Data.h"5UsingNamespaceStd6UsingNamespaceSpace7//2017/1/1489//1 increased permission control relative to C10//Private11//Public12//2 data and behavior together, for this class is open to the external provider interface1314//Declarations are implemented to separate151617//Class MM18//{19//Public20//void Init ();21st//};22//void Mm::init (); All two classes have init so you need a domain name23IntMain ()24{25Date D;26//D.year = 300;//Direct access to the default private modifications are common and can be accessed27D.init ();28D.isleapvear ();29D.print (); If(D.isleapvear ()) cout<<d.getyear () <<"isa leap year" < <Endl; () Else cout<<d.getyear () << "is nota leap" <<Endl; return 0; (+}
No need for all kinds of arguments .....
C-language struct and C + + class ambiguous