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++-操作符重载、并实现复数类
首先回忆下以前学的函数重载 函数重载 函数重载的本质为相互独立的不同函数 通过函数名和函数参数来确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域中 类中的函数重载 ...
随机推荐
- webuploader 教程
1.引入js和css <!-- Web Uploader --> <link rel="stylesheet" type="text/css" ...
- HDU 4893 线段树裸题
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- SqlHelper——仅仅由于在人群中多看了你一眼
一.SqlHelper 出场 不是由于大家都在用SqlHelper所以才用,是由于连接数据库关闭数据库查询数据库的多了也就加上了SqlHelper.当你的非常多需求都有一个同样的方法的时候我们没有必要 ...
- MySQL官方文档
http://dev.mysql.com/doc/refman/5.7/en/index.html 没有比这更好的MySQL文档了,省的去买书了
- elcipse 编译cocos2d-x android
http://blog.csdn.net/eyu8874521/article/details/22605695 最開始学习cocos2dx.大多数人可能是被复杂的环境配置过程搞死的,尤其是和Andr ...
- UVA - 1642 Magical GCD 数学
Magical GCD The Magical GCD of a nonempty sequence of positive integer ...
- Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)
用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...
- php后期静态绑定
php后期静态绑定 自 PHP 5.3.0 起,PHP 增加了一个叫做后期静态绑定的功能,用于在继承范围内引用静态调用的类. 虽然也可以调用非静态方法,但是不会在运行时绑定. static 不再只是简 ...
- poj--3169--Layout(简单差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9098 Accepted: 4347 Descriptio ...
- Oracle 优化和性能调整
分析评价Oracle数据库性能主要有数据库吞吐量.数据库用户响应时间两项指标.数据库用户响应时间又可以分为系统服务时间和用户等待时间两项,即: 数据库用户响应时间=系统服务时间+用户等待时间 因此 ...