C++面向对象程序设计的一些知识点(5)
摘要:运算符能给程序员提供一种书写数学公式的感觉,本质上运算符也是一种函数,因此有类内部运算符和全局运算符之分,通过重载,运算符的“动作”更加有针对性,编写代码更像写英文文章。
1、C++标准允许将运算符重载为类成员或者全局的,一般如果是全局的话,为了效率,都是把它们定义为类友元函数。
/*
** 重载全局运算符“+”、“-”,代码如下:
*/
#include <iostream> using namespace std; //class Complex;
//Complex operator+(const Complex &c1, const Complex &c2);
//Complex operator-(const Complex &c1, const Complex &c2);
// 我的疑问:声明为全局的有什么好处的,或者说有什么必要的么?
class Complex
{
public:
Complex(double real = 0.0, double image = 0.0)
{
this->real = real;
this->image = image;
}
public:
void displayComplex()
{
if(image >= )
cout << "(" << real << '+' << image << 'i' << ")";
else
cout << "(" << real << image << 'i' << ")";
}
friend Complex operator+(const Complex &c1, const Complex &c2);
friend Complex operator-(const Complex &c1, const Complex &c2);
private:
double real;
double image;
}; Complex operator+(const Complex &c1, const Complex &c2)
{
Complex complexTemp;
complexTemp.real = c1.real + c2.real;
complexTemp.image = c1.image + c2.image;
return complexTemp;
}
Complex operator-(const Complex &c1, const Complex &c2)
{
Complex complexTemp;
complexTemp.real = c1.real - c2.real;
complexTemp.image = c1.image - c2.image;
return complexTemp;
}
int main()
{
Complex c, c1(1.1, 2.2), c2(3.3, 4.4);
c = c1 + c2;
c = c1 - c2; // operator-(c1, c2);
c1.displayComplex();
cout << "-";
c2.displayComplex();
cout << "=";
c.displayComplex();
cout << endl; int i;
cin >> i;
return ;
}
重载为友元函数
2、运算符重载的一些基本规则:
禁止用户发明C++语言运算符之外的其他运算符
不要试图改变重载运算符的语义,保持重载后的运算语义与内置运算符语义一致,否则会带来思维上的混乱
不能重载.、.*、::、?:
不能重载#、##
不能重载sizeof()、typeid()
不能重载static_cast<>、dynamic_cast<>、const_cast<>、reinterpret_cast<>
3、运算符重载时,它的返回值类型会有程序运行效率和使用重载运算符方便程度的差别
当返回值为对象时,拷贝构造函数会构建一个临时对象
当返回值为对象引用时,不会引起构造函数的调用,其返回值可以作为左值
当返回值为对象指针时,不会引发构造函数的调用,其返回值可以作为左值
/*
** 重载运算符,返回不同类型的差别
*/
#include <iostream> using namespace std; class Complex
{
public:
Complex(double real = 0.0, double image = 0.0)
{
this->real = real;
this->image = image;
}
Complex(const Complex &c) // 拷贝构造函数
{
real = c.real;
image = c.image;
cout << "copy constructing..." << endl;
}
public:
Complex operator+(const Complex &c)
{
Complex complexTemp;
complexTemp.real = real + c.real;
complexTemp.image = image + c.image;
return complexTemp; // 此时会调用拷贝构造函数,构造一个临时对象
}
Complex& operator-(const Complex &c)
{
real -= c.real;
image -= c.image;
return *this;
}
void displayComplex()
{
if(image >= )
cout << "(" << real << "+" << image << "i" << ")";
else
cout << "(" << real << image << "i" << ")";
}
private:
double real;
double image;
};
int main()
{
Complex c, c1(1.1, 2.2), c2(3.3, 4.4);
(c1 + c2).displayComplex();
cout << "Before c1 - c2" << endl;
c1.displayComplex();
cout << endl;
cout << "After c1 - c2" << endl;
(c1 - c2).displayComplex();
cout << endl;
int i;
cin >> i;
return ;
}
重载返回值不同类型的差别
4、运算符重载示例:
赋值运算符
/*
** 赋值运算符“=”重载
*/
#include <iostream>
#include <cstring> using namespace std; class Student
{
public:
Student(char *name = "")
{
pName = new char[strlen(name) + ];
strcpy(pName, name);
}
~Student()
{
delete pName;
pName = NULL;
}
public:
Student &operator=(const Student &student)
{
cout << "assignment called..." << endl;
// 若为同一个对象则直接返回即可
if(this == &student) // 注意这里需要用指针比较,因为没有实现对象比较的运算符呢
{
cout<< "\tExact the same object." << endl;
return *this;
}
delete pName; // 否则,先释放资源
pName = new char[strlen(student.pName) + ];
strcpy(pName, student.pName);
return *this;
}
void show()
{
cout << pName << endl;
}
private:
char *pName;
}; int main()
{
Student s1, s2("zhangsan");
s1 = s1; s1 = s2;
s1.show(); int i;
cin >> i;
return ;
}
赋值运算符重载示例
函数符
/*
** 函数符“()”重载
*/
#include <iostream> using namespace std; class Student
{
public:
Student(int chinese, int math, int english)
{
this->chinese = chinese;
this->math = math;
this->english = english;
}
~Student()
{ }
public:
void operator()(int index = )
{
switch(index)
{
case :
cout << "chinese:" << chinese << endl;
break;
case :
cout << "math:" << math << endl;
break;
case :
cout << "english:" << math << endl;
break;
default:
cout << "not found" << endl;
}
}
private:
int chinese;
int math;
int english;
};
int main()
{
Student student(, , );
student();
student();
student();
student();
student();
student(); int i;
cin >> i;
return ;
}
函数调用符重载示例
下标运算
/*
** 下表运算符“[]”重载
*/
#include <iostream> using namespace std; class UserString
{
public:
UserString(char *str)
{
len = strlen(str);
this->str = new char[len + ];
strcpy(this->str, str);
}
~UserString()
{
delete str;
str = NULL;
}
public:
char operator[](int index)
{
if(index >= && index < len) // 判断越界
return str[index];
else
{
cout << "beyond scope..." << endl;
return ' ';
}
}
int getLength()
{
return len;
}
private:
char *str;
int len;
}; int main()
{
UserString us("zhangsan");
cout << "\"zhangsan\" length:" << us.getLength() << endl;
int length = us.getLength();
int i;
for(i = ;i < length;i++)
cout << us[i]; // 使用[]访问
cout << endl;
cout << us[i]; cin >> i;
return ;
}
下表运算符重载示例
运算符++、--
/*
** 运算符“++、--”重载
*/
#include <iostream> using namespace std; class Integer
{
public:
Integer(int data)
{
this->data = data;
}
Integer(const Integer &i)
{
this->data = i.data;
}
~Integer()
{ }
public:
Integer operator++(int)
{
Integer temp(*this);
data++;
return temp;
}
Integer operator++()
{
data++;
return *this;
}
void show()
{
cout << data << endl;
}
private:
int data;
}; int main()
{
Integer x();
(x++).show();
(++x).show(); int i;
cin >> i;
return ;
}
++、--运算符重载示例
类型转换
/*
** 类型转换运算符重载,从对象到内置类型
*/
#include <iostream> using namespace std; class Complex
{
public:
Complex(double real, double image)
{
this->real = real;
this->image = image;
}
public:
operator double()
{
return real;
}
private:
double real;
double image;
}; int main()
{
double d = 1.1;
Complex c(4.0, 5.0); d += c; // 此时c.operator double()返回4.0
cout << d << endl; int i;
cin >> i;
return ;
}
类型转换-从对象到内置类型
/*
** 类型转换运算符重载,从内置类型到对象
*/
#include <iostream> using namespace std; class Complex
{
public:
Complex(double real, double image)
{
this->real = real;
this->image = image;
}
Complex(const Complex &c)
{
real = c.real;
image = c.image;
cout << "copy constructor called..." << endl;
}
Complex(double real)
{
this->real = real;
this->image = ;
cout << "one parameter called..." << endl;
}
~Complex()
{}
public:
Complex& operator+=(const Complex &c)
{
real += c.real;
image += c.image;
cout << "overload operator+= called..." << endl;
return *this;
}
void show()
{
cout << real;
if(image >= )
cout << "+" << image << "i" << endl;
else
cout << image << "i" << endl;
}
private:
double real;
double image;
}; int main()
{
Complex c1(, 1.1);
c1 += 3.3;
c1.show(); int i;
cin >> i;
return ;
}
类型转换-从内置类型到对象
5、运算符重载规则
| 运算符 | 规则 |
| 所有单目运算符 | 建议重载为非static成员函数 |
| =、()、[]、->、* | 建议重载为非static成员函数 |
| +=、-=、/=、*=、&=、|=、~=、%=、>>=、<<= | 建议重载为非static成员函数 |
| 所有其他运算符 | 建议重载为全局函数或类的友元函数 |
C++面向对象程序设计的一些知识点(5)的更多相关文章
- C++面向对象程序设计的一些知识点(4)
摘要:累继承可以实现源代码重用,模板技术也可以达到同样的目的.模板可以分为函数模板和类模板,其共同点是允许参数类进行类型化,编译器根据程序员传递的具体类型,又把函数模板实例化为具体函数,把类模板实例化 ...
- C++面向对象程序设计的一些知识点(3)
摘要:多态性提供一组统一的调用接口函数,依据这些条用接口函数具体对象的不同,同一名字的函数会有不同的行为. 1.重载与隐藏 (1).对同一作用域中的同名函数,如果它们的函数特征标不同,那么它们就形成一 ...
- C++面向对象程序设计的一些知识点(2)
1.C++中三种继承方式及派生类中访问控制规则 (1).C++支持的三种继承方式是public.protected.private.C++允许一个类同时以不同的方式对不同的基类加以继承. (2). 不 ...
- C++面向对象程序设计的一些知识点(1)
1.函数重载而不出错是有条件的: (1).这些函数位于同一域内,如同一类中.同一文件中. (2).这些同名函数具有不同的参数特征标(特征标是指形參的个数.类型.排列顺序等). 2.引用特性(与指针对比 ...
- 对面向对象程序设计(OOP)的认识
前言 本文主要介绍面向对象(OO)程序设计,以维基百科的解释: 面向对象程序设计(英语:Object-oriented programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发 ...
- 20162317袁逸灏 第八周实验报告:实验二 Java面向对象程序设计
20162317袁逸灏 第八周实验报告:实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 ...
- 2018-2019-2 20175217 实验二《Java面向对象程序设计》实验报告
一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:吴一凡 学号:20175217 指导教师:娄嘉鹏 实验日期:2019年4月15日 实验时间:--- 实验序号:实验二 实验名称:Jav ...
- 20175315 实验二《Java面向对象程序设计》实验报告
20175315 实验二<Java面向对象程序设计>实验报告 一.实验内容及步骤 1.初步掌握单元测试和TDD 单元测试 任务一:三种代码 用程序解决问题时,要学会写以下三种代码: 伪代码 ...
- 2018-2019-2 20175202实验二《Java面向对象程序设计》实验报告
2018-2019-2 20175202实验二<Java面向对象程序设计>实验报告 一.实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初 ...
随机推荐
- 解决-bash: fork: retry: Resource temporarily unavailable (修改最大线程数)
错误提示的本质是Linux操作系统无法创建更多进程,导致出错.因此要解决这个问题需要修改Linux允许创建更多的进程. 方案一: cat /etc/security/limits.conf echo ...
- cocos2d-x项目101次相遇-安装和环境搭建 -xcode
cocos2d-x 101次相遇 / 文件夹 1 安装和环境搭建 -xcode 2 Scenes . Director, Layers, Sprites 3 建立图片菜单 4 在 ...
- CMake 基本用法--写CMakeList.txt
http://techbase.kde.org/Development/Tutorials/CMake_(zh_CN) http://www.cmake.org/Wiki/CMake 这一章将从软件开 ...
- python之函数用法isinstance()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法isinstance() #isinstance() #说明:返回一个布尔值,判断数据 ...
- java 如何取前32位全是1的int型数据的后八位
直接&255 因为Integer.toBinaryString(255) 是 8个1. 如果一个负数byte转成int则前面全部会补1,就是24个1和它自己的八位,,于是和八个1相&就 ...
- 蚂蚁金服互联网IT运维体系实践
摘要: 本文来自蚂蚁金服首席技术架构师,基础技术部负责人胡喜.从2010年支撑双十一最高交易峰值2万笔/分钟到2015年双十一的8.59万笔/秒,蚂蚁金服的技术架构和运维体系一直都在不断摸索和实践.本 ...
- 实践:由0到1-无线大数据UX团队的成长
背景 大数据产品的在项目成立之初,采用的是模仿原有网优工具的方式做UI设计,由BA主导画草图.手绘线框图.excel制作,更有直接打开参考产品做原型的方式,没有统一的设计和规范可言.随着团队逐渐增多. ...
- HDUOJ----1170Milk
Milk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Linux安装JDK详细步骤(rpm方式)
1. 先从网上下载jdk(jdk-6u32-linux-i586-rpm) ,下载后放在/home目录中,当然其它地方也行. 进入安装目录 #cd /home#cp jdk-6u32-linux-i5 ...
- C#取得页面URL信息
我們在開發網頁應用程式,時常需要去解析網址(Request.Url)的每個片段,進行一些判斷.例如說 "http://localhost:1897/News/Press/Content.as ...