重载运算符了
来源:互联网 发布:刘昊然网络剧 编辑:程序博客网 时间:2024/06/06 03:04
X& X::operator=(const X& source){
}
友元运算符函数的原型在类内部声明格式:
类内部:
Class X{
//
friend 返回类型 operator运算符(形参表);
//
}
类外部:
返回类型 operator运算符(形参表)
{
函数体
}
成员运算符函数定义的语法形式
类内部:
class X{
//
返回类型 operator运算符(形参表);
//
}
返回类型 X::operator运算符(形参表)
{
函数体
}
复数:
class complex{
public:
double real,imag;
complex operator+(complex om1,complex om2){
complex temp;
temp.real = om1.real + om2.real;
temp.imag = om1.imag + om2.imag;
return temp;
}
complex operator++(){ //operator++(ob) ob为complex类引用对象
++real;
++imag;
return *this;
}
complex operator++(0){ //operator++(ob,0)
real++;
imag++;
return *this;
}
complex(double r=0, double i=0){
real = r;
imag = i;
}
}
矩阵加法重载;矩阵输出重载
#include <iostream>
using namespace std;
class CMatrix23
{
private:
int a[2][3];
public:
CMatrix23 operator + ( const CMatrix23& mat );
friend ostream& operator << ( ostream& os,const CMatrix23& mat );
friend istream& operator >> ( istream& is,CMatrix23& mat );
};
CMatrix23 CMatrix23::operator + (const CMatrix23 &mat)
{
CMatrix23 tmp;
for( int i=0;i<2;++i )
{
for( int j =0;j<3;++j )
{
tmp.a[i][j] = this->a[i][j] + mat.a[i][j];
}
}
return tmp;
}
ostream& operator << ( ostream& os,const CMatrix23& mat )
{
for( int i=0;i<2;++i )
{
for( int j =0;j<3;++j )
{
os<<mat.a[i][j]<<" ";
}
os<<endl;
}
return os;
}
istream& operator >> ( istream& is,CMatrix23& mat )
{
for(int i=0;i<2;++i)
{
for(int j=0;j<3;++j)
{
cout<<"请输入第"<<i<<"行第"<<j<<"列的元素:";
is>>mat.a[i][j];
}
}
return is;
}
int main()
{
CMatrix23 mat1;
cin>>mat1;
cout<<mat1;
CMatrix23 mat2;
cin>>mat2;
cout<<mat2;
cout<<mat1+mat2<<endl;
return 0;
}
- 重载运算符了
- 字符串类改进版(加入了运算符重载)
- 运算符重载时到底发生了什么?【DEBUG】
- 赋值运算符重载函数被继承了吗?
- std::vector::iterator重载了下面哪些运算符
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- LINQ to SQL学习小结
- 电容式电压互感器
- Symbian DLL(Dynamic Link Library 动态链接库)
- A tune on the RAC.
- 用户界面设计规范
- 重载运算符了
- socket 编程入门教程
- Ubuntu10.04 LTS 下编译Android
- java排序算法学习(一)--冒泡排序
- 在ubuntu 10.04中添加java5
- 群发邮件
- 测试人员的目的——满足“需求”
- 如何解决发布Web services后无法访问
- java排序算法学习(一)--选择排序