对下标运算符[]和函数调用运算符()的重载
2017-04-13 16:31
274 查看
一.对下标运算符[]的重载:
重载的格式:返回类型 类名 operator [](参数);
#include<iostream>
using namespace std;
class A
{
public:
A(int n);
~A();
int & operator [](int n);//返回类型为int & 引用类型
private:
int *p;
int size;
};
int & A::operator[](int n){
return p
;//返回p
}
A::A(int n)
{
p = new int
;
}
A::~A()
{
delete[]p;
}
int main(){
A a(5);
a[3] = 4;//等价于a.operator[](3)=4,相当于4赋值给了p[3];
cout << "a[3]=" << a[3] << endl;
cin.get();
return 0;
}
二.对()运算符的重载
#include<iostream>
using namespace std;
class A{
public:
float operator()(double a,double b);
};
float A::operator ()(double a,double b){
return a*b;
}
int main(){
A f;
float a=f(10.0,1.23);//默认double类型
cout<<a<<endl;
return 0;
}
三.对流输出运算符‘<<’和流输出‘>>’的重载
#include<iostream>
using namespace std;
class A
{
public:
A(int n);
~A();
int& operator[](int);//这里用返回值类型为引用类型,完全是效率问题。
friend ostream& operator <<(ostream &otput, A& a);//对<<的重载最好用友员函数
friend istream& operator >>(istream &input, A& b);
private:
int *p;
int size;
};
int& A::operator[](int a){
if (a<size&&a>=0)
return p[a];
cout << "out of size" << endl;
exit(0);
}
ostream& operator <<(ostream &otput, A& a){
for (int i = 0; i < a.size; i++)
otput << a.p[i]<<' ';
cout << endl;
return otput;
}
istream& operator >>(istream &input, A& b){
for (int i = 0; i < b.size; i++){
input >> b.p[i];
}
return input;//这里返回值为istream 类型变量是规定。
}
A::A(int n)
{
p = new int
;
size = n;
}
A::~A()
{
delete[]p;
size = 0;
}
int main(){
A a(5);
cout << "input the elements of A a" << endl;
cin >> a;
//cout << "a[5]" << a[5]<<endl;//直接退出。
cout << "a[3]=" << a[3] << endl;
cout << a<<endl;
system("pause");
return 0;
}
重载的格式:返回类型 类名 operator [](参数);
#include<iostream>
using namespace std;
class A
{
public:
A(int n);
~A();
int & operator [](int n);//返回类型为int & 引用类型
private:
int *p;
int size;
};
int & A::operator[](int n){
return p
;//返回p
}
A::A(int n)
{
p = new int
;
}
A::~A()
{
delete[]p;
}
int main(){
A a(5);
a[3] = 4;//等价于a.operator[](3)=4,相当于4赋值给了p[3];
cout << "a[3]=" << a[3] << endl;
cin.get();
return 0;
}
二.对()运算符的重载
#include<iostream>
using namespace std;
class A{
public:
float operator()(double a,double b);
};
float A::operator ()(double a,double b){
return a*b;
}
int main(){
A f;
float a=f(10.0,1.23);//默认double类型
cout<<a<<endl;
return 0;
}
三.对流输出运算符‘<<’和流输出‘>>’的重载
#include<iostream>
using namespace std;
class A
{
public:
A(int n);
~A();
int& operator[](int);//这里用返回值类型为引用类型,完全是效率问题。
friend ostream& operator <<(ostream &otput, A& a);//对<<的重载最好用友员函数
friend istream& operator >>(istream &input, A& b);
private:
int *p;
int size;
};
int& A::operator[](int a){
if (a<size&&a>=0)
return p[a];
cout << "out of size" << endl;
exit(0);
}
ostream& operator <<(ostream &otput, A& a){
for (int i = 0; i < a.size; i++)
otput << a.p[i]<<' ';
cout << endl;
return otput;
}
istream& operator >>(istream &input, A& b){
for (int i = 0; i < b.size; i++){
input >> b.p[i];
}
return input;//这里返回值为istream 类型变量是规定。
}
A::A(int n)
{
p = new int
;
size = n;
}
A::~A()
{
delete[]p;
size = 0;
}
int main(){
A a(5);
cout << "input the elements of A a" << endl;
cin >> a;
//cout << "a[5]" << a[5]<<endl;//直接退出。
cout << "a[3]=" << a[3] << endl;
cout << a<<endl;
system("pause");
return 0;
}
相关文章推荐
- 详解C++中的函数调用和下标以及成员访问运算符的重载
- C++赋值运算符、函数调用运算符、下标运算符(“=”、“()”、“[]”)重载
- 详解C++中的函数调用和下标以及成员访问运算符的重载
- C++ 函数调用运算符 () 重载
- 函数调用运算符的重载
- 函数调用运算符与重载、类型转换
- 第八周 项目一:任务一(成员函数完成运算符的重载)
- 成员函数和运算符的重载
- 第八周任务二——关于时间函数运算符的重载
- 函数的调用与自增(减)运算符
- 第8周项目1:任务1:用类的成员函数完成运算符的重载
- 调用操作符的重载与函数对象
- 面向对象的程序设计学习笔记-10-[]下标运算符的重载-part1
- 运算符-重载为成员函数 和友元函数的区别.
- x项目一成员函数完成运算符的重载、
- 2013-4-19.1~用类的成员函数完成运算符的重载
- 关于虚函数重载遇到的怪问题 -- 为什么经常调用了基类的函数
- 第八周任务1:方案二:请用类的友元函数,而不是成员函数,完成上面提及的运算符的重载;
- 用成员函数重载算术运算符
- 调用操作符的重载与函数对象