当前位置:网站首页>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();
}
=============
【注意事项】
边栏推荐
- Golang - gin - pprof - use and safety
- ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
- Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
- 4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
- 战略进攻能力的重要性,要远远高于战略防守能力
- 中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
- ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
- C# control StatusStrip use
- IDEA的database使用教程(使用mysql数据库)
- /run/NetworkManager占用空间过大
猜你喜欢
随机推荐
Error IDEA Terminated with exit code 1
代码随想录笔记_哈希_454四数相加II
/run/NetworkManager占用空间过大
networkx绘制度分布
go中select语句
清除浮动的四种方式及其原理理解
Spark Learning: Add Custom Optimization Rules for Spark Sql
计算机复试面试问题(计算机面试常见问题)
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
如何使用StarUML画类图[通俗易懂]
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
C#控件CheckBox的使用
深入浅出边缘云 | 4. 生命周期管理
Sliding window method to segment data
CentOS7 - yum install mysql
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
查看Oracle数据库的用户名和密码
IDEA的database使用教程(使用mysql数据库)
百度网盘安装在c盘显示系统权限限制的解决方法
FastAPI encapsulates a generic response