#include<iostream>
using namespace std;
class CDate
{
public:
int Date_Year;
int Date_Month;
int Date_Day;
};
int main()
{
CDate date1;//定义CDate类对象date1、date2
CDate date2;
int age = 0;//定义年龄,初始值为0
date1.Date_Year = 2019;//给date1的公有数据成员赋值
date1.Date_Month = 3;
date1.Date_Day = 9;
date2.Date_Year = 1999;//給date2的公有数据成员赋值
date2.Date_Month = 3;
date2.Date_Day = 9;
age = date1.Date_Year - date2.Date_Year;//计算年龄
cout << "He is " << age << "years old." << endl;//输出生日
cout << "His birthday is "
<< date2.Date_Year
<< '-'
<< date2.Date_Month
<< '-'
<< date2.Date_Day
<< endl;
cout << "date1 occupies " << sizeof(date1) << " bytes.";
cout << endl;
return 0;
}
c++类对象公有数据成员访问
最新推荐文章于 2022-09-04 10:03:10 发布