c++友元实现操作符重载
运算符重载的本质是一个函数
#include <iostream>
using namespace std;
class A {
private:
int m_a;
int m_b;
friend A operator+(A &a1, A &a2);//友元函数訪问私有属性。实现二元运算符重载
friend A operator++(A &a); //友元函数訪问私有属性。实现一元运算符重载(前置)
/*后置为避免与前置函数定义同样,使用占位符int。告诉编译器是后置运算符重载*/
friend A operator++(A &a, int); //友元函数訪问私有属性。实现一元运算符重载(后置)
friend class B; //友元类,訪问私有属性。
若B类是A类的友员类,则B类的全部成员函数都是A类的友员函数
public:
A(int a){
this->m_a = a;
}
A(int a, int b) {
m_a = a;
m_b = b;
}
void show() {
cout << m_a << endl;
}
};
class B {
private:
A *pObjA = new A(21);
public:
void showA() {
cout << pObjA->m_a << endl;
}
};
A operator+(A &a1, A &a2) {
A tmp(a1.m_a + a2.m_a, a1.m_b + a2.m_b);
return tmp;
}
A operator++(A &a) { //前置++
a.m_a++;
a.m_b++;
return a;
}
A operator++(A &a, int) { //后置++
A tmp = a;
a.m_a++;
a.m_b++;
return tmp;
}
int main() {
A a1(1, 9);
A a2(3, 4);
A a3 = a1 + a2;
a3.show();
++a3;
a3.show();
a3++;
a3.show();
return 0;
}
class Complex
{
public:
int a;
int b;
friend Complex operator+(Complex &c1, Complex &c2);
public:
Complex(int a=0, int b=0)
{
this->a = a;
this->b = b;
}
public:
void printCom()
{
cout<<a<<" + "<<b<<"i "<<endl;
}
private:
};
/*
Complex myAdd(Complex &c1, Complex &c2)
{
Complex tmp(c1.a+ c2.a, c1.b + c2.b);
return tmp;
}
*/
Complex operator+(Complex &c1, Complex &c2)
{
Complex tmp(c1.a+ c2.a, c1.b + c2.b);
return tmp;
}
void main()
{
Complex c1(1, 2), c2(3, 4);
//Complex c3 = c1 + c2; //用户自己定义类型 编译器无法让变量相加
//Complex myAdd(Complex &c1, Complex &c2);
//1 普通函数
//Complex c3 = myAdd(c1, c2);
//c3.printCom();
//2 operator+ 函数名称
//Complex c3 = operator+(c1, c2);
//c3.printCom();
//3 +替换 函数名
Complex c3 = c1 + c2; //思考C++编译器怎样支持操作符重载机制的 (依据类型)
c3.printCom();
{
int a =0, b = 0, c; //基础类型C++编译器知道怎样加减
c = a +b;
}
//4 把Complex类变成私有属性
//friend Complex operator+(Complex &c1, Complex &c2);
cout<<"hello..."<<endl;
system("pause");
return ;
}
为vector类重载流插入运算符和提取运算符
class vector
{
public :
vector( int size =1 ) ;
~vector() ;
int & operator[]( int i ) ;
friend ostream & operator << ( ostream & output , vector & ) ;
friend istream & operator >> ( istream & input, vector & ) ;
private :
int * v ;
int len ;
};
vector::vector( int size )
{
if (size <= 0 || size > 100 )
{
cout << "The size of " << size << " is null !\n" ; abort() ;
}
v = new int[ size ] ; len = size ;
}
vector :: ~vector()
{
delete[] v ;
len = 0 ;
}
int &vector::operator[]( int i )
{
if( i >=0 && i < len ) return v[ i ] ;
cout << "The subscript " << i << " is outside !\n" ; abort() ;
}
ostream & operator << ( ostream & output, vector & ary )
{
for(int i = 0 ; i < ary.len ; i ++ )
output << ary[ i ] << " " ;
output << endl ;
return output ;
}
istream & operator >> ( istream & input, vector & ary )
{
for( int i = 0 ; i < ary.len ; i ++ )
input >> ary[ i ] ;
return input ;
}
void main()
{
int k ;
cout << "Input the length of vector A :\n" ;
cin >> k ;
vector A( k ) ;
cout << "Input the elements of vector A :\n" ;
cin >> A ;
cout << "Output the elements of vector A :\n" ;
cout << A ;
system("pause");
}
c++友元实现操作符重载的更多相关文章
- c++ 操作符重载和友元
操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...
- C++基础 (4) 第四天 this指针 全局函数和成员函数 友元 操作符重载
1static强化练习-仓库进货和出货 #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; c ...
- C++ 友元(friend关键字)、类中的重载、操作符重载(operator关键字)
C++ 中友元的用法: 1.在类中使用friend关键字声明 2.类的友元可以是其它类或者具体函数 3.友元不是类的一部分 4.友元不受类中访问级别的限制 5.友元可以直接访问具体类中的所有成员. 友 ...
- [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...
- C++一些注意点之操作符重载
重载操作符需要注意 (1)重载操作符必须具有一个类类型操作数.不能重载内建类型的操作符. operator +(int,int);//这个是错误的,都为内建类型 operator +(int,clas ...
- C++中的操作符重载
一.什么是操作符重载 操作符重载可以分为两部分:“操作符”和“重载”.说到重载想必都不陌生了吧,这是一种编译时多态,重载实际上可以分为函数重载和操作符重载.运算符重载和函数重载的不同之处在于操作符重载 ...
- 15.C++-操作符重载
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
- C++基础知识:操作符重载
1.C++标准库: C++标准库并不是C++语言的一部分C++标准库是由C++语言编写而成的类库和函数的集合C++标准库中定义的类和对象都位于std命名空间中C++标准库的头文件都不带.h后缀C++标 ...
- 15.C++-操作符重载、并实现复数类
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
随机推荐
- poj 3356
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- This Activity already has an action bar supplied by the window decor
问题描写叙述:继承自AppCompatActivity,使用Toolbar替代ActionBar的时候.出现错误 错误信息: 2.Caused by: java.lang.IllegalStateEx ...
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- HDU 1015.Safecracker【暴力枚举】【8月17】
Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is lo ...
- JS的 验证组织机构的合法性
以下直接上代码 //验证组织机构合法性方法 function orgcodevalidate(value){ if(value!=""){ var values=value.spl ...
- C-结构体应用(10)
结构体是用来定义多种类型的复合类型,在 C语言中与类的区别在于结构体注重的是数据而类除了数据还包含函数,第2点区别在于结构体所声明的成员默认是"public"点.而类的默认是pri ...
- windows 下载安装github
参考链接:http://download.csdn.net/download/devsplash/9683971
- 卷积操作中的矩阵乘法(gemm)—— 为什么矩阵乘法是深度学习的核心所在
1. 全连接 k 个输入: n 个神经元: 每个神经元都会学到一组权值向量,以和输入进行内积运算: n 个输出: 2. 卷积 卷积操作对于高维(多个平面)的输入,单个卷积核的深度应和输入的深度(dep ...
- C#对 Json的序列化和反序列化时出现“k_BackingField”
在C#2.0的项目中,以前经常使用Json.NET实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行json相关的操作.微软提供的原生类库使 ...
- Spark RDD概念学习系列之不同角度看RDD
不多说,直接上干货!