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

深圳外贸网站公司网站多少钱

深圳外贸网站公司,网站多少钱,wordpress加图片,成都高端网站制作目录 一、流输出 1、实现单个输出 2、实现连续输出 二、流输入 总结: 三、const修饰 四、取地址 .取地址及const取地址操作符重载 五、[ ]运算符重载 一、流输出 1、实现单个输出 创建一个日期类。 class Date { public:Date(int year 1, int month 1,…

目录

一、流输出

1、实现单个输出

2、实现连续输出

二、流输入

  总结:

三、const修饰

四、取地址

.取地址及const取地址操作符重载

五、[ ]运算符重载


一、流输出

1、实现单个输出

创建一个日期类。

class Date
{
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}
private:int _year;int _month;int _day;
};

以前我们在日期类中想要输出日期,都需要在类中自己创建一个用于输出的成员函数。

	void Print(){cout<< _year << "年" << _month << "月" << _day << "日" << endl;}

对于内置类型我们可以直接用cout<<输出其值。

我们也可以重载流提取<<实现输出内置类型的成员值,首先来了解一下cout的由来。

  • iostream 是 C++ 标准库中的一个头文件,它包含了用于输入和输出的流类的定义。iostream 头文件中定义了 istream 和 ostream 这两个基类,它们分别用于输入和输出操作。

  • ostream 是 iostream 头文件中定义的一个类,它是输出流的基类。ostream 类提供了输出操作的基本功能和接口,例如 << 操作符用于输出数据到流中。

  • cout 是 ostream 类的一个对象,它是标准输出流对象。cout 对象可以使用 << 操作符将数据输出到标准输出设备(通常是控制台)。

iostream 是一个头文件,ostream 是 iostream 中定义的输出流基类,而 cout 是 ostream 类的一个对象,用于将数据输出到标准输出设备。通过使用 cout 对象和 << 操作符,我们可以方便地将数据输出到控制台。

 现在在类中实现<<重载:

	void operator<<(ostream& out){out << _year << "年" << _month << "月" << _day << "日" << endl;}

ostream& 是一个引用类型,表示对输出流对象的引用,通过使用 ostream& 引用类型,可以将输出流对象传递给操作符重载。

 当我们要使用运算符重载<<时,需要使用如下形式:

d1 << cout;//或者d1.operator<<(cout);

运算符重载<<的第一个参数为左操作数,第二个参数为右操作数。

虽然这种形式可以输出我们想要的结果,但这与我们使用的cout<<d1这种常规方式有所出入。

我们可以对其进行修改,将<<运算符重载作为全局函数,将输出流对象的引用作为第一个参数,日期类对象的引用作为第二个参数。

void operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}

同时,为了使全局<<运算符重载能够访问到日期类对象d的私有成员变量,可以在日期类中创建友元函数声明,这样就可以访问对象的成员了。

friend void operator<<(ostream& out, const Date& d);

下面来测试一下: 

class Date
{friend void operator<<(ostream& out, const Date& d);
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout<< _year << "年" << _month << "月" << _day << "日" << endl;}
private:int _year;int _month;int _day;
};
void operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
}void Test()
{Date a(2023, 11, 24);a.Print();cout << a;
}int main()
{Test();return 0;
}

 成功实现运算符<<的重载。

 

2、实现连续输出

如果是下面这种连续输出呢?

void Test2()
{Date a(2023, 11, 24);Date b(2023, 11, 25);cout << a << b << endl;
}

这时编译器会报错。 

 

 为了支持连续输出的形式,我们需要为<<重载增加返回值。

ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}

同时,友元函数声明也要修改一下:

friend ostream& operator<<(ostream& out, const Date& d);

 测试一下:

class Date
{friend ostream& operator<<(ostream& out, const Date& d);
public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout<< _year << "年" << _month << "月" << _day << "日" << endl;}
private:int _year;int _month;int _day;
};
ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
void Test2()
{Date a(2023, 11, 24);Date b(2023, 11, 25);cout << a << b << endl;
}
int main()
{Test2();return 0;
}

