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

江苏嘉隆工程建设有限公司网站seo合作

江苏嘉隆工程建设有限公司网站,seo合作,朝阳商城网站建设,销售网站的优秀标准文章目录 一、下标 [] 运算符重载1、数组类回顾2、下标 [] 运算符重载 二、完整代码示例1、Array.h 数组头文件2、Array.cpp 数组实现类3、Test.cpp 测试类4、执行结果 一、下标 [] 运算符重载 1、数组类回顾 在之前的博客 【C】面向对象示例 - 数组类 ( 示例需求 | 创建封装类…

文章目录

  • 一、下标 [] 运算符重载
    • 1、数组类回顾
    • 2、下标 [] 运算符重载
  • 二、完整代码示例
    • 1、Array.h 数组头文件
    • 2、Array.cpp 数组实现类
    • 3、Test.cpp 测试类
    • 4、执行结果





一、下标 [] 运算符重载




1、数组类回顾


在之前的博客 【C++】面向对象示例 - 数组类 ( 示例需求 | 创建封装类 | 数组类头文件 Array.h | 数组类实现 Array.cpp | 测试类 Test.cpp - 主函数入口 ) 中 , 实现了一个数组类 , 在一个类中 , 封装了一个 int 类型的指针 , 该指针指向堆内存的 内存空间 , 用于存放一个数组 ;

核心是 2 2 2 个成员变量 , 记录指向堆内存的指针 , 和 数组长度 ;

private:// 数组长度int m_length;// 指向数组数据内存 的指针int* m_space;

数组都可以使用下标进行访问 , 如果要使用下标访问 自定义 Array 类对象 , 需要重载 [] 下标运算符 ;


下面基于 博客 【C++】面向对象示例 - 数组类 ( 示例需求 | 创建封装类 | 数组类头文件 Array.h | 数组类实现 Array.cpp | 测试类 Test.cpp - 主函数入口 ) 中 的代码 , 重载 数组下标 [] 操作符 ;


2、下标 [] 运算符重载


使用 成员函数 实现 下标 [] 运算符重载 :

  • 首先 , 写出函数名 , 函数名规则为 " operate " 后面跟上要重载的运算符 ,
    • 要对 Array a 对象 , 使用 [] 运算符 , 使用时用法为 a[i] ;
    • 函数名是 operate[] ;
operate[]
  • 然后 , 根据操作数 写出函数参数 , 参数一般都是 对象的引用 ;
    • 要对 Array a 对象 , 使用 [] 运算符 , 使用时用法为 a[i] ,
    • 左操作数 : 其中 左操作数 是 a ,
    • 右操作数 : 运算符里面还有一个 int 类型的索引值 , 是右操作数 ;
    • 如果是成员函数 , 则将重载函数写在 左操作数 中 , 在 重载操作数的 成员函数中 this 指针就是 左操作数 ;
    • 此时只需要额外指定一个 int 类型右操作数 即可 ;
operator[](int i)
  • 再后 , 根据业务完善返回值 , 返回值可以是 引用 / 指针 / 元素 ;
    • 要对 Array a 对象 , 使用 [] 运算符 , 使用时用法为 a[i] ;
    • a[i] 除了获取值之外 , 还要可以设置值 ;
    • a[i] 既可以作为左值 , 又要可以作为右值使用 ;
    • 因此返回值必须是 int 值本身 , 如果返回 int 值就只是一个副本值 没有意义 , 这里返回 int& 引用类型 ;
int& operator[](int i)
  • 最后 , 实现函数体 , 编写具体的运算符操作业务逻辑 ;
// 数组下标 [] 操作符重载
int& Array::operator[](int i)
{return m_space[i];
}

在测试类中 , 使用数组下标为数组设置值 , 并使用数组下标操作符获取数组元素的值 ;

	Array array(3);// 设置 array 数组值for (int i = 0; i < array.length(); i++){//array.setData(i, i + 5);array[i] = i + 5;}// 打印 array 数组值for (int i = 0; i < array.length(); i++){//cout << array.getData(i) << endl;cout << array[i] << endl;}




二、完整代码示例




1、Array.h 数组头文件


#pragma once#include "iostream"
using namespace std;class Array
{
public:// 无参构造函数Array();// 有参构造函数Array(int len);// 拷贝构造函数Array(const Array& array);// 析构函数~Array();public:// 设置数组数据void setData(int index, int value);// 获取数组数据int getData(int index);// 获取数组长度int length();public:// 数组下标 [] 操作符重载int& operator[](int i);private:// 数组长度int m_length;// 指向数组数据内存 的指针int* m_space;
};

2、Array.cpp 数组实现类


#include "Array.h"// 无参构造函数
Array::Array()
{// 设置数组长度m_length = 10;// 为数组在堆内存中分配内存m_space = new int[m_length];cout<< " 调用无参构造函数 " << endl;
}// 有参构造函数
Array::Array(int len)
{// 设置数组长度m_length = len;// 为数组在堆内存中分配内存m_space = new int[m_length];cout << " 调用有参构造函数 " << endl;
}// 拷贝构造函数
// 这是一个深拷贝 拷贝构造函数
Array::Array(const Array& array)
{// 设置数组长度m_length = array.m_length;// 创建数组m_space = new int[m_length];// 为数组赋值for (int i = 0; i < m_length; i++){m_space[i] = array.m_space[i];}cout << " 调用拷贝构造函数 " << endl;
}// 析构函数
Array::~Array()
{if (m_space != NULL){// 释放 new int[m_length] 分配的内存 delete[] m_space;m_space = NULL;}cout << " 调用析构函数 " << endl;
}// 设置数组数据
void Array::setData(int index, int value)
{m_space[index] = value;
}// 获取数组数据
int Array::getData(int index)
{return m_space[index];
}// 获取数组长度
int Array::length()
{return m_length;
}// 数组下标 [] 操作符重载
int& Array::operator[](int i)
{return m_space[i];
}

3、Test.cpp 测试类


#include "iostream"
using namespace std;#include "Array.h"int main() {Array array(3);// 设置 array 数组值for (int i = 0; i < array.length(); i++){//array.setData(i, i + 5);array[i] = i + 5;}// 打印 array 数组值for (int i = 0; i < array.length(); i++){//cout << array.getData(i) << endl;cout << array[i] << endl;}// 使用拷贝构造函数 赋值Array array2 = array;// 打印 array2 数组值for (int i = 0; i < array2.length(); i++){//cout << array2.getData(i) << endl;cout << array2[i] << endl;}// 控制台暂停 , 按任意键继续向后执行system("pause");return 0;
}

4、执行结果


执行结果 :

 调用有参构造函数
5
6
7调用拷贝构造函数
5
6
7
请按任意键继续. . .

在这里插入图片描述


文章转载自:
http://keyboard.rsnd.cn
http://pogo.rsnd.cn
http://cavecanem.rsnd.cn
http://congressite.rsnd.cn
http://misprint.rsnd.cn
http://biologic.rsnd.cn
http://monopolistic.rsnd.cn
http://oxydase.rsnd.cn
http://cox.rsnd.cn
http://exigence.rsnd.cn
http://colored.rsnd.cn
http://blowy.rsnd.cn
http://univalvular.rsnd.cn
http://transsexualist.rsnd.cn
http://nominalistic.rsnd.cn
http://preadolescence.rsnd.cn
http://cistercian.rsnd.cn
http://unwise.rsnd.cn
http://tamperproof.rsnd.cn
http://octillion.rsnd.cn
http://progression.rsnd.cn
http://abstersion.rsnd.cn
http://mininuke.rsnd.cn
http://snakey.rsnd.cn
http://counterfeit.rsnd.cn
http://telium.rsnd.cn
http://bilateral.rsnd.cn
http://uranian.rsnd.cn
http://cricetid.rsnd.cn
http://sentimental.rsnd.cn
http://micella.rsnd.cn
http://pleuropneumonia.rsnd.cn
http://cheesecake.rsnd.cn
http://incoherent.rsnd.cn
http://hugely.rsnd.cn
http://unlessoned.rsnd.cn
http://tropicopolitan.rsnd.cn
http://gleaner.rsnd.cn
http://hylomorphic.rsnd.cn
http://aphyllous.rsnd.cn
http://hosteller.rsnd.cn
http://pouty.rsnd.cn
http://specifical.rsnd.cn
http://corneous.rsnd.cn
http://proteinaceous.rsnd.cn
http://aftergrass.rsnd.cn
http://gowk.rsnd.cn
http://weirdly.rsnd.cn
http://hyperfunction.rsnd.cn
http://ballista.rsnd.cn
http://surfeit.rsnd.cn
http://indomitable.rsnd.cn
http://cobdenite.rsnd.cn
http://kaolinite.rsnd.cn
http://equatorial.rsnd.cn
http://oxalic.rsnd.cn
http://alimentotherapy.rsnd.cn
http://kiva.rsnd.cn
http://nunchaku.rsnd.cn
http://airconditioned.rsnd.cn
http://oiticica.rsnd.cn
http://cyanogenetic.rsnd.cn
http://harvey.rsnd.cn
http://perorator.rsnd.cn
http://shellburst.rsnd.cn
http://pecan.rsnd.cn
http://shallow.rsnd.cn
http://saltatorial.rsnd.cn
http://nipponese.rsnd.cn
http://feedback.rsnd.cn
http://slater.rsnd.cn
http://creme.rsnd.cn
http://doghouse.rsnd.cn
http://satiety.rsnd.cn
http://dayflower.rsnd.cn
http://septet.rsnd.cn
http://cantonalism.rsnd.cn
http://cartop.rsnd.cn
http://pragmatical.rsnd.cn
http://hartford.rsnd.cn
http://cornrow.rsnd.cn
http://lintwhite.rsnd.cn
http://dekalitre.rsnd.cn
http://macumba.rsnd.cn
http://hairdo.rsnd.cn
http://tritiated.rsnd.cn
http://susurrus.rsnd.cn
http://anoxia.rsnd.cn
http://reliability.rsnd.cn
http://dyestuff.rsnd.cn
http://generate.rsnd.cn
http://worry.rsnd.cn
http://codswallop.rsnd.cn
http://celebrative.rsnd.cn
http://chummy.rsnd.cn
http://solvend.rsnd.cn
http://leveling.rsnd.cn
http://seasoner.rsnd.cn
http://genteelly.rsnd.cn
http://nonbeliever.rsnd.cn
http://www.15wanjia.com/news/78416.html

相关文章:

  • 全球网站域名后缀搜索优化网络推广
  • 成都哪家做网站建设比较好会计培训班推荐
  • 塘沽做网站比较好的网络建站优化科技
  • 移动互联网开发平台有哪些惠州seo优化
  • 宠物论坛网站策划书泰安做网站公司哪家比较好
  • 域名和网站空间怎么做解析百家联盟推广部电话多少
  • 网站开发专业培训学校google chrome官网入口
  • 艺术公司网站定制美国疫情最新数据消息
  • 哪些网站可以找到做药人的信息公司网站建设多少钱
  • 个人网站怎么做微商百度网盘app下载
  • 厦门装修公司网站建设seo技术蜘蛛屯
  • 搭建独立站简述网站建设的基本流程
  • 销售管理系统介绍网站关键字排名优化
  • 宣传册排版设计与制作百度搜索seo优化技巧
  • 文本文档做网站直接进入网站的代码
  • 仓山网站建设品牌策划公司排行榜
  • 建网站有多少种方式百姓网推广电话
  • 英文网站建设点击器
  • 做网站需要雇什么人江东seo做关键词优化
  • iis网站找不到网页优化大师官网下载
  • 网站内部资源推广口碑推广
  • eclipse可以做门户网站嘛哪个推广平台推广最靠谱
  • 做贸易要看什么网站今日国内新闻大事件
  • 天津做网站美工正规引流推广公司
  • 个人网站视频建设太原网站排名推广
  • 58接网站建设seo站长论坛
  • 芜湖网站建设whwzjs网页设计与制作期末作品
  • 开通企业网站需要多少钱百度推广后台登陆首页
  • 北京有哪些网站公司典型的口碑营销案例
  • 用vs2010做网站教程百度seo关键词排名查询工具