第十九章 代码重用 3类书类 简单

#include <iostream>
using namespace std;
class String
{
public:
	String(); //默认的构造函数
	~String(){ delete []str; len=0; 
	      //cout<<"析构函数执行"<<endl; 
	 }
	String(const char*const ch); //构造带值的string
	
	int getlen()const { return len;}   //读取长度
	//const char *getstr()const{ return str;} //读取字符串

	//重载输出函数
	friend ostream&operator<<(ostream &o, const String &s)
	{
		o<<s.str;
		return o;
	}
    
	friend istream&operator>>(istream &i, String &s)
	{
		 i>>s.str;
	     return i;
	}
	//当运算符重载函数定义为成员函数时,二元运算符只带一个参数
	//将这个函数定义为友元函数即可
	friend bool operator<(const String&str1, const String&str2)
	{
		if(strcmp(str1.str, str2.str) < 0){
		    return 1;
		}else
			return 0;
	}
	friend bool operator>(const String&str1, const String&str2)
	{
		if(strcmp(str1.str, str2.str) > 0){
		    return 1;
		}else
			return 0;
	}
	friend bool operator==(const String&str1, const String&str2)
	{
		if(strcmp(str1.str, str2.str) == 0){
		    return 1;
		}else
			return 0;
	}


	//这里是可以修改的
	char &operator[](unsigned short int  length);
	char  operator[](unsigned short int  length)const;

	//复制构造函数
	String (const String&r);

	//重载赋值运算符=
	String &operator=(const String &s);

	String operator+(const String&s); //重载相加运算符
	void operator+=(const String&s); //重载相加运算符
	

private:
	String(unsigned short int); //构造带值的string
	unsigned short int len;
	char *str;
};
//创建一个空的str变量
String::String()
{
    len = 0;
	str = new char[1];
	str[0] = '\0';
};
String::String(const char*const ch)
{
	//cout<<"带一个参数的构造函数"<<endl;
	len = strlen(ch);
	str = new char[len+1];
	for(int i=0; i<len; i++){
	   str[i] = ch[i];
	}
	str[len] = '\0';
};


String::String(unsigned short int lenght)
{
	//cout<<"带一个int参数的构造函数"<<endl;
	len = lenght;
	str = new char[len+1];
	for(int i=0; i<=len; i++){
	   str[i] = '\0';
	}
};

char & String::operator[](unsigned short int length)
{
	    if(length > len){
		   return str[len-1]; //返回可见字符的值
		}else{
		   return str[length];
		}
};

char String::operator[](unsigned short int length)const
{
	    //cout<<"下标运算符const执行"<<endl;
		if(length > len){
		   return str[len-1]; //返回可见字符的值
		}else{
		   return str[length];
		}
};

String::String (const String&rs)
{
	len = rs.getlen();
	str = new char[len+1];
	for(int i=0; i<len; i++){
	    str[i] = rs[i]; 
		//这里因为我们构造一个新对象并且用旧对象来为它赋值,很明显,不会修改旧对象的值,所以旧对象rs在调用operator[]const函数的时候,不用将指定字符串的地址返回,只需要要按值返回这个字符即可
		//第二次重载的operator[]运算符函数,按值返回的一个字符,同时在函数体前面加了一个const
		//表示该函数可以操作const对象,也就是rs
		//这样由于2个同名的函数,它的类型不同,一个可操作const对象,一个不可以,这样就可以做到了对函数的重载
	}
	str[len]='\0';
	//cout<<"复制构造函数完成:"<<str<<endl;

};

String& String::operator=(const String &s)
{
	//cout<<"operator=执行"<<endl;
	if(this == &s)
	{
	    return *this;
	}else{
	    delete []str; //删除左边的字符串
		len = s.getlen();
		str = new char[len+1];
		for(int i=0; i<len; i++){
		    str[i] = s[i];
		}
		str[len] = '\0';
	}
	return *this; //注意按引用返回,也就是别名
}

String String::operator+(const String &s)
{
	//cout<<"operator+执行......"<<endl;
	int total = len + s.getlen();
	String temp(total);
	int i, j;
	for(i=0; i<len; i++){
	     temp[i] = str[i];
	}
	for(j=0; j<s.getlen(); j++, i++){
	    temp[i] = s[j];
	}
	return temp;
}



void String::operator+=(const String&s)
{
	//cout<<"这里执行的是相+=运算符"<<endl;
	int total = len + s.getlen();
	String temp(total);
	int i, j;
	for(i=0; i<len; i++){
	    temp[i] = str[i];
	}
	for(j=0; j<s.getlen(); j++,i++){
	    temp[i] = s[j];
	}
	*this = temp;
}



//3 类书类
/*
#include "String.h"
class Book
{
public:
	Book();
	Book(char*, char*, char*, float);
	//不能修改返回值,在函数内也不能修改,也不想调用复制构造函数,按地址传递
	const String& GetTitle()const{ return title; }
	const String& GetAuthor()const{ return author; }
	const String& GetNumber()const{ return number; }
	float GetPrice()const{ return price; }

	void SetTitle(const String& s){ title = s; }
	void SetAuthor(const String& s){ author = s; }
	void SetNumber(const String& s){ number = s; }
	void SetPrice(float p){ price = p; }

	void SetTotal(const String&t, const String&a,const String&n, float p)
	{
	      title = t;
		  author = a;
		  number = n;
		  price = p;
	}
	
private:
    String title;  //书名
	String author; //作者
	String number; //编号
	float price;   //价格
};
//创建一本空图书
Book::Book():title(""),author(""),number(""),price(0){};
//创建一本有所有内容的图书
Book::Book(char* t, char* a, char* n, float p)
{
	title = t; author=a; number = n; price=p;
}

int main()
{
	Book love("love","Jack","001",35.5);
	cout<<"书名:"<<love.GetTitle()<<endl;
	cout<<"作者:"<<love.GetAuthor()<<endl;
	cout<<"编号:"<<love.GetNumber()<<endl;
	cout<<"价格:"<<love.GetPrice()<<endl;
	love.SetPrice(55.5);
	cout<<"价格:"<<love.GetPrice()<<endl;

	love.SetTotal("hate","mak","002",39.9);
	cout<<"书名:"<<love.GetTitle()<<endl;
	cout<<"作者:"<<love.GetAuthor()<<endl;
	cout<<"编号:"<<love.GetNumber()<<endl;
	cout<<"价格:"<<love.GetPrice()<<endl;

	Book hoat;
	hoat = love;
	cout<<"书名:"<<hoat.GetTitle()<<endl;
	cout<<"作者:"<<hoat.GetAuthor()<<endl;
	cout<<"编号:"<<hoat.GetNumber()<<endl;
	cout<<"价格:"<<hoat.GetPrice()<<endl;
	if(love.GetNumber() == hoat.GetNumber()){
	   cout<<"两本书的编号相同"<<endl;
	}else{
	   cout<<"两本书的编号不相同"<<endl;
	}
	hoat.SetNumber("003");
	if(love.GetNumber() == hoat.GetNumber()){
	   cout<<"两本书的编号相同"<<endl;
	}else{
	   cout<<"两本书的编号不相同"<<endl;
	}

    return 0;
}

*/

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2700872.html