复制构造函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复制构造函数相关的知识,希望对你有一定的参考价值。

// 复制构造函数.cpp : 定义控制台应用程序的入口点。
//复制构造函数:类(const 类&对象(随便起))
/*
Box(const Box&box)
{
length = box.length;
width = box.width;
height = box.height;
}
*/

#include "stdafx.h"
#include<iostream>
using namespace std;
class Box
{
private:
    int length;
    int width;
    int height;
public:
    Box(int a, int b, int c);
    Box(const Box&box)
    {
        length = box.length;
        width = box.width;
        height = box.height;
    }
    void display()
    {
        cout << length*width*height << endl;
    }
   
};


Box::Box(int a, int b, int c)
{
    length = a;
    width = b;
    height = c;
    display();
}
int main()
{
    Box box1(1, 2, 3);
    Box box2 = box1;
     box2.display();
    system("pause");
    return 0;
}

技术分享















































以上是关于复制构造函数的主要内容,如果未能解决你的问题,请参考以下文章

复制构造函数(拷贝构造函数)

C++中复制构造函数被调用的三种情况

C++——构造函数析构函数以及复制构造函数

为啥我们需要复制构造函数以及何时应该在 java 中使用复制构造函数

复制构造函数的运用

为啥标准不将模板构造函数视为复制构造函数?