成功实现连续输出: 

二、流输入

iostreamistream 和 cin 是 C++ 中用于输入的相关类和对象。

  • iostream 是 C++ 标准库中的一个头文件,它包含了用于输入和输出的流类的定义。iostream 头文件中定义了 istream 和 ostream 这两个基类,分别用于输入和输出操作。

  • istream 是 iostream 头文件中定义的一个类,它是输入流的基类。istream 类提供了输入操作的基本功能和接口,例如 >> 操作符用于从流中读取数据。

  • cin 是 istream 类的一个对象,它是标准输入流对象。cin 对象可以使用 >> 操作符从标准输入设备(通常是键盘)读取数据。

总结:iostream 是一个头文件,istream 是 iostream 中定义的输入流基类,而 cin 是 istream 类的一个对象,用于从标准输入设备读取数据。通过使用 cin 对象和 >> 操作符,我们可以方便地从键盘输入数据。

 下面来实现流提取>>运算符重载,与流插入<<实现方式相同。

istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

 测试一下

class Date
{friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}void Print(){cout<< _year << "年" << _month << "月" << _day << "日" << endl;}
private:int _year;int _month;int _day;
};
istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}
void Test3()
{Date d;cin >> d;cout << d;
}
int main()
{Test3();return 0;
}

成功实现流提取运算符重载。 

总结:

如果类的声明和定义是分文件的,我们一般把流提取和流插入运算符重载放到作为内联函数放到头文件中,这样省去了链接的过程,在编译过程就能call地址。

inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}
  • 在C++中,如果一个成员函数直接在类内部定义,它会被视为内联函数。内联函数的定义与声明都在类的定义中,这样编译器可以在调用处将函数的代码插入到调用位置,而不是通过函数调用的方式执行。
  • 通常情况下,短小的函数适合作为内联函数,因为内联函数的调用开销较小,可以避免函数调用的额外开销。将这样的函数定义在类内部可以方便地将其声明和定义放在一起,提高代码的可读性和维护性。
  • 然而,需要注意的是,编译器是否将一个在类内部定义的成员函数视为内联函数,最终还是由编译器决定。编译器可能会根据一些因素(如函数的复杂性、调用频率等)来决定是否将其内联展开。
  • 总之,将短小的函数定义在类内部可以被视为内联函数,这样可以提高代码的执行效率和可读性。但最终是否内联展开还是由编译器决定。

三、const修饰

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

我们来看下面代码:

class A {
public:void Print() {cout << _a << endl;}
private:int _a = 1;
};
int main()
{A aa;aa.Print();return 0;
}

成功输出: 

如果用const修饰aa,这样可以吗? 

const A aa;

编译后程序报错: 

 

这是因为造成了权限放大的问题。

 Print函数的参数是A*this,aa的类型是const A*,所以aa调用Print函数会造成权限放大,而且如果权限平移,在this指针前用const修饰,这样也是禁止的,我们不能修改this指针。

这时有一个新的间接方法:

  • 语法规定叫const成员函数,const修饰*this,也就意味着this的类型变成const A*类型。
  • 内部不改变成员变量的成员函数时,最好加上const,const对象和普通对象都可以调用。

 这时我们就可以对C++类与对象(4)—日期类的实现这篇文章的Date.h文件中部分成员函数进行const修饰了。

#include <iostream>
#include <assert.h>
using namespace std;class Date {
public:Date(int year = 0, int month = 0, int day = 0);void Print();int GetMonthDay(int year, int month) const;bool operator==(const Date& d) const;bool operator!=(const Date& d) const;bool operator< (const Date& d) const;bool operator<=(const Date& d) const;bool operator> (const Date& d) const;bool operator>=(const Date& d) const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);// d1 - 100Date operator-(int day);// d1 - d2;int operator-(const Date& d) const;// ++d1Date& operator++();// d1++Date operator++(int);Date& operator--();Date operator--(int);private:int _year;int _month;int _day;
};

