矩阵类Matrix
#include<iostream>
using namespace std;
class Matrix{
private:
int** ali;
int r,c;
public:
Matrix(int a,int b):r(a),c(b){
ali = new int*[r];
for(int i=0;i<r;i++)
ali[i] = new int[c];
}
int *operator[](int row)
{
return ali[row];
}
int getRows()
{
return r;
}
int getColumns()
{
return c;
}
friend ostream& operator<<(std::ostream& cout, const Matrix& m) {
for (int i = 0; i < m.r; i++) {
for (int j = 0; j < m.c; j++) {
cout << m.ali[i][j] << " ";
}
cout << std::endl;
}
return cout; // 返回流对象以支持链式操作
}
friend Matrix operator+(const Matrix a,const Matrix b)
{
Matrix re(a.r,a.c);
int i,j;
for(i=0;i<a.r;i++)
{
for(j=0;j<a.c;j++)
{
re[i][j]=a.ali[i][j]+b.ali[i][j];
}
}
return re;
}
Matrix operator*(const Matrix b)
{
int i,j;
Matrix re(r,r);
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
int k;
int sum=0;
for(k=0;k<c;k++)
{
sum+=ali[i][k]*b.ali[k][j];
}
re[i][j]=sum;
}
}
return re;
}
};
点类Point的运算符重载
#include<iostream>
#include<cmath>
using namespace std;
class Point{
private:
int x,y;
public:
Point(int a,int b):x(a),y(b){}
friend double operator+(const Point a,const Point b)
{
double x1 = static_cast<double>(a.x - b.x);
double y1 = static_cast<double>(a.y - b.y);
return sqrt(x1*x1+y1*y1);
}
Point operator+(int n)
{
return Point(x+n,y+n);
}
friend ostream& operator<<(std::ostream& cout, const Point& m)
{
cout<<"["<<m.x<<","<<m.y<<"]";
return cout;
}
};
二进制非负整数类Bina
#include<iostream>
#include<cmath>
using namespace std;
class Binary{
private:
int a,b,c,d;
public:
Binary(int a,int b,int c,int d):a(a),b(b),c(c),d(d){
}
int check()
{
int sum=0;
if((a>1||a<0)||(b>1||b<0)||(c>1||c<0)||(d>1||d<0))
return -1;
else
{
sum+=a*pow(2,3);
sum+=b*pow(2,2);
sum+=c*pow(2,1);
sum+=d*pow(2,0);
return sum;}
}
void operator+(Binary t)
{
d = d+t.d;
if(d>=2)
{
d%=2;
c++;}
c = c+t.c;
if(c>=2)
{
c%=2;
b++;}
b = b+t.b;
if(b>=2)
{
b%=2;
a++;}
a = a+t.a;
if(a>=2)
{
a%=2;}
}
friend ostream& operator<<(std::ostream& cout, const Binary& c)
{
cout<<c.a<<c.b<<c.c<<c.d;
return cout;
}
};
MyArray类的运算符重载
#include<iostream>
#include<cmath>
using namespace std;
class MyArray{
private:
int size;
int *ptr;
public:
MyArray(){
}
MyArray(int n):size(n){
ptr = new int[size];
}
MyArray(const MyArray& m) : size(m.size) {
ptr = new int[size];
for (int i = 0; i < size; ++i) {
ptr[i] = m.ptr[i];
}
}
MyArray& operator=(const MyArray& m) {
if (this != &m) {
size = m.size;
ptr = new int[size];
for (int i = 0; i < size; ++i) {
ptr[i] = m.ptr[i];
}
}
return *this;
}
int &operator[](int index) const
{
return ptr[index];
}
int &operator[](int index)
{
return ptr[index];
}
friend MyArray operator+(const MyArray &a,const MyArray &b)
{
int l = a.size;
MyArray p(l);
for(int i=0;i<l;i++)
p.ptr[i] = a.ptr[i] + b.ptr[i];
return p;
}
friend MyArray operator-(const MyArray a,const MyArray b)
{
int l = a.size;
MyArray p(l);
for(int i=0;i<l;i++)
p.ptr[i] = a.ptr[i] - b.ptr[i];
return p;
}
friend int operator*(const MyArray a,const MyArray b)
{
int sum=0;
for(int i=0;i<a.size;i++)
{
sum+=a.ptr[i] * b.ptr[i];
}
return sum;
}
friend ostream& operator<<(ostream& os,const MyArray &a)
{
int i;
for(i=0;i<a.size-1;i++)
{
os<<a[i]<<",";
}
os<<a[i]<<endl;
return os;
}
int getSize() const {
return size;
}
};
MyString类的运算符
ostream& operator<<(ostream& out, const MyString& str)
{
out << str.ptr;
return out;
}
int operator==(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) == 0)return 1;
else return 0;
}
int operator!=(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) == 0)return 0;
else return 1;
}
int operator<(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) < 0)return 1;
else return 0;
}
int operator<=(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) > 0)return 0;
else return 1;
}
int operator>(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) > 0)return 1;
else return 0;
}
int operator>=(const MyString& s1, const MyString& s2)
{
if (strcmp(s1.ptr, s2.ptr) < 0)return 0;
else return 1;
}
MyString operator+(const MyString& s1, const MyString& s2)
{
MyString s;
s.ptr = new char[strlen(s1.ptr) + strlen(s2.ptr) + 1];
strcpy(s.ptr, s1.ptr);
strcat(s.ptr, s2.ptr);
return s;
}
MyString &MyString :: operator=(const MyString& s)
{
ptr = new char[strlen(s.ptr) + 1];
strcpy(ptr, s.ptr);
return *this;
}
MyString &MyString :: operator+=(const MyString& s)
{
char* temp;
temp = ptr;
ptr = new char[strlen(temp) + strlen(s.ptr) + 1];
strcpy(ptr, temp);
delete[]temp;
strcat(ptr, s.ptr);
return *this;
}
char MyString::operator[](int i)
{
return this->ptr[i];
}
|
#include<iostream>
using namespace std;
class Complex{
private:
double real,imag;
public:
Complex(double a,double b):real(a),imag(b){}
friend Complex operator*(Complex &a,Complex &b)
{
double real1 = a.real*b.real - a.imag*b.imag;
double imag1 = a.real*b.imag + a.imag*b.real;
Complex p(real1,imag1);
return p;
}
void operator+(int n)
{
real+=n;
imag+=n;
}
friend ostream &operator<<(ostream& os, const Complex& a) {
os << a.real << "+" << a.imag << "i" << endl;
return os;
}
};
Circle类的运算符重载 |
#include<iostream>
using namespace std;
const double pi = 3.14159265;
class Circle{
private:
double x,y,radius;
public:
Circle(double a,double b,double c):x(a),y(b),radius(c){}
friend double operator+(Circle a,Circle b)
{
double sum = 3.14159265*a.radius*a.radius + 3.14159265*b.radius*b.radius;
return sum;
}
void operator+(double n)
{
x+=n;
y+=n;
radius+=n;
}
friend ostream& operator<<(ostream& out,Circle &a)
{
out<<"圆心:"<<a.x<<","<<a.y<<" 半径:"<<a.radius<<endl;
return out;
}
};
Time类的运算符重载
#include<iostream>
#include<cmath>
using namespace std;
class Time{
private:
int hour,minute,second;
int sumsecond;
public:
Time(int a,int b,int c):hour(a),minute(b),second(c){
int sum = 0;
sum+=hour*60*60;
sum+=minute*60;
sum+=second;
sumsecond = sum;
}
Time operator+(int n)
{
sumsecond+=n;
sumsecond%=(60*60*24);
int hourn = n/(60*60);
n%=60*60;
int minuten = n/60;
n%=60;
int secondn = n;
hour += hourn;
minute += minuten;
second += secondn;
if(second>=60)
{
minute += second/60;
second%=60;
}
if(minute>=60)
{
hour += minute/60;
minute%=60;
}
if(minute>=24)
hour%=24;
return *this;
}
Time operator-(int n)
{
n%=(60*60*24);
int sum = sumsecond - n;
if(sum<0)
sum+=60*60*24;
sumsecond = sum;
int h = sum/(60*60);
sum%=3600;
int m = sum/60;
sum%=60;
int s = sum;
Time a(h,m,s);
return a;
}
int operator-(Time a)
{
return sumsecond - a.sumsecond;
}
friend ostream &operator<<(ostream& os,Time &m)
{
os<<m.hour<<":"<<m.minute<<":"<<m.second<<endl;
return os;
}
};
自定义字符串类MyStri |
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class MyString {
private:
char *sptr;
int n = 0;
public:
MyString() {
cout << "Object Constructed. No Memory Allocated." << endl;
sptr = NULL;
}
MyString(const string &a) {
n = a.size();
sptr = new char[n + 1];
strcpy(sptr, a.c_str());
cout << "Object Constructed. " << n + 1 << " Bytes Allocated." << endl;
}
MyString(const MyString &a)
{
n = a.n;
sptr = new char[n + 1];
strcpy(sptr, a.sptr);
cout << "Object Constructed. "<<n+1<<" Bytes Allocated." << endl;
}
MyString& operator=(const MyString &a)
{
if (this != &a) {
delete[] sptr;
n = a.n;
sptr = new char[n + 1];
strcpy(sptr, a.sptr);
}
return *this;
}
void printString() {
if (n == 0)
cout << "No Memory Allocated In This Object." << endl;
else
cout << sptr << endl;
}
int getSize() {
return n;
}
void stringCopy(const MyString &a)
{
delete[] sptr;
int l=n;
n = a.n;
sptr = new char[n + 1];
strcpy(sptr, a.sptr);
cout <<"String Copy, "<<n+1<<" Bytes Reallocated And "<<l+1<<" Bytes Freed."<< endl;
}
void stringCat(const MyString &a) {
int newSize = n + a.n + 1;
int l = n;
char *t = new char[newSize];
strcpy(t, sptr);
strcpy(t + n, a.sptr);
delete[] sptr;
sptr = t;
n = newSize - 1;
cout <<"String Connection, "<<n+1<<" Bytes Reallocated And "<<l+1<<" Bytes Freed."<< endl;
}
~MyString()
{
if(n==0)
cout<<"Object Destructed. No Memory Freed."<<endl;
else
cout<<"Object Destructed. "<<n+1<<" Bytes Freed."<<endl;
delete []sptr;
}
};
this的使用
#include<iostream>
using namespace std;
class Test{
private:
int x,y;
public:
Test(int a,int b):x(a),y(b){}
Test *setXY1(int a,int b)
{
x+=a;
y+=b;
return this;
}
Test &setXY2(int a,int b)
{
x-=a;
y-=b;
return *this;
}
void showXY()
{
cout<<x<<" "<<y<<endl;
}
};