为了熟练类而写的一个程序
类很重要,所以需要熟练程序代码:
#include<iostream> using namespace std; class Date { private: int month; int day; int year; public: Date(int a,int b,int c) { setMonth(a ); setDay(b); setYear(c ); } void setMonth(int a) { if(a>=1&&a<=12) month=a; else month=0; } void setDay(int b) { day=b; } void setYear(int c) { year=c; } int getMonth() { return month; } int getDay() { return day; } int getYear() { return year; } void displayDate() { cout<<"现在的时间是: "<<getMonth()<<"/"<<getDay()<<"/"<<getYear()<<endl; } }; int main() { int month1,day1,year1; cout<<"请输入现在的月份:"; cin>>month1; cout<<endl; cout<<"请输入现在的日子:"; cin>>day1; cout<<endl; cout<<"请输入现在的年份:"; cin>>year1; cout<<endl; Date nian1(month1,day1,year1); nian1. displayDate(); return 0; }