今天主要介绍构造函数,拷贝构造函数,析构函数,const,赋值运算符的重载。难度由易到难,每个概念下都有相应的例子(代码段)。
统一包含的头文件:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
using namespace std;
完成对象的初始化工作:构造函数
构造函数名字与类名相同;编译器自动调用;在对象声明周期内只调用一次。
构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
构造函数特征如下:
- 函数名与类名相同。
- 无返回值。
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
普通类模板:
class Date {
public:
void SetDate(int year, int month, int day) {
_year = year;
_month = month;
_day = day;
}
void PrintDate() {
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main() {
Date d;
d.SetDate(2019,7,30);
d.PrintDate();
system("pause");
return 0;
}
转化为构造函数形式:
//日期类
class Date {
public:
//构造函数
Date(int year, int month, int day) {
_year = year;
_month = month;
_day = day;
cout << "Date::Date():" << this << endl;
}
void SetDate(int year, int month, int day) {
_year = year;
_month = month;
_day = day;
}
void PrintDate() {
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
void TestDate() {
Date d1(2019, 7, 30);
d1.PrintDate();
}
int main() {
int a;
int b = 10;
int c(20);
TestDate();
system("pause");
return 0;
}
运行结果如下:
注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
class Date
{
比特科技
public :
// 1.无参构造函数
Date ()
{}
// 2.带参构造函数
Date (int year, int month , int day )
{
_year = year ;
_month = month ;
_day = day ;
}
private :
int _year ;
int _month ;
int _day ;
};
void TestDate()
{
Date d1; // 调用无参构造函数
Date d2 (2015, 1 1); // 调用带参的构造函数
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
Date d3();
}
完成对象中资源清理工作:析构函数
~析构函数中没有参数 原因:死不带去
~ 相反符号
eg:Seqlist构造函数
~Seqlist析构函数
析构函数特征如下:
- 析构函数名是在类名前加上字符 ~。
- 无参数无返回值。
- 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
- 对象生命周期结束时,C++编译系统系统自动调用析构函数。
class A {
public:
A(int a,int b,int c) {
}
void PrintB() {}
~A { //析构函数
}
};
private:
};
void C() {}
A d(w,y,z);
d.PrintB();
int main() {
int a;
int b;
int c;
C();
system("pause");
return 0;
}
完成对象拷贝:拷贝构造函数:也是构造函数的一种重载的形式
默认拷贝构造函数:浅拷贝—将一个对象中内容原封不动的拷贝到另一个对象中
带&是拷贝构造函数 Date(const Date& d)
单参数(const类类型&)–>如果没有&,编译器编译失败,导致无限递归
**
**
拷贝构造函数特征: 拷贝构造函数也是特殊的成员函数,其特征如下:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
- 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。(只有默认值拷贝,如Date,没有值的无法完成默认拷贝构造,如String)
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1);
return 0; }
class Date {
public:
//构造函数
Date(int year=1900, int month=1, int day=1) {
_year = year;
_month = month;
_day = day;
}
//拷贝构造函数
Date(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
cout << "Date(Date&):" << endl;
}
//构造函数
Date(Date* p) {
}
void PrintDate() {
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
void TestDate() {
Date d3;
Date d1(2019, 7, 30);
Date d2(d1); //调用拷贝构造函数
d3 = d1;
}
int main() {
TestDate();
system("pause");
return 0;
}
浅拷贝:两个对象共用同一份地址
若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷
贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
// 这里d2调用的默认拷贝构造完成拷贝,d2和d1的值也是一样的。
Date d2(d1);
return 0; }
下面这个例子包含:全缺省构造函数;拷贝构造函数;运算符的重载。
//构造函数重载
class Date {
public:
//全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1) {
_year = year;
_month = month;
_day = day;
}
//拷贝构造函数
Date(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
cout << "Date(Date&):" << endl;
}
void PrintDate() {
cout << _year << "-" << _month << "-" << _day << endl;
}
/*bool IsSame(const Date& d) {
return _year == d._year &&
_month == d._month &&
_day == d._day;
}*/
//bool operator==(const Date& left, const Date& right) {
// return left_year == right._year &&
// left_month == right._month &&
// left_day == right._day;
//}
//运算符的重载
bool operator==(const Date& d){
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
bool operator!=(const Date& d){
return != (*this == d);
}
private:
int _year;
int _month;
int _day;
};
int main() {
int a = 10;
int b = 10;
Date d1(2019, 6, 4);
Date d2(2019, 6, 4);
if (d1 == d2) {
//*if (d1.IsSame(d2)) {
cout << "同年同月同日生"<<endl;
}
}