封装,可以达到,对外提供接口,屏蔽数据,对内开放数据。
比如我们用struct 封装的类,即知其接口,又可以直接访问其内部数据,这样却没有达
到信息隐蔽的功效。而class 则提供了这样的功能,屏蔽内部数据,对外开放接口。
struct 中所有行为和属性都是public 的(默认)。C++中的class 可以指定行为和属性的
访问方式,默认为pirvate。
struct instance analysis1:
#include <iostream>
using namespace std;
struct Date
{
int year;
int month;
int day;
};
void init(Date &d)
{
cout<<"year,month,day:"<<endl;
cin>>d.year>>d.month>>d.day;
}
void print(Date & d)
{
cout<<"year month day"<<endl;
cout<<d.year<<":"<<d.month<<":"<<d.day<<endl;
}
bool isLeapYear(Date & d)
{
if((d.year%4==0&& d.year%100 != 0) || d.year%400 =&