当前位置:网站首页>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();
}


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

边栏推荐
- ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
- [CPU Design Practice] Simple Pipeline CPU Design
- IDEA连接MySQL数据库并执行SQL查询操作
- SAP message TK 248 solved
- Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
- 分布式锁有哪些,怎么实现(分布式锁的三种实现的对比)
- The operator,
- 3.爬虫之Scrapy框架1安装与使用
- JSP中如何借助response对象实现页面跳转呢?
- 报错IDEA Terminated with exit code 1
猜你喜欢

CLion用于STM32开发

NameNode (NN) and SecondaryNameNode (2NN) working mechanism

Architecture Camp | Module 8

IDEA的database使用教程(使用mysql数据库)

MATLAB | 我也做了一套绘图配色可视化模板

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

C#控件CheckBox的使用

PHP Serialization: eval

IDEA can't find the Database solution

golang八股文整理(持续搬运)
随机推荐
Optimization of five data submission methods
20.nn.Module
C# List Usage List Introduction
技能大赛训练题:MS15_034漏洞验证与安全加固
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
IDEA的database使用教程(使用mysql数据库)
The latest complete code: Incremental training using the word2vec pre-training model (two loading methods corresponding to two saving methods) applicable to various versions of gensim
C#控件 ToolStripProgressBar 用法
聊聊 SAP 产品 UI 上的消息显示机制
go中select语句
爱可可AI前沿推介(7.31)
0X7FFFFFFF,0X80000000「建议收藏」
PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
C# 中的Async 和 Await 的用法详解
A detailed explanation of the usage of Async and Await in C#
基于改进YOLOv5的轻量化航空目标检测方法
CentOS7 - yum install mysql
C#控件CheckBox的使用
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
Productivity Tools and Plugins