当前位置:网站首页>49. The copy constructor and overloaded 】
49. The copy constructor and overloaded 】
2022-07-31 14:09:00 【Lee is struggling...】

【The default copy constructor is called when no explicit copy constructor is provided】

#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; // Use the initialization method
s1.show();
cout << "***********************" << endl;
Student s2(s); // Assign values using parentheses
s2.show();
return 0;
}

=============
【When explicitly providing a copy constructor】
#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) //The copy constructor uses a reference ,Because a pass-by-reference parameter changes, the actual parameter also changes
{
number = s.number;
name = s.name;
}
void show()
{
cout << "学生的学号是:" << number << "学生的姓名是:" << name << endl;
}
};
int main()
{
Student s(21032114, "李威涛");
Student s1 = s; // Use the initialization method
s1.show();
cout << "***********************" << endl;
Student s2(s); // Assign values using parentheses
s2.show();
return 0;
}

=============
【Copy constructor and overloaded operator functions】
(Why return the object name,It's actually using a copy constructor)
#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 << "*************An overloaded operator was called**********" << endl;
Complex c1;
c1.real = real + c.real; // Returns the real part of the object=original real department+Quote the real part;
c1.imag = imag + c.imag;
return c1; //返回对象名,In fact, I want to use the copy function
}
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();
}


=============
【when overloading operators,If not call new object,要返回*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 << "*************An overloaded operator was called**********" << endl;
real = c.real; // Returns the real part of the object=original real department+Quote the real part;
imag = c.imag;
return *this; //Temporary variables disappear later
}
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();
}


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

边栏推荐
- 五个维度着手MySQL的优化
- C# using ComboBox control
- The use of C# control CheckBox
- A detailed explanation of the usage of Async and Await in C#
- uniapp微信小程序引用标准版交易组件
- 线程池的使用二
- Linux bash: redis-server: command not found
- 技能大赛dhcp服务训练题
- 1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
- How to quickly split and merge cell data in Excel
猜你喜欢

232层3D闪存芯片来了:单片容量2TB,传输速度提高50%

Introduction to the PartImageNet Semantic Part Segmentation dataset

Motion capture system for end-positioning control of flexible manipulators

使用CompletableFuture进行异步处理业务

All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)

4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率

八大排序汇总及其稳定性

Open Inventor 10.12 重大改进--和谐版

Spark学习:为Spark Sql添加自定义优化规则

Redis 】 【 publish and subscribe message
随机推荐
Detailed explanation of network protocols and related technologies
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
爱可可AI前沿推介(7.31)
Shell script classic case: backup of files
SetoolKit使用指南
Text similarity calculation (Chinese and English) detailed explanation of actual combat
Shell script classic case: detecting whether a batch of hosts is alive
[QNX Hypervisor 2.2 User Manual] 9.13 rom
Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
Shell脚本经典案例:文件的备份
Use of C# Assembly
leetcode: 485. Maximum number of consecutive 1s
AWS实现定时任务-Lambda+EventBridge
技能大赛dhcp服务训练题
Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
【Pytorch】F.softmax()方法说明
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
最近很火的国产接口神器Apipost体验
C# control ToolStripProgressBar usage
技能大赛训练题: 子网掩码划分案例