【C++】——运算符重载

简介: 【C++】——运算符重载

🎯第一题:

设计一个复数类Complex,包含成员变量实部real和虚部imag;运算符重载+/-实现两个复数加、减的功能。在主函数里创建两个复数对象,分别求两个复数的和与差,在主函数里显示运算结果。效果如图:

🎯 第二题:

现有一学生类定义:

  class Student{


        ……..


     private:


int _id;


        string _name;


        char *_addr;


   };


实现学生类的赋值运算符=重载,主函数调用赋值运算符并输出对象信息。

53067290fa96fc20abb3aa3b1f08d11a_ff94b4de96b7d563a22b3786630dc15f.png

🎯 第三题:

编写一个程序,声明一个2行2列矩阵类Matrix,重载运算符“+”,使之能用于矩阵的加法运算。重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入与输出。从键盘输入两个矩阵a和b,求两个矩阵之和并输出。效果如图:

83fc9c64f6b347d96d6021d1d773ef15_ed177dba8624ba13b2862da5e37ef3b9.png

🎯 答案:

💻第一题:

#include <iostream>
 
using namespace std;
 
class Complex {
 
private:
 
double real;
 
double imag;
 
public:
 
Complex(double x=0,double y=0):real(x),imag(y) {}
 
void show() const; //输出数据
 
friend Complex operator+(const Complex& a1, const Complex& a2) ; //重载为类的友元函数
 
friend Complex operator-(const Complex& a1, const Complex& a2);//重载为类的友元函数
 
};
 
/******************************************************************/
 
void Complex::show() const {
 
if(imag>=0)
 
cout<<real<<"+"<<imag<<"i"<<endl;
 
else
 
cout<<real<<imag<<"i"<<endl;
 
}
 
Complex operator+(const Complex& a1,const Complex& a2) {
 
return Complex(a1.real+a2.real,a1.imag+a2.imag);
 
}
 
Complex operator-(const Complex& a1,const Complex& a2) {
 
return Complex(a1.real-a2.real,a1.imag-a2.imag);
 
}
 
/******************************************************************/
 
int main() {
 
Complex c1(5.1,3.4);
 
Complex c2(3.6,5.3);
 
Complex c;
 
cout<<"c1:";
 
c1.show();
 
cout<<"c2:";
 
c2.show();
 
c=c1+c2;
 
cout<< "c1+c2:";
 
c.show();
 
c=c1-c2;
 
cout<<"c1-c2:";
 
c.show();
 
return 0;
 
}
 
 
 

💻第二题:

#include <iostream>
 
#include <cstring>
 
using namespace std;
 
class Student {
 
private:
 
int _id;
 
string _name;
 
char *_addr;
 
public:
 
Student(int id,string name,const char *addr);
 
Student();
 
Student& operator=(Student& stu);
 
void show();
 
};
 
Student::Student() {}
 
Student::Student(int id,string name,const char *addr) {
 
/******************************************/ 
 
_id=id;
 
_name=name;
 
this->_addr=new char[strlen(addr)+1];
 
if(_addr)     strcpy(_addr,addr);
 
/******************************************/ 
 
}
 
Student& Student::operator=(Student& stu) {
 
//注意深拷贝
 
/******************************************/ 
 
_id=stu._id;
 
_name=stu._name;
 
_addr=new char[strlen(stu._addr)+1];
 
if(_addr)     strcpy(_addr,stu._addr);
 
return *this;
 
/******************************************/ 
 
}
 
void Student::show() {
 
cout<<"学号:"<<_id<<endl;
 
cout<<"姓名:"<<_name<<endl;
 
cout<<"住址:"<<_addr<<endl;
 
}
 
int main() {
 
Student stu2,stu1(10001,"zhangsan","luoyang");
 
stu1.show();
 
stu2=stu1;
 
stu2.show();
 
return 0;
 
}
 

💻第三题:

#include <iostream>
 
#include <iomanip>
 
using namespace std;
 
class Matrix {
 
private:
 
int a[2][2];
 
friend istream& operator>>(istream& is,Matrix& _m);
 
friend ostream& operator<<(ostream& os,Matrix& _m);
 
friend Matrix  operator+(const Matrix& mat1,const Matrix& mat2);
 
};
 
ostream& operator<<(ostream& os,Matrix& _m) {
 
//每个数4列 setw(4)
 
for(int i=0; i<2; i++) {
 
for(int j=0; j<2; j++)
 
os<<setw(4)<<_m.a[i][j];
 
os<<endl;
 
}
 
return os;
 
}
 
/****************************************************/
 
istream& operator>>(istream& is,Matrix& _m) {
 
for(int i=0; i<2; i++)
 
for(int j=0; j<2; j++)
 
is>>_m.a[i][j];
 
return is;
 
}
 
Matrix  operator+(const Matrix& mat1,const Matrix& mat2) {
 
Matrix mat;
 
for(int i=0; i<2; i++)
 
for(int j=0; j<2; j++) {
 
mat.a[i][j]=mat1.a[i][j]+mat2.a[i][j];
 
}
 
return mat;
 
}
 
/****************************************************/
 
int main() {
 
Matrix m1,m2,m;
 
cout<<"input matrix m1:"<<endl;
 
cin>>m1;
 
cout<<"input matrix m2:"<<endl;
 
cin>>m2;
 
m=m1+m2;
 
cout<<"output matrix m:"<<endl;
 
cout<<m;
 
return 0;
 
}
 
 
 
目录
相关文章
|
1月前
|
编译器 C++
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
27 1
|
2月前
|
程序员 编译器 C++
C++中的运算符重载(Operator Overloading)
C++中的运算符重载(Operator Overloading)
33 1
|
11天前
|
存储 编译器 C++
【C++】:拷贝构造函数和赋值运算符重载
【C++】:拷贝构造函数和赋值运算符重载
11 1
|
24天前
|
C++ 索引
C++核心技术要点《运算符重载》
C++核心技术要点《运算符重载》
25 2
|
2天前
|
自然语言处理 程序员 C++
C++基础知识(五:运算符重载)
运算符重载是C++中的一项强大特性,它允许程序员为自定义类型(如类或结构体)重新定义标准运算符的行为,使得这些运算符能够适用于自定义类型的操作。这样做可以增强代码的可读性和表达力,使得代码更接近自然语言,同时保持了面向对象编程的封装性。
|
2天前
|
Java 程序员 C++
|
4天前
|
编译器 C++
【C++】详解运算符重载,赋值运算符重载,++运算符重载
【C++】详解运算符重载,赋值运算符重载,++运算符重载
|
7天前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
16天前
|
C++
c++进阶篇(一)——运算符重载
c++进阶篇(一)——运算符重载
|
2月前
|
编译器 C语言 C++
从C语言到C++⑤(第二章_类和对象_中篇)(6个默认成员函数+运算符重载+const成员)(下)
从C语言到C++⑤(第二章_类和对象_中篇)(6个默认成员函数+运算符重载+const成员)
13 1