作业,实现运算符的重写

#include<iostream>
#include<cstring>

using namespace std;

class Parent{
	private:
		char *a;
		int len; 
	public:
		Parent(){
			a = nullptr;
			len = 0;
		}
		Parent(const char *n){
			if(nullptr != n){
				len = strlen(n);
				a = new char(len+1);
				strcpy(a,n);
			}
			else{
				a = nullptr;
				len = 0;
			}
		}
		~Parent(){
			delete []a;
			a = nullptr;
		}
		
		Parent(const Parent& other){
			len = other.len;
			a = new char[len+1];
			strcpy(a,other.a);
		} 
		
		Parent& operator=(const Parent& other){
			if(&other != this){
				len = other.len;
				if(a == nullptr)
					a = new char[len+1];
				strcpy(a,other.a);	
			}			
			return *this;
		} 
		
		const Parent operator+(Parent &R) const{
            Parent x;
        	if(a!=nullptr&&R.a!=nullptr){
           	 	x.len=len+R.len;
	          	x.a=new char[len+1];
	       		strcpy(x.a,a);
	    		strcat(x.a,R.a);
	        	return x;
			}  
        	else
        	{
               cout << "空字符串无法拼接" << endl;
               return x;
        	}
    	}
    	
    	const bool operator!=(Parent &R)const{
           if((a!=nullptr)&&(R.a!=nullptr)){
            	if(strcmp(a,R.a)==0)
                	return 0;
            	else
                	return 1;
        	}
        	else{
            	cout << "字符串不存在" << endl;
            	return 0;
        	}
    	}
    	
    	const bool operator==(Parent &R)const{
        	if(strcmp(a,R.a)==0)
            	return 1;
        	return 0;
    	}
    	
    	const bool operator<(Parent &R)const{
	        int x=strcmp(a,R.a);
	        if(x<0)
	            return 1;
	        else
	            return 0;
    	}
    	
    	const bool operator<=(Parent &R)const{
	        int x=strcmp(a,R.a);
	        if(x<0||x==0)
	            return 1;
	        return 0;
	    }
       
        const bool operator>(Parent &R)const{
	        int x=strcmp(a,R.a);
	        if(x>0)
	            return 1;
	        return 0;
	    }
		
		const bool operator>=(Parent &R)const{
	        int x=strcmp(a,R.a);
	        if(x>0||x==0)
	            return 1;
	        return 0;
	    }
	    
	    const int size()const{
        	return strlen(a);
    	}
    	
    	const char *res(){
        	if(a!=nullptr)
        	{
        	char *s=nullptr;
        	s=a;
        	return s;
        	}
           else{
            	cout << "空字符串无法展示" << endl;
            	return nullptr;
        	}
		}

		
		friend ostream& operator<<(ostream& L,const Parent& R);
   		friend istream& operator>>(istream& L, Parent& R);
};

	ostream& operator<<(ostream& L, const Parent& R){
	    L <<"字符串="<< R.a <<"长度=" << R.size() <<endl;
	    return L;
	}

	istream& operator>>(istream& L, Parent& R){
	    cout<<"请输入\n";
	    L >>R.a;
	    return L;
	}


int main(){
	Parent s1("hello");
	Parent s2("word");
	Parent s3;
	s3=s1+s2;
	cout << "s3=" << s3.res() << endl;

	bool ret=s1!=s2;
	cout << "s1!=s2 " << ret << endl;

	ret=s1==s2;
	cout << "s1==s2 " << ret << endl;

	ret=s1<s2;
	cout << "s1<s2 " << ret << endl;

	ret=s1<=s2;
	cout << "s1<=s2 " << ret << endl;

	ret=s1>s2;
	cout << "s1>s2 " << ret << endl;

	ret=s1>=s2;
	cout << "s1>=s2 " << ret << endl;

	cin>>s3;
	cout <<s3;

	return 0;

} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值