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


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

边栏推荐
猜你喜欢

The operator,

For enterprises in the digital age, data governance is difficult, but it should be done

The pre-sale of the new Hyundai Paristi is open, and safety and comfort are not lost

ECCV 2022 | Robotic Interaction Perception and Object Manipulation

VU 非父子组件通信

【蓝桥杯选拔赛真题46】Scratch磁铁游戏 少儿编程scratch蓝桥杯选拔赛真题讲解

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

五个维度着手MySQL的优化

The recently popular domestic interface artifact Apipost experience

多智能体协同控制研究中光学动作捕捉与UWB定位技术比较
随机推荐
Open Inventor 10.12 重大改进--和谐版
DELL SC compellent 康贝存储系统怎么抓取配置信息
爱可可AI前沿推介(7.31)
BigDecimal 简介,常用方法
对数字化时代的企业来说,数据治理难做,但应该去做
redhat/openssl生成自签ca证书并使用
五个维度着手MySQL的优化
Controller层代码这么写,简洁又优雅!
C# control ToolStripProgressBar usage
【Pytorch】torch.argmax()用法
csdn发文助手问题
[Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
【redis】发布和订阅消息
新款现代帕里斯帝预售开启,安全、舒适一个不落
Shell script classic case: detecting whether a batch of hosts is alive
49.【拷贝构造函数与重载】
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
LeetCode·每日一题·1161.最大层内元素和·层次遍历
Shell脚本经典案例:文件的备份
Usage of += in C#