C++类构造函数、拷贝构造函数最深理解

本文深入探讨了C++中构造函数、拷贝构造函数的概念及其应用。通过具体实例,详细解析了不同构造函数的调用过程及作用,尤其是初始化列表的重要性。

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

C++类构造函数、拷贝构造函数最深理解

用一道例题加深理解:

【问题描述】设计一个CRectangle类,其中包括CPoint类的两个对象成员,表示左上角和右下角的两个点。要求求解矩形的面积。

注意,每个类的构造函数、拷贝构造函数需要输出“*** is called”,具体的请根据输出进行分析。

(main函数已经给定)

【样例输出】

#Define p1######

CPoint contstructor with default value(0,0) is called.

#Define p2 ######

CPoint contstructor with default value(0,0) is called.

#Define rect1 ######

CPoint contstructor with default value(0,0) is called.

CPoint contstructor with default value(0,0) is called.

CRectangle default contstructor is called.

#Define rect2 ######

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CRectangle contstructor with (CPoint,CPoint) is called.

#Define rect3 ######

CPoint contstructor with default value(0,0) is called.

CPoint contstructor with default value(0,0) is called.

CRectangle contstructor with (int,int,int,int) is called.

#Define rect4 ######

CPoint copy contstructor is called.

CPoint copy contstructor is called.

CRectangle copy contstructor is called.

#Calculate area ######

rect1面积为0

rect2面积为200

rect3面积为50

rect4面积为200

从输出可以看出CPoint类有无形参构造函数、两个整型的构造函数和拷贝构造函数

    CPoint()
    {
        x = y = 0;
        cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
    }
    CPoint(int a,int b)
    {
        x = a;
        y = b;
        cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
    }
    CPoint(CPoint &p)
    {
        x = p.x;
        y = p.y;
        cout<<"CPoint copy contstructor is called."<<endl;
    }

CRectangle类有4个构造函数:1、默认构造函数;2、形参为CPoint类的构造函数;3、形参为三个整型的构造函数;4、拷贝构造函数

    CRectangle()
    {
        cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
    }
    CRectangle(CPoint a,CPoint b):p1(a),p2(b)//形参的传递,此处可以调用四次CPoint拷贝构造函数
    {
        cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
    }
    CRectangle(int a,int b,int c,int d):p1(a,b),p2(c,d)//可以用这样的方式再次调用CPoint的构造函数
    {
        cout<<"CRectangle contstructor with (int,int,int,int) is called."<<endl;
    }
    CRectangle(CRectangle &p):p1(p.p1),p2(p.p2)
    {
        cout<<"CRectangle copy contstructor is called."<<endl;
    }

【强调】
调用CPoint拷贝构造函数时,使用列表初始化。
格式为:

CRectangle(CPoint a,CPoint b):p1(a),p2(b)CRectangle(CRectangle &p):p1(p.p1),p2(p.p2)

不能在函数体内用

CRectangle(CPoint a,CPoint b)
{
	p1 = a;
	p2 = b;
}

可以看一下运行结果
在这里插入图片描述
我们发现函数调用了构造函数,和我们的预想完全不一样,因为我们希望调用四次拷贝构造函数,这就是初始化列表的作用。

——————————下面上代码——————————

