4.【拷贝构造函数与重载】

9d39ab32fa694ebb98708b752a3dd31d.png

 

 【没有明确提供拷贝构造函数的时候回调用默认的拷贝构造函数】

cfbae4889fcd41edb9dedbad06456d3b.png

#include <iostream>
#include <string.h>
using namespace std;
class Student
{
private:
    int number;
    string name;
public:
    Student(int nu, string na) :number(nu), name(na) {}
    Student() {}
    void show()
    {
        cout << "学生的学号是:" << number << "学生的姓名是:" << name << endl;
    }

};
int main()
{
    Student s(21032114, "李威涛");
    Student s1 = s;                   //   利用初始化的方法
    s1.show();
    cout << "***********************" << endl;
    Student s2(s);                      //    利用小括号的方法赋值
    s2.show();
    return 0;

}

294ed64dbde54bc1b8fcfb5b6d9344c8.png

 =============

【明确提供拷贝构造函数时】

#include <iostream>

#include <string.h>

using namespace std;

class Student

{

private:

 int number;

 string name;

public:

 Student(int nu, string na) :number(nu), name(na) {}

 Student() {}

 Student(const Student& s) //拷贝构造函数要用用引用 ,因为引用值传递形参改变实参也改变

 {

  number = s.number;

  name = s.name;

 }

 void show()

 {

  cout << "学生的学号是:" << number << "学生的姓名是:" << name << endl;

 }

};

int main()

{

 Student s(21032114, "李威涛");

 Student s1 = s; // 利用初始化的方法

 s1.show();

 cout << "***********************" << endl;          

 Student s2(s); // 利用小括号的方法赋值

 s2.show();

 return 0;

}

c55c641bfa584f809ae347587109d642.png

 =============

【拷贝构造函数与重载运算符函数】

(为什么要返回对象名,实际上是在用拷贝构造函数)

#include <iostream>
using namespace std;
class Complex
{
private:
    double real;
    double imag;
public:
    Complex(double r, double i) :real(r), imag(i) { cout << "调用了构造函数" << endl; }
    Complex() {}
    Complex(const Complex& c)
    {
        real = c.real;
        imag = c.imag;
        cout << "*************调用了拷贝构造函数:************" << endl;
    }
    Complex operator+(Complex& c)
    {
        cout << "*************调用了重载运算符**********" << endl;
        Complex c1;
        c1.real = real + c.real;     //    返回对象的实部=原实部+引用实部;
        c1.imag = imag + c.imag;
        return c1;                    //返回对象名,其实是想用拷贝函数
    }
    void show()
    {
        cout << "加到一起是:" << real << "+" << imag << "i" << endl;
    }
};
int main()
{
    Complex c(2, 3), c1(c);
    c1.show();
    Complex c2(3, 4), c3(2, 5),c4;
    c4 = c2.operator+(c3);          //  实际上为: c4=c1(real,imag);
    
    c4.show();
}

8c3538d8947e4399a189bfbcf359f9c8.png

 978b16d459cd40bf81cbe5ab2150a2df.png

=============

 【在重载运算符的时候,如果不调用新的对象,要返回*this】

74520cbbdb034dcc87d47d3b5b04e116.png

 #include <iostream>

using namespace std;

class Complex

{

private:

 double real;

 double imag;

public:

 Complex(double r, double i) :real(r), imag(i) { cout << "调用了构造函数" << endl; }

 Complex() {}

 Complex(const Complex& c)

 {

  real = c.real;

  imag = c.imag;

  cout << "*************调用了拷贝构造函数:************" << endl;

 }

 Complex operator=(Complex& c)

 {

  cout << "*************调用了重载运算符**********" << endl;

  

   real = c.real; // 返回对象的实部=原实部+引用实部;

    imag = c.imag;

    return *this; //临时变量后面会消失

 }

 void show()

 {

  cout << "加到一起是:" << real << "+" << imag << "i" << endl;

 }

};

int main()

{

 Complex c(2, 3), c1(c);

 c1.show();

 Complex c2(3, 4), c3(2, 5),c4;

  c2.operator=(c3); //  

  c2.show();

}

bd8ebcd33d0e46538ff9f7fc3cd255d5.png

 b7935f3b42c74747908d01a4d055c2c3.png

=============

【注意事项】

2c745b9e890e4d108446efab20a2c211.png

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吉士先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值