友元及重载

1、什么是友元?友元的存在形式有?友元有何特点?

友元是放在类中,使其可以访问类的私有成员的函数或类;

友元分为友元函数和友元类;

友元可以访问类的私有成员,放在类中不受类的public,private,protected的影响。

2、运算符重载的原则是什么?有哪些规则?

**原则:**是对新类型数据的实际需要对原有的运算符进行的适当改造,重载功能应相应的与原有功能相似,避免没有目的地使用运算符重载;

**规则:**1.不能凭空臆造一个运算符;

2.原有的运算符的优先级和结合性不改变;

3.不改变运算符用法,原有几个操作数,操作数在左边还是右边都不改变;

4.不能有默认参数,否则改变了运算符操作数的个数;

5.重载逻辑运算符后不在具有短路功能;

6.运算符重载必须有一个是枚举或者自定义类型。

3、不能重载的运算符有哪几个?

**1 *. 2 . **3 **?: 4 :: **5 **sizeof

4、运算符重载的形式有哪几种?

1友元函数重载

2成员函数重载

3普通函数重载

5、自增运算符的前置形式和后置形式有什么区别?返回值类型分别是什么?

前置:参数栏不填,返回一个this指针;

后置:参数栏填int作为区分标志,返回一个对象。
实现String类的其它运算符的重载

#include<string.h>                                                                                                                                                         
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
class String 
{
public:
    String(){
        cout<<"string"<<endl;
    }
    String(const char *pstr){
        _pstr = new char[sizeof(pstr)+1];
        strcpy(_pstr,pstr);
    }
    String(const String &hs){
        _pstr = new char[sizeof(hs._pstr)+1];
        strcpy(_pstr,hs._pstr);
    }
    ~String(){
        if(_pstr){
            delete []_pstr;
            _pstr = nullptr;
        }
    }
    String &operator=(const String &);
    String &operator=(const char *);

    String &operator+=(const String &);
    String &operator+=(const char *);

    char &operator[](std::size_t index);
    const char &operator[](std::size_t index) const;

    std::size_t size() const;
    const char* c_str() const;

    friend bool operator==(const String &, const String &);
    friend bool operator!=(const String &, const String &);

    friend bool operator<(const String &, const String &);
    friend bool operator>(const String &, const String &);
    friend bool operator<=(const String &, const String &);
    friend bool operator>=(const String &, const String &);

    friend std::ostream &operator<<(std::ostream &os, const String &s);
    friend std::istream &operator>>(std::istream &is, String &s);

private:
    char * _pstr;
};

String operator+(const String &lhs, const String &rhs){
    String a = lhs;
    return a+=rhs;
}
String operator+(const String &lhs, const char *R){
    String a = lhs;
    return a+=R;
}
String operator+(const char *L, const String &rhs){                           String a(L);
    return a+=rhs;
}
String & String::operator=(const String &Rhs){
    if(this != &Rhs){
        delete []_pstr;
        _pstr = nullptr;
        _pstr = new char[sizeof(Rhs._pstr)+1]();
        strcpy(_pstr,Rhs._pstr);
    }
    return *this;
}
String& String::operator=(const char* R){
    delete []_pstr;
    _pstr = nullptr;
    _pstr = new char[sizeof(R)+1]();
    strcpy(_pstr,R);
    return *this;
}
String& String::operator+=(const String &Rhs){
//    if(_pstr == nullptr){                                                        
//        _pstr = new char[strlen(Rhs._pstr)+1];
//        strcpy(_pstr,Rhs._pstr);
//        return *this;
//    }
//    else if(Rhs == nullptr){
//        return *this;
//    }
//    else{
        cout<<"+=&Rhs"<<endl;
       char *temp = new char[strlen(_pstr)+strlen(Rhs._pstr)+1];
       temp = strcat(_pstr,Rhs.c_str());
//       delete []_pstr;
       _pstr = new char[strlen(temp)+1];
       strcpy(_pstr,temp);
       delete []temp;
       temp = nullptr;
       return *this;
  
//    }
} 
String& String::operator+=(const char*R){
    char* temp2 = new char[strlen(_pstr)+strlen(R)+1]();
    temp2 = strcat(_pstr,R);
//    delete []_pstr;
    _pstr = nullptr;
    _pstr = new char[strlen(temp2)+1]();
    strcpy(_pstr,temp2);
    delete []temp2;
    temp2 = nullptr;
    return *this;
}
char& String::operator[](std::size_t index){
    if(index < strlen(_pstr)){
        return _pstr[index];
    }
    else{
        static char  nullchar = '\0';
        return nullchar;
    }
}
const char& String::operator[](std::size_t index)const{
    if(index < strlen(_pstr)){
        return _pstr[index];
    }
    else{
        static char  nullchar = '\0';
        return nullchar;
    }
}
std::size_t String::size()const{
    if(_pstr != nullptr){
        return sizeof(_pstr);
    }
    return 0;                                                             
}
const char* String::c_str()const{
    if(_pstr){
        const char* tem = _pstr;
        return tem;
    }
    else{
        return 0;
    }
}
bool operator==(const String &lhs,const String &rhs){
    if(lhs._pstr == rhs._pstr){
        return 1;
    }
    return 0;
}
bool operator!=(const String &lhs,const String &rhs){
    if(lhs._pstr != rhs._pstr){
        return 1;
    }
    return 0;
}
bool operator<(const String &lhs,const String &rhs){
    if(strcmp(lhs._pstr,rhs._pstr) < 0){
        return true;
    }
    return false;
}
bool operator>(const String &lhs,const String &rhs){
    if(strcmp(lhs._pstr,rhs._pstr) > 0){
        return true;                                                       
    }
    return false;
}
bool operator<=(const String &lhs, const String &rhs){
    if(strcmp(lhs._pstr,rhs._pstr) <= 0){
        return true;
    }
    return false;
}
bool operator>=(const String &lhs, const String &rhs){
    if(strcmp(lhs._pstr,rhs._pstr) >= 0){
        return true;
    }
    return false;
}
std::ostream &operator<<(std::ostream &os, const String &s){
    os << s._pstr;
    return os;
}
std::istream &operator>>(std::istream &is, String &s){
    is >> s._pstr;
    return is;
}
int main(){
    String s1 = "hello world!";
    cout <<"s1 = "<< s1 << endl;
    String s2 = "nihaohuai";
    cout<<s2<<endl;
    String s3 = s1; 
    cout<<"s3(s1)="<<s3<<endl;
    s3 += s2; 
    cout<<s3<<endl;//打不出来
    cout <<"s2 = " << s2 << endl
        <<"s3 = " << s3 << endl;
    s3+= "i am +=";
    cout << "s3+=char*" << s3 << endl
        <<"s3+=size() = "<<s3.size() << endl;                                      

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值