c++第三章类与对象基本知识例题(草稿)

本文介绍了C++中Date类的成员函数,包括公有SetDate, Display和GetYear函数,以及构造函数的不同形式,如带参构造、带默认参数值构造和复制构造。通过实例展示了这些概念在实际程序中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

` starmultiple `

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值