栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++运算符重载实例代码详解(调试环境 Visual Studio 2019)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++运算符重载实例代码详解(调试环境 Visual Studio 2019)

最近看了菜鸟教程里的C++教程

遇到很多运算符重载,为了方便我的学习,我把这些总结了一下
如有错误(包括之前的博文)请评论留言,谢谢!

由于代码里注释的很清楚,我就不做过多的解释了。

下面是这次总结的头文件,用来放置一些类和方法。

//C++运算符重载实例.h
#pragma once
#include 
using namespace std;
class chongzai
{
private:
 int i, j, k;
public:
 chongzai()
 {
 i = 0;
 j = 0;
 k = 0;
 }
 chongzai(int a, int b, int c)
 {
 i = a;
 j = b;
 k = c;
 }
 //以下分别为A1,A2,A3的显示函数
 void display1()
 {
 cout << "A1:" << endl;
 cout << "i=" << i << endl;
 cout << "j=" << j << endl;
 cout << "k=" << k << endl;
 cout << "------------------" << endl;
 }
 void display2()
 {
 cout << "A2:" << endl;
 cout << "i=" << i << endl;
 cout << "j=" << j << endl;
 cout << "k=" << k << endl;
 cout << "------------------" << endl;
 }
 void display3()
 {
 cout << "A3:" << endl;
 cout << "i=" << i << endl;
 cout << "j=" << j << endl;
 cout << "k=" << k << endl;
 cout << "------------------" << endl;
 }
 
 
 chongzai operator-()   //以负号( - )为例
 {
 i = -i;
 j = -j;
 k = -k;
 return chongzai(i, j, k);
 }
 
 
 chongzai operator+(const chongzai& n) //以加号( + )为例
 {
 chongzai A;
 A.i = this->i + n.i;
 A.j = this->j + n.j;
 A.k = this->k + n.k;
 return A;
 }
 
 
 bool operator<(const chongzai& n)  //以小于号( < )为例
 {
 if (i < n.i)
  return true;
 if (i >= n.i)
  return false;
 return false;
 }
 
 
 friend ostream& operator<<(ostream& output, const chongzai& A)
 {
 output << "i:" << A.i << endl;
 output << "j:" << A.j << endl;
 output << "k:" << A.k << endl;
 output << "------------------" << endl;
 return output;
 }
 friend istream& operator>>(istream& input, chongzai& A)
 {
 input >> A.i >> A.j >> A.k;
 return input;
 }
 
 
 chongzai operator++()   //以前置++为例
 {
 ++i;
 ++j;
 ++k;
 return chongzai(i, j, k);
 }
 chongzai operator++(int)   //后置++的特殊格式
 {
 i++;
 j++;
 k++;
 return chongzai(i, j, k);
 }
 
 
 void operator=(const chongzai& A)  //以拷贝构造函数为例
 {
 i = A.i;
 j = A.j;
 k = A.k;
 }
 
 
 chongzai operator()(int a, int b, int c)
 {
 chongzai A;
 //利用()里的参数进行各种运算
 A.i = a + b;
 A.j = b + c;
 A.k = a + c;
 return A;
 }
};
class chongzai2
{
private:
 int arr[5];
public:
 int n;
 chongzai2()
 {
 for (n = 0; n < 5; n++)
 {
  arr[n] = n;
 }
 }
 
 
 int& operator[](int n)
 {
 if (n >= 5)
 {
  cout << "索引超过最大值" << endl;
  return arr[0];
 }
 return arr[n];
 }
};

然后这是主程序

//C++运算符重载实例.cpp
#include "标头.h"
#include 
using namespace std;

int main()
{
 chongzai A1(10, 20, 30), A2(100, 200, 300), A3;
 -A1;     //一元运算符重载
 A1.display1();
 A3 = A1 + A2;    //二元运算符重载
 A3.display3();
 if (A1 < A2)    //关系运算符重载
 cout << "D1=D2" << endl << endl;
 cout << "请输入A3的各项参数:" << endl; //输入输出元算符重载
 cin >> A3;
 cout << "A3的各项参数为:" << endl;
 cout << A3;
 ++A1;     //前置++运算符重载
 A1.display1();
 A1++;     //后置++运算符重载
 A1.display1();
 A1 = A2;     //赋值运算符重载
 A1.display1();
 A2 = A3(2, 4, 6);    //函数调用运算符()重载
 A2.display2();
 chongzai2 B;
 cout << "B[4]的值为:" << B[4] << endl;
 cout << "B[5]的值为:" << B[5] << endl; //下标运算符[]重载
 return 0;
}

在输入A3的时候,举个例子,输入1 2 3

输出结果为

总结

到此这篇关于C++运算符重载实例(调试环境 Visual Studio 2019)的文章就介绍到这了,更多相关C++运算符重载内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61933.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号