C++笔记:关于类2

class和struct的区别
在C++中二者的区别只是默认访问权限不同:class成员的默认访问权限是private,而struct是public;在C中,除上述区别外,struct内没有成员函数(成员方法)
慢慢来,会好的

#include<iostream>
using namespace std;
class MyDate
{public://如果不写public就会默认为private
	void InitMyDate()
	{
		cout << "year	" << "month	" << "day	" << endl;
		cin >> year;
		cin >> month;
		cin >> day;
	}
	bool IsLeapYear()
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			return true;
		else
			return false;
	}
	void PrintMyDate()
	{
		cout << year <<"年"<< month<<"月" << day <<"日"<< endl;
	}
	int GetYear()//为了获得year,可以使用此公开成员函数
	{
		return year;
	}
private:
	int year;
	int month;
	int day;
};
int main()
{
	MyDate date1;
	date1.InitMyDate();
	date1.PrintMyDate();
	if (date1.IsLeapYear() == 1)
		cout <<date1.GetYear()<< "是闰年" << endl;//date1.year是不对的,year是私有成员,此处已经是类外
	else
		cout << date1.GetYear() <<"不是闰年" << endl;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值