002类的构造函数与函数的默认参数
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;
class Time
{
public:
int Hour;
int Minute;
int Second;
public:
Time(int TemHour, int TemMinute, int TemSecond=30);
Time();
};
Time::Time(int TemHour, int TemMinute, int TemSecond)
{
Hour = TemHour;
Minute = TemMinute;
Second = TemSecond;
}
Time::Time()
{
Hour = 10;
Minute = 10;
Second = 10;
}
int functionSum(int a,int b=10)
{
return a + b;
}
int main(void)
{
Time myTime01(12, 34, 23);
Time myTime02=Time(12, 13, 45);
Time myTime03 = Time{ 13, 23, 45 };
Time myTime04{ 12, 14, 56 };
Time myTime05 = { 13, 13, 45 };
Time myTime07;
Time myTime08 = Time();
Time myTime09{};
Time myTime10={};
Time myTime20 = {12,12,12};
Time myTime21 = myTime20;
Time myTime22(myTime20);
Time myTime23{ myTime20 };
Time myTime24 = { myTime20 };
Time myTime25(12, 12);
int Number01 = functionSum(33,13);
int Number02 = functionSum(10);
system("pause");
return 0;
}
003隐式类型转换与explicit
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;
class Time
{
private:
int Hour;
int Minute;
int Second;
public:
explicit Time(int Hour, int Minute, int Second);
Time(int Hour);
};
Time::Time(int Hour, int Minute, int Second)
{
this->Hour = Hour;
this->Minute = Minute;
this->Second = Second;
}
Time::Time(int Hour)
{
this->Hour = Hour;
}
void functionTransform(Time TemTime)
{
return;
}
int main(void)
{
Time myTime01 = 12;
Time myTime04 = { 16 };
Time myTime02=(12, 23, 43, 54);
functionTransform(12);
Time myTime06(2, 12, 34);
Time myTime07=(2, 12, 34);
system("pause");
return 0;
}
004构造函数初始化列表
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;
class Time
{
private:
int Hour;
int Minute;
int Second;
public:
explicit Time(int Hour, int Minute, int Second);
Time(int Hour);
};
Time::Time(int TemHour, int TemMinute, int TemSecond):Hour(TemHour),Minute(TemMinute),Second(TemSecond)
{
}
Time::Time(int Hour)
{
this->Hour = Hour;
}
int main(void)
{
Time myTime01 = Time(12, 12, 12);
system("pause");
return 0;
}