zoukankan      html  css  js  c++  java
  • 第十九章 代码重用 4为图书重载GetAuthor函数 简单

    // 4 为图书重载GetAuthor函数
    //本节我们再创建一本图书,这本书是由两个合写的,为了记录这两个人的姓名,我们需要定义一个字符串
    #include "String.h"
    class Book
    {
    public:
    	Book();
    	Book(char*, char*, char*, float);
    	//不能修改返回值,在函数内也不能修改,也不想调用复制构造函数,按地址传递
    	const String& GetTitle()const{ return title; }
    	const String& GetAuthor()const{ return author; }
    	
    	String& GetAuthor(){ 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;
    	}
    
    	//这里主要是
    	//这是因为love.GetAuthor()和hoat.GetAuthor()函数返回的都是String类对像author,所以这会调用String类的operator+()函数来执行对两个对象的相加操作
    	//由于GetAuthor()会返回一个const对像
    	//const对像只能调用const成员函数
    	//假如你不想修改String类的operator+()函数,那么还可以通过将book类的成员函数GetAuthor()重载为返回一个非常量对像的函数
    	//第九行将GetAuthor()成员函数重载为一个返回非const对像的非const成员函数,这样,我们就可以执行String类非const对像的相加操作
    
    
    	String loveAndHate = love.GetAuthor() + hoat.GetAuthor();
    	Book LoveHoat;
    	LoveHoat.SetAuthor(loveAndHate);
    	LoveHoat.SetTitle("新书");
    	cout<<"书名:"<<LoveHoat.GetTitle()<<endl;
    	cout<<"作者名:"<<LoveHoat.GetAuthor()<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    多线程(一) NSThread
    Swift 烧脑体操(一)
    Swift 烧脑体操(二)
    UINavigationController使用的注意事项
    更多请查看我的文章
    本地通知
    网络编程(二)NSURLSessionConfiguration
    A
    51Nod 1116 K进制下的大数(暴力枚举)
    51Nod 1065 最小正子段和
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2700875.html
Copyright © 2011-2022 走看看