c++期末复习 重载运算符

查漏补缺:

在priavte的函数中 *类型的永远要注意有没有用new函数分配内存 否则会报错

#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char * s;
public:
String();
String(const char *);
String(const String &);
~String();
String & operator=(const char *);
String & operator=(const String &);
String operator+(const char *);
String operator+(const String &);
String & operator+=(const char *);
String & operator+=(const String &);
friend istream & operator>>(istream &, String &);
friend ostream & operator<<(ostream &, const String &);
friend bool operator==(const String &, const char *);
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const char *);
friend bool operator!=(const String &, const String &);
};
String::String() : s(new char[1]) { s[0] = '\0'; }
String:: String(const char *a) : s(new char[strlen(a) + 1]) {
        strcpy(s, a);
    }

String::String(const String &o) {
        s = new char[strlen(o.s) + 1];
        strcpy(s, o.s);
    }
String::~String() { delete[] s; }
bool operator==(const String &a, const char *string){
    return strcmp(a.s,string)==0;
}
bool operator==(const String &a, const String &b){
return strcmp(a.s,b.s)==0;
}
bool operator!=(const String &a, const char *string){
    return !strcmp(a.s,string)==0;
}
bool operator!=(const String &a, const String &b){
return !strcmp(a.s,b.s)==0;
}
String & String::operator=(const char *a){
    if(s!=a){
        delete[] s;
    }
    strcpy(s,a);
    return *this;
}
String & String::operator=(const String &a){
        if(s!=a.s){
        delete[] s;
    }
    strcpy(s,a.s);
    return *this;
}
String String::operator+(const char *a){
    String temp;
    temp.s=new char[strlen(s)+strlen(a)+1];
    strcpy(temp.s,s);
    strcat(temp.s,a);
    return temp;
}
String String::operator+(const String &a){
    String temp;
    temp.s=new char[strlen(s)+strlen(a.s)+1];
    strcpy(temp.s,s);
    strcat(temp.s,a.s);
    return temp;
}
//String & String::operator+=(const char *a){
//        String temp;
//    temp.s=new char[strlen(s)+strlen(a)+1];
//    strcpy(temp.s,s);
//    strcat(temp.s,a);
//    delete []s;
//    strcpy(s,temp.s);
//    return *this;
//}
String & String::operator+=(const char *str) {
   char *temp;
    temp= new char[strlen(s)+strlen(str)+1];
    strcpy(temp, s);
    strcat(temp, str);
    delete[] s;
    s = temp;
    return *this;
}
String &String::operator+=(const String &other) { return *this += other.s; }
istream & operator>>(istream &is, String &a){
    is>>a.s;
    return is;
}
ostream & operator<<(ostream &os, const String &a){
    os<<a.s;
    return os;
}

int main()
{
String s;
s += "hello";
cout<<s<<endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}

方式 

友元函数 成员函数

1、friend TYPE operator@(形参表);

    friend Complex operator+(const Complex &, const Complex &);
    friend Complex operator-(const Complex &, const Complex &);
    friend ostream & operator<<(ostream &, const Complex &);
    friend istream & operator>>(istream &, Complex &)

 重载运算符

自增自减运算符重载


#include<iostream>
using namespace std;
class CheckedPtr

{

public:

CheckedPtr(int * b, int * e) : beg(b), end(e), curr(b) {  }

CheckedPtr & operator ++(); // prefix ++

CheckedPtr & operator --(); // prefix --

CheckedPtr   operator ++(int); // postfix ++

CheckedPtr   operator --(int); // postfix --

int * GetBeg();

int * GetEnd();

int * GetCurr();

private:

int * beg;  // pointer to beginning of the array

int * end;  // one past the end of the array

int * curr; // current position within the array

};
int* CheckedPtr::GetBeg() {
    return beg;

int*  CheckedPtr::GetCurr(){
    return curr;

int* CheckedPtr::GetEnd(){
    return end;
    
}


CheckedPtr & CheckedPtr::operator ++(){
    if(curr<end) ++curr;
    return *this;
}

CheckedPtr & CheckedPtr::operator --(){
        if(curr>beg) --curr;
    return *this;
}

CheckedPtr CheckedPtr:: operator ++(int){
    CheckedPtr temp(*this);
    ++(*this);
    return temp;
}

CheckedPtr CheckedPtr ::operator --(int){
    CheckedPtr temp(*this);
        --(*this);
        return temp;
}


int main(){

int n;

cin>>n;

int * array = new int[n];

for(int i=0;i<n;i++)

cin>>array[i];

CheckedPtr cp(array, array+n);

for(;cp.GetCurr()<cp.GetEnd();cp++)

cout<<*cp.GetCurr()<<" ";

cout<<endl;

for(--cp;cp.GetCurr()>cp.GetBeg();cp--)

cout<<*cp.GetCurr()<<" ";

cout<<*cp.GetCurr()<<endl;

delete [] array;

return 0;

}
 

下标重载

v 容器类一般会定义下标操作符 operator[] ,必须定义为成员函数
v 定义下标操作符时,一般需要两个版本,一个为非 const 成员并返回引用,另一个为 const 成员并返回 const 引用
v 返回引用类型,可作为左值也可作为右值
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值