//矩阵相加
#include<iostream>
using namespace std;
class aa
{
private:
int a[2][3];
public:
aa();
aa operator + (aa c1);
void display();
};
aa::aa()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
a[i][j]=2;
}
}
aa aa:: operator + (aa c1)
{
aa b;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
b.a[i][j]=a[i][j]+c1.a[i][j];
}
}
return b;
}
void aa::display()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
aa a,b,c;
c=a+b;
c.display();
return 0;
}
//运算符重载
#include<iostream>
using namespace std;
class s
{
private:
char *p;
public:
s()
{p=NULL;}
s (char *str);
bool operator >(s &s1);
void display();
};
s::s(char *str)
{
p=str;
}
bool s::operator >(s &s1)
{
if(strcmp(p,s1.p)>0)
{
return true;
}
else
return false;
}
void s::display()
{
cout<<p;
}
int main()
{
s s1("hello"),s2("book");
cout<<(s1>s2)<<endl;
return 0;
}
//单目运算符 i++ ++i
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string name;
int age;
public:
void display();
Student(string n="NULL",int a=0)
{
name=n;age=a;
}
Student operator ++();
Student operator ++(int) ; //后
};
Student Student::operator ++()
{
age++;
return *this;
}
Student Student::operator ++(int)
{
Student a;
a=*this;
age++;
return a;
}
void Student::display()
{
cout<<name<<endl;
cout<<age<<endl;
}
int main()
{
Student s1("jiang",0),s2("jiang",0),s3;
cout<<"s1"<<endl;
s3=s1++;
s1.display();
cout<<"s3=";
s3.display();
cout<<endl<<"s2"<<endl;
s3=++s2;
s2.display();
cout<<"s3=";
s3.display();
return 0;
}