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


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

边栏推荐
- Uniapp WeChat small application reference standard components
- 海康摄像机取流RTSP地址规则说明
- I summed up the bad MySQL interview questions
- 3.爬虫之Scrapy框架1安装与使用
- An article makes it clear!What is the difference and connection between database and data warehouse?
- go使用makefile脚本编译应用
- 机器学习模型验证:被低估的重要一环
- MySQL玩到这种程度,难怪大厂抢着要!
- Linux bash: redis-server: 未找到命令
- 1-hour live broadcast recruitment order: industry leaders share dry goods, and enterprise registration is open丨qubit · point of view
猜你喜欢

The Selenium IDE of the Selenium test automation

Nuget打包并上传教程

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

AWS实现定时任务-Lambda+EventBridge
![[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)](/img/a6/8d53f5087a33c2bea8c99a5bb922da.png)
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)

八大排序汇总及其稳定性

An article makes it clear!What is the difference and connection between database and data warehouse?

1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点

MySQL玩到这种程度,难怪大厂抢着要!

推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】
随机推荐
线程池的使用二
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
C# using ComboBox control
Sliding window method to segment data
ADS communicate with c #
Nuget打包并上传教程
AWS实现定时任务-Lambda+EventBridge
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
MySQL 23 classic interviews hang the interviewer
The Selenium IDE of the Selenium test automation
C# Get network card information NetworkInterface IPInterfaceProperties
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
uniapp微信小程序引用标准版交易组件
技能大赛训练题:MS15_034漏洞验证与安全加固
拥塞控制,CDN,端到端
hyperf的启动源码分析(二)——请求如何到达控制器
Solution for browser hijacking by hao360
最新完整代码:使用word2vec预训练模型进行增量训练(两种保存方式对应的两种加载方式)适用gensim各种版本
Detailed guide to compare two tables using natural full join in SQL
Tortoise speed by "template"