zoukankan      html  css  js  c++  java
  • 泡茶看数据结构-临时(对象设计技术)

    一.开场白

      第二次《DATA STRUCTURES AND PROBLEM SOVLING WITH C++》英文授课,让我产生英文写文章的念头。但是,慢慢开始参合英文吧。一下子写的话,怕写出来博客园首页都不敢收录了。^_^!今天,从课堂和自己下午看总结下,关于Object Design Techniques的3点:

      ①Program Design

      ②Handling RunTime Errors

      ③Operator Overloading[操作符重载]

    二.程序设计-Program Design

      #关于程序设计,程序或大或小,出现必然有它的原因。

        1)这就是计算机程序出现于一个客户想要解决的问题。

        2)计算机程序开始于对问题的分析,然后产生的程序是可靠的,容易维护的。

          这点很难,可靠不是绝对的,相对可靠慢慢接近。而容易维护,则建立在demo功底上,慢慢重构自己的demo才能有所为,达到容易维护。

           如图:

                 

       

        #软件生命周期 the software development life cycle

        1)工程师添加该软件demo然不考虑对系统的集成。

        2)系统的退化导致系统难更新,工程师需要创造新的软件代替。

          第一点我没体会出来。先记下了。

        如图:

                    

      #Requst → Analysis → Design → Implementation → Testing → Maintenance

        这点我就不细说了,一个开放的流程。我也体会着。

    三.Handling RunTime Errors

      书上分三种,主要的是 Terminate Program , Set a Flag ,C++ Exceptions。我觉得不止,待会查查跟新上去。

        1)终止程序 Terminate Program 

          在c++标准库(d_time24.h)中的实现(这是老版本中):

                 exit(1); //终止程序
    time24 time24::duration(const time24& t)
    {
    	// convert current time and time t to minutes
    	int currTime = hour * 60 + minute;
    	int tTime = t.hour * 60 + t.minute;
    
    	// if t is earlier than the current time, throw an exception
    	if (tTime < currTime)
           {
                cerr << "time24 duration(): argument is an earlier time";
               exit(1); 
            }  
    	else
    		// create an anonymous object as the return value
    		return time24(0, tTime-currTime);
    }        
    

           2)C++ Exceptions 抛异常

           在c++标准库(d_time24.h)中的实现(这是新版本中): 

                   throw rangeError("time24 duration(): argument is an earlier time");//抛出异常
    time24 time24::duration(const time24& t)
    {
    	// convert current time and time t to minutes
    	int currTime = hour * 60 + minute;
    	int tTime = t.hour * 60 + t.minute;
    
    	// if t is earlier than the current time, throw an exception
    	if (tTime < currTime)
    		throw rangeError(
    			"time24 duration(): argument is an earlier time");
    	else
    		// create an anonymous object as the return value
    		return time24(0, tTime-currTime);
    }
    

          3)Set a Flag 

          最常用的然后是 Bool,然后结合判断语句来进行。

    四.运算符重载 Operator Overloading

       课文中学的不是很清楚,看清楚了我会更新上去。找了下人家的文章,http://www.cnblogs.com/jiaohuang/archive/2011/03/26/2079111.html

       1)在类中重载+=操作符

                  赋值操作符必须定义为成员函数,无论形参为何种类型。赋值必须返回*this 的引用

       2)在类外面的重载+

       3)在类外面的输入输出操作符

        

       自己更新内容:

         1)定义:

          String 里面 有个 equals()方法 和 == 相似。所以这些函数的操作和数字的算术运算相似,定义为重载运算符。

         2)例子:

            Stream I/O Operators 

    (Output) <<:
    friend ostream& operator<<(ostream& ostr, const className& obj);
    
    (Input) >>:
    istream& operator>>(istream& istr, className& obj);

        3)重载运算符两种方法:

           #实现使用运算符函数的自由函数

              运算符可以重载为自由函数或类成员函数。使用运算符函数形式可以讲运算符重载为自由函数。在c++标准库(d_time24.h)中,用于比较time24两个对象,

           ==的形式为:

    bool operator== (const time24& lhs, const time24& rhs);

           它的实现必须是公有成员函数访问数据:运算符 == (自由格式函数)

    // compare hours and minutes
    bool operator== (const time24& lhs, const time24& rhs)
    {
        return lhs.hour == rhs.hour && lhs.minute == rhs.minute;
    }

           #将运算符函数申明为类的友元

             这个函数不是类成员,可以访问类private部分,这种技术避免调用类成员。友元函数(友元)可以使用类中的私有成员函数的自由函数,是直接在函数原型前加上关键字 firend 。友元不是类的成员函数。在c++标准库(d_time24.h)中,+运算符:

          friend time24 operator+ (const time24& lhs, const time24& rhs);
                // form and return lhs + rhs
          friend time24 operator+ (const time24& lhs, int min);
                // form and return lhs + min
                // Precondition:  min must be >= 0
          friend time24 operator+ (int min, const time24& rhs);
                // form and return min + rhs
                // Precondition:  min must be >= 0

              可以从实现中看出,用多种运算符对增加对象的时间增加了灵活性。  

    // create an anonymous object with hour = lhs.hour + rhs.hour
    // and minute = lhs.minute+rhs.minute.
    time24 operator+ (const time24& lhs, const time24& rhs)
    {
        return time24(lhs.hour+rhs.hour, lhs.minute+rhs.minute);
    }
    
    // create an anonymous object with hour = lhs.hour and
    // minute = lhs.minute + min.
    time24 operator+ (const time24& lhs, int min){
        return time24(lhs.hour, lhs.minute + min);
    }
    
    // return the value rhs + min that is computed by
    //    time24 operator+ (const time24& lhs, int min)
    time24 operator+ (int min, const time24& rhs)
    {
        return rhs + min;
    }  

    五.总结

        数据结构果然是必修课,喜欢。下了严蔚敏的数据结构视频,在学下。给个分享吧,http://pan.baidu.com/s/1pJ8V9er

  • 相关阅读:
    三大主流负载均衡软件对比(LVS+Nginx+HAproxy)
    nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    centos安装nginx并配置SSL证书
    hadoop创建目录文件失败
    The server time zone value 'EDT' is unrecognized or represents more than one time zone.
    脚本启动SpringBoot(jar)
    centos做免密登录
    数据库远程连接配置
    Bash 快捷键
    TCP三次握手四次断开
  • 原文地址:https://www.cnblogs.com/Alandre/p/3564875.html
Copyright © 2011-2022 走看看