話很少說,先上代碼ios
#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date():year(1),month(1){day=3;};
Date(int t1,int t2=5,int t3=5);
void print();
};
Date::Date(int s1,int s2,int s3){
year=s1;
month=s2;
day=s3;
}
void Date::print()
{
cout<<year<<" "<<month<<" "<<day<<endl;
}
int main()
{
Date w1;
Date w2(1);
Date w3(1,2);
Date w4(1,2,3);
w1.print();
w2.print();
w3.print();
w4.print();
return 0;
}
函數
輸出結果:spa
1 1 3ip
1 5 5string
1 2 5io
1 2 3class
我感受我已經不適合打代碼了,把輸出函數打成了 cout<<year<<" "<<month<<" "<<year<<endl;stream
我還擱那傻了吧唧的調。。。構造函數
在函數重載的時候有個地方須要注意,若是要初始化三個參數,則必須將Date();這個函數去掉,由於對於所有指定了默認值的構造函數,不能再定義其餘參數類型與之相同的重載構造函數。co
#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class Date
{
private:
int year,month,day;
public:
//Date():year(1),month(1){day=3;};
Date(int t1=1,int t2=5,int t3=5);
void print();
};
Date::Date(int s1,int s2,int s3){
year=s1;
month=s2;
day=s3;
}
void Date::print()
{
cout<<year<<" "<<month<<" "<<day<<endl;
}
int main()
{
Date w1;
Date w2(1);
Date w3(1,2);
Date w4(1,2,3);
w1.print();
w2.print();
w3.print();
w4.print();
return 0;
}
輸出結果:
1 5 5
1 5 5
1 2 5
1 2 3