- c++第三章 类与对象基本知识例题
- 例3.3CDate类的成员函数中类的实现
- 例3.6this指针的作用示例
- 例3.7带参构造函数
- 例3.8带默认参数值的构造函数
- 例3.9复制构造函数自动调用示例
- 例3.10构造函数与析构函数的调用顺序
- 例3.11文件消息类的设计
- 例3.13对象数组的使用
- 例3.15动态对象及动态对象数组示例
- 例3.16对象引用的使用示例
#include
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
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
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
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
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;
}
例3.10构造函数与析构函数的调用顺序
#include
using namespace std;
class CDate
{
int date_year, date_month, date_day;
public:
CDate(int y = 2000, int m = 1, int d = 1);
void Display();
~CDate();
};
CDate::CDate(int y, int m, int d) :date_year(y),
date_month(m), date_day(d)//普通构造函数的定义
{
cout << "constructor :"<<date_year
<<"-"<<date_month<<"-"<<date_day<<endl;
}
CDate::~CDate()
{
cout << " Donstructor :" << date_year << "-"
<< date_month << "-" << date_day << endl;
}
void CDate::Display()
{
cout << date_year << "-" << date_month
<< "-" << date_day << endl;
}
int main()
{
CDate today;//定义一个对象
CDate newday(2022, 4, 7);//再定义一个对象
newday.Display();//调用函数输出
return 0;
}
例3.11文件消息类的设计
#include
#include
using namespace std;
class CMessage
{
private:
char* pmessage;//字符指针变量
public:
CMessage(const char* text = "中国一点也不能少!")//构造函数
{
pmessage = new char[strlen(text) + 1];//申请动态空间
strcpy_s(pmessage, strlen(text) + 1, text);
} //输出文本
void show()
{
cout << pmessage << endl;
} //析构函数
~CMessage()
{
cout << "Destructor called.\n"; //仅作为调用标记
delete[] pmessage; //释放动态空间
}
};
int main()
{
CMessage Mes1;
CMessage Mes2("爱我中华!"); //这里会调用构造函数
CMessage* pm = new CMessage("我爱我的祖国");
Mes1.show();
Mes2.show();
pm->show();
delete pm; //释放pm指向的内存,这里会调用析构函数
return 0;
};
例3.13对象数组的使用
#include
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;
}
int main()
{
CDate array[3] ={ CDate(2019,3,8),CDate(2019,3,11) };
for (int i = 0;i < 3;i++)
array[i].Display();
return 0;
}
constructor called.//调用构造函数初始化array[0]
constructor called.//调用构造函数初始化array[1]
constructor called.//调用构造函数初始化array[2]
2019-3-8//array[0].Display()产生的输出
2019-3-11//array[1].Display()产生的输出
2000-1-1//array[2].Display()产生的输出
例3.15动态对象及动态对象数组示例
#include
using namespace std;
class CDate
{
int date_year, date_month, date_day;
public:
CDate(int y = 2000, int m = 1, int d = 1);
void Display();
~CDate();
};
CDate::CDate(int y, int m, int d) :date_year(y),
date_month(m), date_day(d)//普通构造函数的定义
{
cout << "constructor called:" << endl;
}
CDate::~CDate()
{
cout << " Donstructor called:" << endl;
}
void CDate::Display()
{
cout << date_year << "-" << date_month
<< "-" << date_day << endl;
}
int main()
{//申请一个对象的动态空间,并将首地址赋值给对象指针q
CDate* q = new CDate(2019, 3, 9);
cout << "one dynamic object is:" << endl;
q->Display();
delete q;//释放q对象的动态空间,调用一次析构函数
q = new CDate[3];//生成动态一维对象数组,调用三次无参构造函数
q[0] = CDate(2019, 5, 1);//调用有参数构造函数,生成无名对象赋值给q[0]
q[1] = CDate(2019, 10, 1);//调用有参数构造函数,生成无名对象赋值给q[1]
for (int i = 0;i < 3; i++)
q[i].Display();
delete[]q;//释放对象数组动态空间,调用3次析构函数
return 0;
}
例3.16对象引用的使用示例
#include
using namespace std;
class CDate
{
int date_year, date_month, date_day;
public:
CDate(int y = 2000, int m = 1, int d = 1);
void Display();
~CDate();
};
CDate::CDate(int y, int m, int d) :date_year(y),
date_month(m), date_day(d)//普通构造函数的定义
{
cout << "constructor called:" << endl;
}
CDate::~CDate()
{
cout << " Donstructor called:" << endl;
}
void CDate::Display()
{
cout << date_year << "-" << date_month
<< "-" << date_day << endl;
}
CDate DateA(2019, 3, 13), DateB(2019, 5, 1);
CDate& pDate = DateA;//引用pDate初始化为对象DateA的别名
void f()//用于输出对象DateA、DateB和引用pDate的值
{
DateA.Display();
DateB.Display();
pDate.Display();
}
int main()
{
cout << "original DateA,DateB,pDate:" << endl;
f();
pDate = DateB;//相当于DateA=DateB,pDate仍是DateA的别名
cout << "after pDate=DateBB,DateA,DateB,pDate:" << endl;
f();//此处输出的DateA、DateB、pDate值一定相等
pDate = CDate(2019, 10, 1);
//修改引用pDate=Date(2019,10,1),Date(2019,10,1);
cout << "after pDate=Date(2019,10,1),DateA,DateB,pDate:"
<< endl;
f();//此处输出的DateA和pDate值一定相等,而DateB保持原值
return 0;
}
输出结果
constructor called:
constructor called:
original DateA,DateB,pDate:
2019-3-13
2019-5-1
2019-3-13
after pDate=DateBB,DateA,DateB,pDate:
2019-5-1
2019-5-1
2019-5-1
constructor called:
Donstructor called:
after pDate=Date(2019,10,1),DateA,DateB,pDate:
2019-10-1
2019-5-1
2019-10-1
Donstructor called:
Donstructor called:
例3.17添加修改日期的函数ModifyDate
#include
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);
~CDate();
void Display();
void Modifydate(int, int, int);
};
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";
}
CDate::~CDate()
{
cout << " Donstructor called:" << endl;
}
void CDate::Display()
{
cout << date_year << "-" << date_month << "-" << date_day << endl;
}
CDate fun(CDate newdate1)
{
CDate newdate2(newdate1);
return newdate2;
}
void Fun(CDate DateVar)//普通函数Fun()的定义,参数为值形式参数
{
DateVar.Modifydate(2022, 4, 9);
DateVar.Display();
}
void CDate::Modifydate(int y, int m, int d)
//修改日期函数的定义
{
date_year = y;
date_month = m;
date_day = d;
}
int main()
{
CDate DateA;
DateA.Display();
Fun(DateA);//实参对象单向传值给形参对象,调用复制构造函数
cout << "after calling fun() DateA;";
DateA.Display();
return 0;
}
输出结果
constructor called.//调用构造函数生成实参对象
2000-1-1//输出实参对象的值
Copy Constructor called.//参数传递时调用复制构造函数
2022-4-9//形式参数对象的值被修改
Donstructor called://函数Fun()退出时,形式参数对象生命期结束
after calling fun() DateA;2000-1-1//输出实参对象生命的值未改变
Donstructor called://实参对象生命期结束,调用析构函数
```cpp
## 例3.18对象指针作为形式参数的使用示例
```cpp
#include
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);
~CDate();
void Display();
void ModifyDate(int, int, int);
};
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";
}
CDate::~CDate()
{
cout << " Donstructor called:" << endl;
}
void CDate::Display()
{
cout << date_year << "-" << date_month << "-" << date_day << endl;
}
void CDate::ModifyDate(int y, int m, int d)
{
date_year = y;
date_month = m;
date_day = d;
}
void Fun(CDate *pDate)//对象指针作为Fun()形式参数
{
pDate ->ModifyDate(2022, 4, 9);//修改
pDate->Display();
}
int main()
{
CDate DateA(2022);
DateA.Display();//输出对象的值
Fun(&DateA);//实参对象地址单向传给形参对象指针
cout << "after calling fun() DateA:";
DateA.Display();//实参对象的值已改变
return 0;
}
```cpp
输出结果
```c++
constructor called.//调用构造函数生成实参对象DateA
2022-1-1//输出实参对象的值
2022-4-9//输出对象DateA的值
after calling fun() DateA:2022-4-9//输出实参对象的值,已间接修改
Donstructor called:实参对象生命期结束
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)