当前位置:网站首页>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();
}
=============
【注意事项】
边栏推荐
猜你喜欢
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
STM32的CAN过滤器
CLion用于STM32开发
3.爬虫之Scrapy框架1安装与使用
对数字化时代的企业来说,数据治理难做,但应该去做
C#高级--委托
The Selenium IDE of the Selenium test automation
动作捕捉系统用于柔性机械臂的末端定位控制
技能大赛训练题:交换机的远程管理
随机推荐
hyperf的启动源码分析(二)——请求如何到达控制器
49.【拷贝构造函数与重载】
MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!
拥塞控制,CDN,端到端
机器学习模型验证:被低估的重要一环
ADS communicate with c #
C# using NumericUpDown control
Linux bash: redis-server: 未找到命令
Nuget打包并上传教程
STM32的CAN过滤器
Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
Introduction to the PartImageNet Semantic Part Segmentation dataset
VU 非父子组件通信
推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】
Open Inventor 10.12 Major Improvements - Harmony Edition
Miller_Rabin Miller Rabin probability sieve [template]
C# control ListView usage
MySQL 23 classic interviews hang the interviewer
For enterprises in the digital age, data governance is difficult, but it should be done
The Selenium IDE of the Selenium test automation