标题:啥时候洗脑?
关键词:重载构造函数
内容
A. 一句话概括今日目标完成情况
(a)复习前面学的 100%
(b)学习10.2 90%
B.具体内容
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date():year(1900),month(1),day(1){}; //无参构造函数
Date(int yy,int mm=1,int dd=1); //默认参数构造函数
Date(Date &d):year(d.year),month(d.month),day(d.day){};
Date(char *ps);
void print_ymd();
};
Date::Date(int yy,int mm,int dd):year(1900),month(1),day(1)
{
if(yy>=1900&&yy<=9999){ //这段还是有效性检验
year=yy;
}
else{
return;
}
if(mm>=1&&mm<=12){
month=mm;
}
else{
year=1900;
return;
}
if(dd>=1&&dd<=31){
day=dd;
}
else{
year=1900;
month=1;
return;
}
}
Date::Date(char *ps):year(1900),month(1),day(1)
{
char py[5],pm[3],pd[3];
strncpy(py,ps,4);
ps=ps+5;
strncpy(pm,ps,2);
ps=ps+3;
strncpy(pd,ps,2);
int yy=atoi(py),mm=atoi(pm),dd=atoi(pd);
if(yy>=1900&yy<9999){
year=yy;
}
else{
return;
}
if(mm>=1&&mm<=12){
month=mm;
}
else{
year=1900;
return;
}
if(dd>=1&&dd<=31){
day=dd;
}
else{
year=1900;
month=1;
return;
}
}
void Date::print_ymd()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
int main()
{
Date date1;
cout<<"date1:";
date1.print_ymd();
system("pause");
Date date2(2006);
cout<<"date2:";
date2.print_ymd();
system("pause");
Date date3(2006,4);
cout<<"date3;";
date3.print_ymd();
system("pause");
Date date4(2006,4,8);
cout<<"date4:";
date4.print_ymd();
system("pause");
Date date5(2006,14,8);
cout<<"date5:";
date5.print_ymd();
system("pause");
Date date6(date4);
cout<<"date6:";
date6.print_ymd();
system("pause");
Date date7("2008-08-08");
cout<<"date7:";
date7.print_ymd();
system("pause");
cout<<endl;
cout<<"Thanks for using!";
return 0;
}
哎,课太多了,只有晚上有一点空看看10.2.
老师讲的基本理解了,就是昨天学了构造函数的定义,而今天是重载构造函数。感觉这节比较简单……也不知道要说点什么。。。啊
看了看明天学的是析构函数,明天晚上的公选课翘了吧...真没意思。。。
C.明日计划
10.3、10.4搞定