#include <iostream>
using namespace std;
class CPoint
{
public:
    CPoint()
    {
        x = y = 0;
        cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
    }
    CPoint(int a,int b)
    {
        x = a;
        y = b;
        cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
    }
    CPoint(CPoint &p)
    {
        x = p.x;
        y = p.y;
        cout<<"CPoint copy contstructor is called."<<endl;
    }
    int Get_x(){return x;}
    int Get_y(){return y;}
private:
    int x,y;
};
class CRectangle
{
public:
    CRectangle()
    {
        cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
    }
    CRectangle(CPoint a,CPoint b):p1(a),p2(b)
    {
        cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
    }
    CRectangle(int a,int b,int c,int d):p1(a,b),p2(c,d)//可以用这样的方式再次调用CPoint的构造函数
    {
        cout<<"CRectangle contstructor with (int,int,int,int) is called."<<endl;
    }
    CRectangle(CRectangle &p):p1(p.p1),p2(p.p2)
    {
        cout<<"CRectangle copy contstructor is called."<<endl;
    }
    int GetArea()
    {
        int d = p1.Get_x()-p2.Get_x();
        int l = p1.Get_y()-p2.Get_y();
        return d*l;
    }
private:
    CPoint p1,p2;
};
int main()
{
    int a=1, b=1, c=6, d=11;
    cout<<"# Define p1 ######"<<endl;
    CPoint p1;
    cout<<"# Define p2 ######"<<endl;
    CPoint p2(10,20);
    cout<<"# Define rect1 ######"<<endl;
    CRectangle rect1;
    cout<<"# Define rect2 ######"<<endl;
    CRectangle rect2(p1, p2);
    cout<<"# Define rect3 ######"<<endl;
    CRectangle rect3(a, b, c, d);
    cout<<"# Define rect4 ######"<<endl;
    CRectangle rect4(rect2);
    cout<<"# Calculate area ######"<<endl;
    cout << "rect1面积为" << rect1.GetArea() << endl;
    cout << "rect2面积为" << rect2.GetArea() << endl;
    cout << "rect3面积为" << rect3.GetArea() << endl;
    cout << "rect4面积为" << rect4.GetArea() << endl;
    system("pause");
    return 0;
}

这个代码是我经过完善后的最终版本,下面贴最初的版本,带大家一起看看优劣 (zz方案)。

#include <iostream>
using namespace std;
//请在这里补充CPoint,CRectangle类的定义
class CPoint
{
public:
    CPoint(int a=0,int b=0)
    {
        x=a;
        y=b;
        cout<<"CPoint contstructor with default value(0,0) is called."<<endl;
    }
    CPoint(CPoint &p)
    {
        x=p.x;
        y=p.y;
        cout<<"CPoint copy contstructor is called."<<endl;
    }
    int x,y;     //没有放在私有成员private里
};
class CRectangle
{
public:
    CRectangle()
    {
        x1=y1=x2=y2=0;
        cout<<"CRectangle default contstructor is called."<<endl;
    }
    CRectangle(CPoint a,CPoint b):p1(a),p2(b)
    {
        x1=p1.x;
        y1=p1.y;
        x2=p2.x;
        y2=p2.y;
        cout<<"CRectangle contstructor with (CPoint,CPoint) is called."<<endl;
    }
    CRectangle(int a,int b,int c,int d)
    {
        x1=a;
        y1=b;
        x2=c;
        y2=d;
        cout<<"CRectangle contstructor with (int,int,int,int) is called."<<endl;
    }
    CRectangle(CRectangle &p):p1(p.p1),p2(p.p2)
    {
        x1=p1.x;
        y1=p1.y;
        x2=p2.x;
        y2=p2.y;
        cout<<"CRectangle copy contstructor is called."<<endl;
    }
    int GetArea();
private:
    CPoint p1,p2;
    int x1,y1,x2,y2;
};
int CRectangle::GetArea()
{
    int l,d;
    l=x2-x1;
    d=y2-y1;
    return (l*d);
}


int main()
{
    int a=1, b=1, c=6, d=11;
    cout<<"# Define p1 ######"<<endl;
    CPoint p1;
    cout<<"# Define p2 ######"<<endl;
    CPoint p2(10,20);
    cout<<"# Define rect1 ######"<<endl;
    CRectangle rect1;
    cout<<"# Define rect2 ######"<<endl;
    CRectangle rect2(p1, p2);
    cout<<"# Define rect3 ######"<<endl;
    CRectangle rect3(a, b, c, d);
    cout<<"# Define rect4 ######"<<endl;
    CRectangle rect4(rect2);
    cout<<"# Calculate area ######"<<endl;
    cout << "rect1面积为" << rect1.GetArea() << endl;
    cout << "rect2面积为" << rect2.GetArea() << endl;
    cout << "rect3面积为" << rect3.GetArea() << endl;
    cout << "rect4面积为" << rect4.GetArea() << endl;
    system("pause");
    return 0;
}

在此代码里,我在CRectangle类中定义的private变量是四个点的坐标。相应地,为了得到X1、2,Y1、2的具体值,我们只能把CPoint中的x、y设置在public中,因为一旦设置为私有变量,在类外就无法得到了。
对比来看,经过更改后的代码更能将类构造、拷贝构造函数淋漓尽致展示出来。
最后的最后---->>>贴一个运行结果
在这里插入图片描述

                  希望能帮助到你yo~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值