3-3CDate类的成员函数中类的实现
#include<iostream>
using namespace std;
class CDate
{
private://private可以缺省,默认为私有
int Date_Year, Date_Month, Date_Day;
public:
void SetDate(int year, int month, int day)//对数据成员初始化的公有成员函数
{
Date_Year = year;
Date_Month = month;
Date_Day = day;
}
void Display()//执行显示功能的公有成员函数
{
cout << Date_Year << "-" << Date_Month << "-" << Date_Day;
cout << endl;
}
int GetYear()//公有成员函数,提取Date_Year变量值
{
return Date_Year;
}
};
例3.6this指针的作用示例
#include<iostream>
using namespace std;
class CDate
{
private:
int Date_Year, Date_Month, Date_Day;
public:
void SetDate(int, int, int);
void Dispaly();
};
void CDate::SetDate(int y, int m, int d)
{
Date_Year = y;
Date_Month = m;
Date_Day = d;
}
void CDate::Dispaly()
{
cout << "调用该函数的对象的this指针是";
cout << this << endl; //输出当前主调对象的地址
cout << "当前对象Date_Year成员的起始地址:";
cout << &this->Date_Year << endl;
cout << "当前对象Date_Mouth成员的起始地址:";
cout << &this->Date_Month << endl;
cout << "y=" << this->Date_Year
<< ",m=" << this->Date_Month
<< endl; //输出this所指对象的数据成员值
}
int main()
{
CDate dateA, dateB;
dateA.SetDate(2022, 4, 3);//定义两个对象
dateB.SetDate(2002,1,29);
cout << "dateA地址:" << &dateA << endl;//输出对象dateA的地址
dateA.Dispaly();
cout << "dateB地址:" << &dateB << endl;//输出对象dateB的地址
dateB.Dispaly();
return 0;
}
例3.7带参构造函数
#include<iostream>
using namespace std;
class CDate
{
private:
int Date_Year, Date_Month, Date_Day;
public:
CDate(int, int, int);//构造函数原型声明
void Dispaly( );
};
CDate::CDate(int y, int m, int d)//在类外实现构造函数
{
cout << "Executing constructor...\n";//刻意加入的输出语句,用来体现构造函数由系统自动调用完成对数据成员的初始化
Date_Year = y;
Date_Month = m;
Date_Day = d;
}
void CDate::Dispaly()
{
cout << Date_Year << "-" << Date_Month << "-" << Date_Day << endl;
}
int main()
{
CDate today(2022, 4, 3);//定义对象同时完成初始化
cout << "Today is:";
today.Dispaly();
return 0;
}
例3.8带默认参数值的构造函数
#include<iostream>
using namespace std;
class CDate
{
private:
int Date_Year, Date_Month, Date_Day;
public:
CDate(int y = 2000, int m = 1, int d = 1);//带默认参数值的构造函数
void Display();
};
CDate::CDate(int y, int m, int d)//函数实现时不再提供默认参数值
{
cout << "Executing constructor..." << endl;
Date_Year = y;
Date_Month = m;
Date_Day = d;
}
void CDate::Display()
{
cout << Date_Year << "-" << Date_Month << "-" << Date_Day << endl;
}
int main()
{
CDate initiateday;//定义对象不提供实际参数,全采用默认值
CDate newday(2022);//只提供一个实际参数,其余两个采用默认值
CDate today(2022, 4, 3);//提供三个实际参数
cout << "Initiateday is:";
initiateday.Display();
cout << "Newday is:";
newday.Display();
return 0;
}
例3.9复制构造函数自动调用示例
结果
Executing constructor...
Executing constructor...
Executing constructor...
Initiateday is:2000-1-1
Newday is:2022-1-1
Today is:2022-4-3
可见被定义的数值改变,未被定义的为初使值
#include<iostream>
using namespace std;
class CDate
{
int date_year, date_month, date_day;
public:
CDate(int y = 2000,int m = 1, int d = 1);
CDate(const CDate& date);
void Display();
};
CDate::CDate(int y, int m, int d) :date_year(y), date_month(m), date_day(d)
{
cout << "constructor called.\n";
}
CDate::CDate(const CDate &date)
{
date_year = date.date_year;
date_month = date.date_month;
date_day = date.date_day + 1;
cout << "Copy Constructor called.\n";
}
void CDate::Display()
{
cout << date_year << "-" << date_month << "-" << date_day << endl;
}
CDate fun(CDate newdate1)
{
CDate newdate2(newdate1);
return newdate2;
}
int main()
{
CDate day1(2022, 4, 3);
CDate day3;
CDate day2(day1);
CDate day4 = day2;
day3 = day2;
day3 = fun(day2);
day3.Display();
return 0;
}