当前位置:网站首页>49.【拷贝构造函数与重载】
49.【拷贝构造函数与重载】
2022-07-31 13:24:00 【李在奋斗……】

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

#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;
}

=============
【明确提供拷贝构造函数时】
#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;
}

=============
【拷贝构造函数与重载运算符函数】
(为什么要返回对象名,实际上是在用拷贝构造函数)
#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();
}


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

#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();
}


=============
【注意事项】

边栏推荐
- 基于高阶微分器的无模型滑模控制器及其在自动电压调节器中的应用
- 技能大赛训练题:域用户和组织单元的创建
- 六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
- golang中使用泛型
- The use of C# control CheckBox
- SAP message TK 248 solved
- A detailed explanation of the usage of Async and Await in C#
- CentOS7 installation MySQL graphic detailed tutorial
- 报错IDEA Terminated with exit code 1
- IDEA找不到Database解决方法
猜你喜欢

Golang - gin - pprof - use and safety

365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal

C#获得网卡信息 NetworkInterface IPInterfaceProperties

ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning

抓住金三银四的尾巴,解锁程序员面试《刷题神器》

技能大赛训练题:MS15_034漏洞验证与安全加固

pytorch gpu版本安装最新

csdn发文助手问题

Install the latest pytorch gpu version

endnote引用
随机推荐
Centos7 install mysql5.7
What should I do if selenium is reversed?
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
技能大赛训练题:MS15_034漏洞验证与安全加固
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
电脑重要文件很多,如何备份比较安全?
ECCV 2022 | 机器人的交互感知与物体操作
八大排序汇总及其稳定性
4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
golang八股文整理(持续搬运)
C# List用法 List介绍
The importance of strategic offensive capability is much higher than strategic defensive capability
查看Mysql数据库版本
Selenium自动化测试之Selenium IDE
SAP message TK 248 solved
报错IDEA Terminated with exit code 1
How to quickly split and merge cell data in Excel