zoukankan      html  css  js  c++  java
  • 林锐书:写一个hello world by seasoned professional

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    
    class String
    {
    private:
        int size;
        char *ptr;
    public:
        String():size(0),ptr(new char(''))
        { cout << "default
    "; }
    
        String(const String &s):size(s.size)
        {
            cout << "1 param constructor
    ";
            ptr = new char[size + 1];
            strcpy(ptr,s.ptr);
        }
        
        //my code
        String(char *s)
        {
            cout << "2 param constructor
    ";
            ptr = new char[strlen(s) + 1];
            strcpy(ptr,s);
        }    
        
        ~String()
        {
            delete []ptr;
        }
    
        friend ostream& operator<<(ostream &,const String & );
        String& operator=(const String &s);
    };
    
    //c++ primer第四版中文,435页
    //14.2.1 输出操作符的重载:为了与io标准库一直,操作符应接受ostream&作为第一个参数,对类类型const对象的引用作为第2个参数,并返回对ostream形参的引用。
    /*friend*/ ostream& operator<<(ostream & os,const String &s)  //这里的friend去掉了!就像类内的static在外面定义时要去掉一样。
                                                              //不去掉的话会报错 error: ‘friend’ used outside of class
    {
        return (os << s.ptr);
    }
    
    String& String::operator=(const String &s)
    {
        cout << "operator=
    ";
        if(this != &s)
        {
            delete []ptr;
            ptr = new char[strlen(s.ptr) + 1];
            strcpy(ptr,s.ptr);
        }
        return *this;
    }
    
    int main()
    {
        String s1 = "hello world";
        String s2 = s1;
        s2 = s1;
        
        cout << s1 <<endl;
        cout << s2 <<endl;
    
        return 0;
    }

    /work/ctest/public_private$ ./1
    2 param constructor
    1 param constructor
    operator=
    hello world
    hello world

     /work/ctest/public_private$ ./1
    2 param constructor
    1 param constructor
    operator=
    hello world
    hello world

    下面的是林锐的书的原来的代码

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    
    class String
    {
    private:
    	int size;
    	char *ptr;
    
    public:
    	String():size(0),ptr(new char(''))
    	{ cout << "default
    "; }
    
    	String(const String &s):size(s.size)
    	{
    		cout << "1 param constructor
    "; 
    		ptr = new char[size + 1];
    		strcpy(ptr,s.ptr);
    	}
    	
    	~String()
    	{
    		delete []ptr;
    	}
    
    	friend ostream& operator<<(ostream &,const String & );
    	String& operator=(const char *s);
    };
    
    ostream& operator << (ostream & os,const String &s)  
    {
    	return (os << s.ptr);
    }
    
    String& String::operator=(const char *s)
    {
    	cout << "operator=
    "; 
    	//if(this != &s)//这一行代码要注释掉,不然会 error: comparison between distinct pointer types ‘String*’ and ‘const char**’ lacks a cast
    	{
    		delete []ptr;
    		ptr = new char[strlen(s) + 1];
    		strcpy(ptr,s);
    	}
    	return *this;
    }
    
    int main()
    {
    	String s1 ;
    	s1 = "Hello World";
    
    	cout << s1 <<endl;
    
    	return 0;
    }
    

     /work/ctest/public_private$ ./1
    default
    operator=
    Hello World

  • 相关阅读:
    stress工具使用指南和结果分析
    copy.c实现
    sysbench测试阿里云CPU
    sysbench测试阿里云ECS云磁盘的IOPS,吞吐量
    iostat详解
    sysbench_fileio.sh
    rm -f /var/lib/rpm/__db*;rpm --rebuilddb
    HeadFirst 13 (包装器, 过滤器) not Finish
    基于Linux的oracle数据库管理 part5( linux启动关闭 自动启动关闭 oracle )
    基于Linux的oracle数据库管理 part4( shell管理 上 )
  • 原文地址:https://www.cnblogs.com/jeanschen/p/3407176.html
Copyright © 2011-2022 走看看