四、取地址

.取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成

class Date
{
public :Date* operator&(){return this ; }const Date* operator&()const{return this ;}
private :int _year ; // 年int _month ; // 月int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容。

五、[ ]运算符重载

class Array
{
public:int& operator[](int i){assert(i < 10);return _a[i];}const int& operator[](int i) const{assert(i < 10);return _a[i];}
private:int _a[10];int _size;
};void Func(const Array& aa)
{for (int i = 0; i < 10; ++i){//aa[i]++;cout << aa[i] << " ";}
}

首先,我们来看Array类:

  • Array类定义了一个私有的整型数组_a,大小为10,以及一个私有的整型变量_size

  • Array类重载了[]运算符,这样我们就可以像使用普通数组一样使用Array类的对象。

  • operator[]函数有两个版本,一个是非常量版本,一个是常量版本。非常量版本返回一个可修改的引用,常量版本返回一个不可修改的常量引用。

  • operator[]函数中,使用了assert函数来确保索引i小于10,防止数组越界。

然后,我们来看Func函数:

  • Func函数接受一个Array类的常量引用作为参数。因为参数是常量引用,所以我们不能在函数中修改参数的值。

  • Func函数中,有一个循环,循环变量i从0遍历到9。在循环体中,首先注释掉了aa[i]++,这是因为aa是一个常量引用,我们不能修改它的值。然后,使用cout打印出aa[i]的值,然后打印一个空格。

举个例子,如果我们创建一个Array类的对象a,并初始化_a数组为0到9,然后调用Func(a),那么控制台上会打印出0 1 2 3 4 5 6 7 8 9


文章转载自:
http://dinncodharmsala.stkw.cn
http://dinncovibropack.stkw.cn
http://dinncobalk.stkw.cn
http://dinncogeopolitist.stkw.cn
http://dinncosentiment.stkw.cn
http://dinnconwt.stkw.cn
http://dinncoutilitarian.stkw.cn
http://dinncoinsomnious.stkw.cn
http://dinncostertor.stkw.cn
http://dinncovehemency.stkw.cn
http://dinncoincapacitant.stkw.cn
http://dinncoturbid.stkw.cn
http://dinncoholster.stkw.cn
http://dinncocheapness.stkw.cn
http://dinncofrondent.stkw.cn
http://dinncotesty.stkw.cn
http://dinncorabat.stkw.cn
http://dinncoamidone.stkw.cn
http://dinncosba.stkw.cn
http://dinncobanda.stkw.cn
http://dinncotemporariness.stkw.cn
http://dinncotasty.stkw.cn
http://dinncoskiogram.stkw.cn
http://dinncokurus.stkw.cn
http://dinncoacushla.stkw.cn
http://dinncoruggedness.stkw.cn
http://dinncooverclothe.stkw.cn
http://dinncoescarpment.stkw.cn
http://dinnconoetic.stkw.cn
http://dinncoentia.stkw.cn
http://dinncoutwa.stkw.cn
http://dinncoenact.stkw.cn
http://dinnconatriuresis.stkw.cn
http://dinncotortellini.stkw.cn
http://dinncounderpopulated.stkw.cn
http://dinncoaraneose.stkw.cn
http://dinncobanda.stkw.cn
http://dinncoduckbill.stkw.cn
http://dinncoafterburner.stkw.cn
http://dinncocounterdrug.stkw.cn
http://dinncosafely.stkw.cn
http://dinncogentlewomanly.stkw.cn
http://dinncolewdster.stkw.cn
http://dinnconumber.stkw.cn
http://dinncocontemporary.stkw.cn
http://dinncoresearch.stkw.cn
http://dinncoradiancy.stkw.cn
http://dinncobriefcase.stkw.cn
http://dinncodivertissement.stkw.cn
http://dinncoparliament.stkw.cn
http://dinncoprosify.stkw.cn
http://dinncoarlene.stkw.cn
http://dinncoodium.stkw.cn
http://dinncodisobedience.stkw.cn
http://dinncolaurence.stkw.cn
http://dinncofilings.stkw.cn
http://dinncorheebok.stkw.cn
http://dinncoantisexual.stkw.cn
http://dinncooverbusy.stkw.cn
http://dinncoactually.stkw.cn
http://dinncorubricate.stkw.cn
http://dinncoelectronics.stkw.cn
http://dinncogerminant.stkw.cn
http://dinncobrasilia.stkw.cn
http://dinncoscapolite.stkw.cn
http://dinncoparticipialize.stkw.cn
http://dinncoaphanite.stkw.cn
http://dinncobolshevism.stkw.cn
http://dinncobpc.stkw.cn
http://dinncoawag.stkw.cn
http://dinncoeventuality.stkw.cn
http://dinncoinfrastructure.stkw.cn
http://dinncogastrula.stkw.cn
http://dinncoballistic.stkw.cn
http://dinncodandy.stkw.cn
http://dinncodownstreet.stkw.cn
http://dinncofright.stkw.cn
http://dinncopleuston.stkw.cn
http://dinncorestitute.stkw.cn
http://dinncomanueline.stkw.cn
http://dinncosolemn.stkw.cn
http://dinncoventriloquy.stkw.cn
http://dinncoribald.stkw.cn
http://dinncosurveil.stkw.cn
http://dinncogreco.stkw.cn
http://dinncopoland.stkw.cn
http://dinncomagnetoelasticity.stkw.cn
http://dinncoratepayer.stkw.cn
http://dinncolathi.stkw.cn
http://dinncothallic.stkw.cn
http://dinncochambray.stkw.cn
http://dinnconightdress.stkw.cn
http://dinncobroad.stkw.cn
http://dinncoeyedropper.stkw.cn
http://dinncobasifixed.stkw.cn
http://dinncozenaida.stkw.cn
http://dinncoump.stkw.cn
http://dinncorheophobic.stkw.cn
http://dinncotriol.stkw.cn
http://dinncoanathemata.stkw.cn
http://www.dinnco.com/news/156175.html

相关文章:

  • 购物网站建设方案网站优化的方式有哪些
  • 上海平台网站建设平台谷歌seo服务
  • 酒店网站 asp.net珠海seo关键词排名
  • 哪里有零基础网站建设教学服务培训网址
  • 西安做网站建设哪家好网络关键词优化软件
  • 广告联盟怎么赚钱网络公司优化关键词
  • 公司网页模板免费下载重庆seo网站
  • 网站 做购物车信息发布平台推广有哪些
  • 网站开发报价技巧网页设计与制作学什么
  • 电商网站开发的现状济南seo排名搜索
  • 做网站一般注册商标哪个类东莞seo网络推广专
  • 网站如何做微信支付宝支付宝移动优化课主讲:夫唯老师
  • 盘锦做网站谁家好各大搜索引擎网址
  • 做网站的抬头怎么做最新搜索关键词
  • 苏州园区公积金管理中心网站推广优化外包公司哪家好
  • 一流的上海网站建设网站排名优化价格
  • 衡水网站建设服务商怎么做网站赚钱
  • 济南手机网站建设电话百度seo排名查询
  • 台州网站推广排名b2b电商平台
  • 网站怎样做外链建站模板平台
  • 可以做任务的网站有哪些外链工厂
  • 网站设置的用途深圳网站做优化哪家公司好
  • 网站后台如何做广州网站建设推广专家
  • 整形医院网站建设app推广软文范文
  • 宜春网站推广优化新闻稿发布
  • 企业网站的设计要求有哪些搜索引擎关键词竞价排名
  • 网站建设销售话术900句买卖交易平台
  • wordpress浮动条件百度seo服务方案
  • 连云港做网站制作株洲百度seo
  • 怎么做网站赚钱软件中文搜索引擎大全