泡茶看数据结构-临时(对象设计技术)

一.开场白

  第二次《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 minutesint currTime = hour * 60 + minute;int tTime = t.hour * 60 + t.minute;// if t is earlier than the current time, throw an exceptionif (tTime < currTime){cerr << "time24 duration(): argument is an earlier time";exit(1); }  else// create an anonymous object as the return valuereturn 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 minutesint currTime = hour * 60 + minute;int tTime = t.hour * 60 + t.minute;// if t is earlier than the current time, throw an exceptionif (tTime < currTime)throw rangeError("time24 duration(): argument is an earlier time");else// create an anonymous object as the return valuereturn 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 + rhsfriend time24 operator+ (const time24& lhs, int min);// form and return lhs + min// Precondition:  min must be >= 0friend 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

转载于:https://www.cnblogs.com/Alandre/p/3564875.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421188.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分享几个可供学习,休闲的网站

设计的无限可能 http://seeseed.com 包含各种文案&#xff0c;创意短片&#xff0c;各种模板 http://addog.com 微软office plus/ppt 模板之王 http://officeplus.cn 中国数字科技馆 http://cdstm.cn 在线一键抠图 http://remove.bg 全球杂志 http://magazinelib.com 图片…

leetcode 112路径总和

leetcode 112 其实leetcode上的题解和评论都很好的&#xff0c;找题解不用到csdn来&#xff0c;我是想把自己做过的题都记录下来才来写这个 class Solution { public:bool hasPathSum(TreeNode* root,int targetSum) {if(!root) return false;if(root->leftnullptr&&am…

1083. List Grades (25)

1083. List Grades (25) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and outp…

1011. World Cup Betting (20)

1011. World Cup Betting (20) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueWith the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing b…

.net 日期总结,用于业务时间查询

//今天是这周的第几天&#xff0c;是个英文星期&#xff0c;可以转换成intstring a DateTime.Now.DayOfWeek.ToString();//今天//MessageBox.Show(Convert.ToInt32(DateTime.Now.DayOfWeek) "");昨天MessageBox.Show(DateTime.Now.AddDays(-1).ToString());//星期…

1042. Shuffling Machine (20)

1042. Shuffling Machine (20) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueShuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid &qu…

leetcode 73 矩阵置零 C++ 两种解法

leetcode 73 两种解法~~&#xff0c;没有一个是我想出来的&#xff0c;哈哈~~ one class Solution { public:void setZeroes(vector<vector<int>>& matrix) {int mmatrix.size(),nmatrix[0].size();bool colfalse,rowfalse;for(int i0;i!m;i){if(!matrix[i][0…

JS第一课

<!DOCTYPE html> <html><head><meta charset"utf-8" /><title></title></head><body><script>///*1,它可以做什么。a, 动态改变页面的内容和页面外观b,验证表单数据&#xff0c;各大网站的注册验证功能可以通…

排序二叉树

排序二叉树 二叉树&#xff1a;作为基本数据结构的一种&#xff0c;是红黑树&#xff0c;B树等树形结构的基础。而排序二叉树是按照二叉树的结构来组织的。在本文中采用链表结构来创建二叉树。排序二叉树的    基本原理&#xff1a; 排序二叉树是将归并排序的基本思想构建二…

1020. Tree Traversals (25)

1020. Tree Traversals (25) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueSuppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to out…

leetcode 387 C++数组做法

leetcode 387 C数组做法 class Solution { public:int firstUniqChar(string s) {int ns.length();if(n0) return -1;int table[26]{0};for(int i0;i!n;i){table[s[i]-a];}for(int i0;i!n;i){if(table[s[i]-a]1)return i;}return -1;} };END

获取Class对象方式

在java中&#xff0c;每个class都有一个相应的Class对象&#xff0c;当编写好一个类&#xff0c;编译完成后&#xff0c;在生成的.class文件中&#xff0c;就产生一个Class对象&#xff0c;用来表示这个类的类型信息。获得Class实例的三种方式&#xff1a; 1). 利用对象调用get…

前端学习(1002):简洁版滑动下拉菜单问题解决

快速滑动 不停切换 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><scrip…

js bom and dom

以下的代码只是一些小的例子。我画了一张图来总结js大的结构 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><script>//Point 1 delayer and timer (BOM browser Object Model)var delayer;var tim…

leetcode 383 赎金信 C++

自己想的&#xff0c;一个思路两个解法&#xff0c;从字符串中的第一个唯一字符的思路搬过来的 one class Solution { public:bool canConstruct(string ransomNote, string magazine) {int table2[26]{0};for(int i0;i!magazine.length();i){table2[magazine[i]-a];}for(int …

Win32下 Qt与Lua交互使用(二):在Lua脚本中使用Qt类

话接上篇。成功配置好QtLuatoLua后&#xff0c;我们可以实现在Lua脚本中使用各个Qt的类。直接看代码吧。 #include "include/lua.hpp" #include <QWidget> #include <QApplication> #include <QFile> #include <QDebug>static int tolua_new…

1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30) 时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains o…

mysql列属性auto(mysql笔记四)

常见的的是一个字段不为null存在默认值 没值得时候才去找默认值&#xff0c;可以插入一个null到 可以为null的行里 主键&#xff1a;可以唯一标识某条记录的字段或者字段的集合 主键设置 主键不可为null,声明时自动设置为not null 字段上设置 字段名 primary key定义完字段后 …

详解html结构之间的各个关系,层级关系(以列表为例)

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>层级关系查找元素</title></head><body><div id "div">hello<ul id ""><li>li1</li><li>li2</…