当前位置:网站首页>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();
}
=============
【注意事项】
边栏推荐
猜你喜欢
IDEA找不到Database解决方法
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Optimization of five data submission methods
C#使用NumericUpDown控件
代码随想录笔记_哈希_454四数相加II
Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details
C# 中的Async 和 Await 的用法详解
3.爬虫之Scrapy框架1安装与使用
报错:npm ERR code EPERM
百度网盘安装在c盘显示系统权限限制的解决方法
随机推荐
networkx绘制度分布
基于神经网络的多柔性梁耦合结构振动控制
C# List用法 List介绍
Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
STM32的CAN过滤器
Four ways to clear the float and its principle understanding
[CPU Design Practice] Simple Pipeline CPU Design
Verilog——基于FPGA的贪吃蛇游戏(VGA显示)
CentOS7 - yum install mysql
sqlalchemy determines whether a field of type array has at least one consistent data with an array
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
go使用makefile脚本编译应用
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
技能大赛训练题:ftp 服务攻防与加固
STM32——软件SPI控制AD7705[通俗易懂]
JSP response对象简介说明
matlab as(assert dominance)
Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
机器学习模型验证:被低估的重要一环
Edge Cloud Explained in Simple Depth | 4. Lifecycle Management