当前位置: 首页 > news >正文

C++中,运算符重载,+,-,*,/,=,+=,[]的使用

1.使用运算符重载,完成复数运算

复数四则运算法则如下:

具体代码如下 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;                 

class Complex{
private:
		double a;
		double b;
public:
		Complex(double a,double b)
			:a(a),b(b)
		{
			this->a=a;
			this->b=b;
		}

		 double geta()const{return a;}
		 double getb()const{return b;}
	
		void show()
		{
			cout<<a<< ((b>=0)?("+"):("-")) <<abs(b)<<"i"<<endl;
		}


};

Complex operator+(const Complex& c1,const Complex& c2)
{
	Complex c3(c1.geta()+c2.geta(),c1.getb()+c2.getb());
	return c3;
}


Complex operator-(const Complex& c1,const Complex& c2)
{
	Complex c3(c1.geta()-c2.geta(),c1.getb()-c2.getb());
	return c3;
}


Complex operator*(const Complex& c1,const Complex& c2)
{
	 Complex c5(c1.geta()*c2.geta()-c1.getb()*c2.getb(),
	 			c1.getb()*c2.geta()+c1.geta()*c2.getb());
	return c5;
}


Complex operator/(const Complex& c1,const Complex& c2)
{
	double ob=c2.geta()*c2.geta()+c2.getb()*c2.getb();
	if(ob==0)
	{
		cout<<"不存在"<<endl;
		return Complex(0,0);
	}
	else
	{
		Complex c6(
		(c1.geta()*c2.geta()+c1.getb()*c2.getb())/ob,
		(c1.getb()*c2.geta()-c1.geta()*c2.getb())/ob);
		return c6;
	}
}

int main(int argc,const char** argv){

	Complex c1(1,2);
	Complex c2(3,-4);	
	cout<<"   c1= ";c1.show();
	cout<<"   c2= ";c2.show();
	
	Complex c3=c1+c2;
	cout<<"c1+c2= ";c3.show();
	Complex c4=c1-c2;
	cout<<"c1-c2= ";c4.show();

	Complex c5=c1*c2;
	cout<<"c1*c2= ";c5.show();
	Complex c6=c1/c2;
	cout<<"c1/c2= ";c6.show();
}

运行截图;

 

2. +,=,+=,[]的使用

其中,=,+=,[] 需要在类中声明,缘由是他们是等待赋值,

2.1以 = 赋值为例:

mystring&  operator=(const mystring& other);

我们知道,在这里隐藏一个this指针,指向它原本的对象,这是我们就有,this指向的对象,和other两个对象

mystring&  mystring::operator=(const mystring& other)
{
    if(this==&other)
    {
        return *this;    
    }

    delete[] str;
    str=(char*)malloc(other.getlen()+1);
    strcpy(str,other.getstr());
    this->len=other.getlen();
    return *this;

}

我们把other对象所对应的值赋给this,所指向的对象就可以完成赋值

2.2再以 [] 下标为例:

char& operator[](int i);   实现声明

char& mystring::operator[](int i){

        return this->str[i];//通过访问需要修改的下表的引用
}

str[0]='H';//这地方的本质为, this->str[0]=''H;精准通过下表进行修改。

具体代码如下:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;                 

class mystring{
private:
	char* str;
	int len;
public:
	mystring(const char* s);
	~mystring();
	void show();

	char* getstr()const{return str;}
	int getlen()const{return len;}
	mystring&  operator=(const mystring& other);
	mystring&  operator+=(const mystring& other);
	char& operator[](int i);
};


mystring::mystring(const char* s)
{
	len=strlen(s);
	str=(char*)malloc(len+1);
	strcpy(str,s);
}


mystring::~mystring()
{
	free(str);
	str=NULL;
}


void mystring::show()
{
	cout<<str<<endl;
}


mystring operator+(const mystring& s1,const mystring& s2)
{
	int len=strlen(s1.getstr())+strlen(s2.getstr());	
	char* arr=(char*)malloc(len+1);
	strcpy(arr,s1.getstr());
	strcat(arr,s2.getstr());
	return mystring(arr);
}


mystring&  mystring::operator=(const mystring& other)
{
	if(this==&other)
	{
		return *this;	
	}

	delete[] str;
	str=(char*)malloc(other.getlen()+1);
	strcpy(str,other.getstr());
	this->len=other.getlen();
	return *this;

}


mystring&  mystring::operator+=(const mystring& other)
{
	char* temp=NULL;
	temp=(char*)malloc(this->len);
	strcpy(temp,str);
	delete[] str;
	this->len=strlen(str)+other.getlen();
	str=(char*)malloc(this->len+1);
	strcpy(str,temp);
	strcat(str,other.getstr());
	
	free(temp);
	temp=NULL;

	return *this;

}


char& mystring::operator[](int i){

		return this->str[i];
}

int main(int argc,const char** argv){
	
	mystring str="hello";
	mystring ptr="world";
	
	str.show();
	ptr.show();
	str=str+ptr;

	str.show();

	str+=ptr;
	str.show();

	str[0]='H';
	str.show();

	return 0;
}

运行截图:

相关文章:

  • 利用Ai对生成的测试用例进行用例评审
  • Spring MVC 与 Spring Boot:从“手动挡”到“自动驾驶”的进化论,兼谈前后端分离的哲学
  • 单机上使用docker搭建minio集群
  • 3分钟快速本地部署deepseek
  • 网站快速收录:如何优化网站内部搜索功能?
  • Python学习总结
  • 【量化策略】双均线交叉策略
  • transformer架构嵌入层位置编码之RoPE旋转位置编码及简单实现示例
  • python读取sqlite温度数据,并画出折线图
  • 自己的百科词条能删掉吗?个人如何删除自己的百科词条?
  • 电动机能耗制动控制电路
  • SVM 支持向量机
  • Java笔记18
  • 前缀和与差分
  • 【深度学习】Adam和AdamW优化器有什么区别,以及为什么Adam会被自适应学习率影响
  • 鸿蒙开发深入浅出03(封装通用LazyForEach实现懒加载)
  • MySQL 数据库基础
  • Ryu控制器:L2交换功能实现案例
  • 帆软report
  • 使用GPU训练模型
  • 中年人多活动有助预防阿尔茨海默病
  • 专家解读《人源类器官研究伦理指引》:构建类器官研究全过程伦理治理框架
  • 胡塞武装称以色列所有机场均为其打击目标
  • 台湾花莲县海域发生5.7级地震,震源深度15公里
  • 抗战回望18︱《广西学生军》:“广西的政治基础是青年”
  • 广西科学调度保障春灌面积1373.53万亩