继承关系中的拷贝构造函数和赋值操作重载函数分析

本文深入分析了在继承关系中,拷贝构造函数和赋值操作重载函数的调用机制。当子类未实现这些函数时,会调用父类的相关函数。若父类函数为私有,则会导致编译失败。文章通过具体代码示例展示了如何通过子类实现这些函数来控制对象的拷贝和赋值行为。

1 继承关系中的拷贝构造函数和赋值操作重载函数分析

在继承关系中,如果子类未实现拷贝构造函数,那么在子类进行拷贝构造操作时,会直接调用父类的拷贝构造函数,如果父类的拷贝构造函数是私有的,则会编译失败。我们有时候也会故意把父类的拷贝构造函数设置为私有的,从而禁止子类的拷贝构造操作。

在继承关系中,如果子类未实现赋值操作重载函数,那么在子类进行赋值操作时,会直接调用父类的赋值操作重载函数,如果父类的赋值操作重载函数是私有的,则会编译失败。我们有时候也会故意把父类的赋值操作重载函数设置为私有的,从而禁止子类进行赋值操作。

如下代码:

class A
{
public:
    A()
    {
        cout << "A()" << endl;
    }

    A(const A& obj)
    {
        cout << "A(const A& obj)" << endl;
    }

    A& operator = (const A& obj)
    {
        cout << "A& operator = (const A& obj)" << endl;
        return *this;
    }
};

class B : public A
{

};



int main()
{
    B b1;
    B b2(b1);
    b1 = b2;

	return 0;
}

输出结果:
在这里插入图片描述

一旦子类实现了拷贝构造函数,则在拷贝构造的时候会调用子类的拷贝构造函数和父类的构造函数。子类实现了赋值操作重载函数,则只会调用子类的。

代码如下:

class A
{
public:
    A()
    {
        cout << "A()" << endl;
    }

    A(const A& obj)
    {
        cout << "A(const A& obj)" << endl;
    }

    A& operator = (const A& obj)
    {
        cout << "A& operator = (const A& obj)" << endl;
        return *this;
    }
};

class B : public A
{
public:
    B()
    {
        cout << "B()" << endl;
    }

    B(const B& obj)
    {
        cout << "B(const B& obj)" << endl;
    }

    B& operator = (const B& obj)
    {
        cout << "B& operator = (const B& obj)" << endl;
        return *this;
    }
};



int main()
{
    B b1;
    B b2(b1);
    b1 = b2;

	return 0;
}

执行结果如下:
在这里插入图片描述

```cpp #include <iostream> // CShape 基类 class CShape { public: virtual double Area() const = 0; virtual double Length() const = 0; virtual ~CShape() {} // 拷贝构造函数 CShape(const CShape& other) = default; // 重载赋值运算符 CShape& operator=(const CShape& other) = default; }; // CRectangle 类,继承自 CShape class CRectangle : public CShape { private: double length; double width; public: CRectangle(double l, double w) : length(l), width(w) {} // 拷贝构造函数 CRectangle(const CRectangle& other) : length(other.length), width(other.width) {} // 重载赋值运算符 CRectangle& operator=(const CRectangle& other) { if (this != &other) { length = other.length; width = other.width; } return *this; } double Area() const override { return length * width; } double Length() const override { return 2 * (length + width); } }; // CCuboid 类,继承自 CRectangle class CCuboid : public CRectangle { private: double height; public: CCuboid(double l, double w, double h) : CRectangle(l, w), height(h) {} // 拷贝构造函数 CCuboid(const CCuboid& other) : CRectangle(other), height(other.height) {} // 重载赋值运算符 CCuboid& operator=(const CCuboid& other) { if (this != &other) { CRectangle::operator=(other); height = other.height; } return *this; } double Area() const override { double baseArea = CRectangle::Area(); double sideArea1 = CRectangle::Length() / 2 * height; return 2 * baseArea + sideArea1; } double Length() const override { return 4 * (CRectangle::Length() / 2 + height); } }; // Display 函数 void Display(const CShape& shape) { std::cout << shape.Area() << "," << shape.Length() << std::endl; } ``` 测试程序如下: ```cpp int main() { CRectangle liv_Rect(4,8); CCuboid liv_Cub(4,4,8); std::cout << liv_Rect.Area() << "," << liv_Rect.Length() << std::endl; std::cout << liv_Cub.Area() << "," << liv_Cub.Length() << std::endl; Display(liv_Rect); Display(liv_Cub); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值