zoukankan      html  css  js  c++  java
  • 第十九章 代码重用 5包含对系统的消耗 简单

    /*
    //5 包含对系统的消耗
    //由于book类的数据成员中包含量了3个String类的对像,因此在创建一个book类对像时也就不可避免的创建3个String类的对像,本节我们通过在String类和book类的构造和析构函数中添车输出语句演示这种消耗
    #include "String.h"
    class Book
    {
    public:
    	Book();
    	~Book(){ cout<<"Book类的析构函数执行...."<<endl;}
    	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){
    	 cout<<"Book类的不带参数构造函数执行...."<<endl;
     
    };
    //创建一本有所有内容的图书
    Book::Book(char* t, char* a, char* n, float p)
    {
    	cout<<"Book类的带参数构造函数执行...."<<endl;
    	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;
    }
    */
    

      

  • 相关阅读:
    将Apache2.4手动安装成Windows的服务
    [译文]PHP千年虫(y2k compliance)
    Apache2.4 authz_core_module模块使用
    Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.错误
    [转载]开启debug调试模式
    thinkphp 去掉URL 里面的index.php
    在WINDOWS下安装PEAR
    php5.5.15注释问题PHP Deprecated: Comments starting with '#' are deprecated in *.ini 警告解决办法
    Maven 与 IntelliJ IDEA 的完美结合
    JavaRebel 2.0 发布,一个JVM插件
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2700893.html
Copyright © 2011-2022 走看看