]]>q算W重?友原函数(l构体、类和运符重蝲)http://www.shnenglu.com/sixleaves/archive/2014/08/29/208178.htmlswpswpFri, 29 Aug 2014 08:31:00 GMThttp://www.shnenglu.com/sixleaves/archive/2014/08/29/208178.htmlhttp://www.shnenglu.com/sixleaves/comments/208178.htmlhttp://www.shnenglu.com/sixleaves/archive/2014/08/29/208178.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/208178.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/208178.html最q在l习法、觉得有必要ȝ下C++q方面的知识QC++实是复杂,but i love it?/span>
W一Q运符重蝲的基本应用。第二:q算W重载于友原函数的关pR第三:l构体于cd于运符重蝲的区别。首先我需要普及下基本的知识: 预备知识Q?/span>operator(操作W?、operand(操作?、表辑ּ 表达式是由操作符和操作数构成的,而且一般每个表辑ּ都有副作用,q且都有l果Q关注结果和副作用)。什么意思?
关于副作用的理解Q其实就是在辑ֈl果的这个过E中所做的一些事情,而这些事情生了一些媄响,但是不媄响结果?/span>
好像有点l、看看例子在理解吧?/span>
看例子:
int a = 1,b=2,c; c = a + b;
在这D代码里面,c = a + b;是表达式、其?= ?+ 又称为操作符、c、a、b又称为操作数。其l果是计算Za+b的|副作用没有?/span>
又比如:
cout << "Helloe" << endl;其中 <<是操作符、cout?helloe"、endl是操作数?lt;<的结果是q回了ostream的引用,而副作用是?Helloe"输出到屏q?/span>
W一Q运符重蝲的基本应用?/strong> 看如下代码:
1 #include <iostream> 2 3usingnamespace std; 4 5class Point { 6private: 7double x,y; 8public: 9 Point(double x = 0, double y = 0):x(x),y(y) { 10 11 } 12 Point operator+(const Point &p) const{//q种是最基本的方式、其本质是做操作数调用operator+函数 13 Point temp; 14 temp.x = this->x + p.x; 15 temp.y = this->y + p.y; 16return temp; 17 } 18/* 19 ostream& operator<<(ostream& out) { 20 out << x << " " << y; 21 return out; 22 } 23*/ 24 friend ostream& operator<<(ostream& out, Point & p); 25 }; 26///* 27ostream& operator<<(ostream& out, Point & p) { 28out << p.x <<" "<< p.y; 29returnout; 30 } 31//*/ 32 33int main() { 34 35 Point a(1,5); 36 Point b(5,7); 37 Point c; 38 c = a + b; 39//c << cout << endl; 40 cout << c << endl; 41return 0; 42 }
]]>sixleaves之AC++杂谈录第2?<<循环和计?gt;> http://www.shnenglu.com/sixleaves/archive/2014/02/21/205872.htmlswpswpFri, 21 Feb 2014 08:21:00 GMThttp://www.shnenglu.com/sixleaves/archive/2014/02/21/205872.htmlhttp://www.shnenglu.com/sixleaves/comments/205872.htmlhttp://www.shnenglu.com/sixleaves/archive/2014/02/21/205872.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/205872.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/205872.htmlq章我写得有Ҏ切Q应Z前C++学过Q有些概念一跌过Q看不懂的,可以往下找U色字体处开始(从@环不变式分析处开始的分析Q再回头来看q个Q?br />
1 #include <iostream> 2 #include <string> 3 4int main() { 5//ask for the person's name 6 std::cout << "Please enter your first name: "; 7 8//read the name 9 std::string name; 10 std::cin >> name; 11 12//build the message that we intend to write 13const std:;string greeting = "Hello, " + name + "!"; 14 15//we have to rewrite this part 16 }
#
#分析Q我们现在需要重写(重构//we have...后面的代码)Q应该这h考,以前的那个程序不具备好的可扩展性,Z么呢Q首先如果要求输入的框架~程10行(I白#行变?0行)Q后面的代码久要多加很多行,一行行的进行输出。这时我们可以用循环对代码进行重构。我们先分析Q在greeting上下I白行只有一行,所以我们用pad
#表示I白行,而ȝ行数? * pad + 3Q头֊greeting那行Q。这h们就可以让程序输ZQ意多行。于是有如下代码
int r = 0; //setting r to 0 makes the invariant true
while(r != rows) { //we can assume that the invariant is true here //waiting a row of output makes the invariant false std::cout << std::endl; //incrementing r makes the invariant true again r++;
} //we can conclude that the invariant is true here
#首先你应该想一惌保不变式始lؓtrueQ只要确保在循环q入点ؓtrueQ一ơ@环结束点为trueQ那么这个不变式久永qؓtrueQunderstandQ如果还不理解,先吧我说 #的这句话理解了,在l往下看Q不然你不知道我在讲什么东? #我们的不变式是上述断言invariant:we have written r rows so far #我们分析q,不变式的两个断点Q一个设在开_一个在l尾Q所以开头时r = 0。此时程序一行也没输出,不变式ؓtrueQ在l尾处r++后,仍ؓtrueQؓ什么呢QD个例子,r = 0Q进来之后,输Z行,所以此时r不应该在?Q而应该ؓ1. #q是每一行输出的框架Q{换成for循环是上面相应的代码,而至于另外一个@环一样个道理?br />#下面再介l一个重要的概念Q这个概忉|之前q真没学好,看完后,恍然大悟Q大大悟阿Q那是循环时的计数问题?br /> #在C中C++中我们写循环l常是重int i = 0Q从0开始是不?q是,你是不是l常q样写for(int i = 0; i <= number; i++);但是更好的写法应该是for(int i = 0; i #!= number; i++);Z么呢Q请听我慢慢道来. #首先我们知道在不对称区间[0, rows)计数的话Q很明显是rows个数Q但是如果你使用的是对称区间Q[num,rows]则有rows - num + 1个数Q是不是很不明显Q再则从0开 #始一目了Ӟ别说你看不出来,我在举个例子Q?Q?6],和[21,86]哪一个你能快速判断出有几个数?br /> #有的人又_q算什么阿Q我?开始贝[1,66]Q不多一个数么,习惯好。我惌Q你说的没粗Q但我懒Q用不对U区间跟块算出,更不会出错。在则,用不对称区间的好 #处是Ҏ和invariantQ@环不变式Q相l合Q例如,如果你从1开始计敎ͼ有的人想我们把不变式Ҏ现在输出Wr行,但是q样是不能作Z个不变式的,所谓不变式Q就?br /> #q个断言永远正确Q但是当你结束@环时r = rows + 1Q就变成了输出第rows + 1行,但这个不变式变成错的鸟Qunderstand?br /> #再则我们选者!=而不?lt;=来作为比较操作符。这个差别很,但是很不一P前者,循环l束Ӟ只要没有在@环里breakQ,p判断此时r = rowsQ但是如果是后者,? #们这能证明至输Zrows行,为啥Q回忆下学过的mathQ?lt;=,是什么意思? #q有一条好处,我就不罗嗦了Q综上所属,你可以发C0开始计数的好处Q,惛_一Ӟ在写链表Ӟ是因ؓq个计数问题Q自׃整了个证明方法,哈哈Q每惛_早就有更 #单的方式了?br />#本h才疏学浅Q看不懂的,可以留言讨论之?img src ="http://www.shnenglu.com/sixleaves/aggbug/205872.html" width = "1" height = "1" />
]]>sixleaves之AC++杂谈录第1?<<使用字符?gt;>http://www.shnenglu.com/sixleaves/archive/2014/02/20/205856.htmlswpswpThu, 20 Feb 2014 12:38:00 GMThttp://www.shnenglu.com/sixleaves/archive/2014/02/20/205856.htmlhttp://www.shnenglu.com/sixleaves/comments/205856.htmlhttp://www.shnenglu.com/sixleaves/archive/2014/02/20/205856.html#Feedback0http://www.shnenglu.com/sixleaves/comments/commentRss/205856.htmlhttp://www.shnenglu.com/sixleaves/services/trackbacks/205856.html
代码如下
1// ask for person's name, and greet the person 2 3 #include <iostream> 4 #include <string> 5 6int main() { 7//ask for the person's name 8 std::cout << "Please enter your first name: "; 9 10//read the name 11 std::string name; 12 std::cin >> name; 13 14//write a greeting 15 std::cout << "Hello, " << name << "!" << std::endl; 16return 0; 17 }
#name是一个变量(它的cd是std::stringQ,而变量是一个有名字的对象(变量一定是对象Q但对象不一定ؓ变量Q因为对象可以没有名字,而且对象对应pȝ中的一块内存)?br />
#line 11Q是一个definitionQ即是一个定义,定义了一个名叫做name的std::stringcd的变量。而且出现在一个函数提中,所以是一个local variableQ当E序执行攑ֈ}Q就会销毁name变量Qƈ且释放name占用的内存,以让其他变量使用?br />
#line 12Q?gt;>从标准输入中d一个字W串Qƈ且保存它在name对象中。当通过标准库读取一个字W串Ӟ他会忽略输入中的所有空白符Q而吧其他字符d到name中,直到它遇到其他空白符或者文件结束标志。因此std::cin >> name;的结果是从标准输入中d一个单词?br />
#输入输出库会把它的输Z存在buffer的内部数据结构上Q通过~存可以优化输出操作。(因ؓ许多操作pȝ在向输出讑֤写入字符旉要花大量的时_
#有三个事件会促ɾpȝh~冲区?br />
#W一Q缓存区满了Q自动刷新?br />
#W二Q标准库被要求读取标准输入流。(即std::cin是std::istreamcdQ。如line 12.
#W三Q显C的要求h~冲。(std::endll束了输Qƈ且刷新缓冲区)
# 1//ask for a person's name, and generate a framed greeting
2 #include <iostream> 3 #include <string> 4 5int main() { 6 std::cout << "Please enter your first name: "; 7 std::string name; 8 std::cin >> name; 9//build the message that we intend to write 10const std::string greeting = "Hello, " + name + "!"; 11 12//build the second and fourth lines of the input 13const std::string spaces(greeting.size(), ' '); 14const std::string second = "* " + spaces + " *"; 15 16//build the first and fifth lines of the output 17const std::string first(second.size(), '*'); 18 19//write it all 20 std::cout << std::endl; 21 std::cout << first <<std::endl; 22 std::cout << second << std::endl; 23 std::cout << "* " << greeting << " *" << std::endl; 24 std::cout << second << std::endl; 25 std::cout << first << std::endl; 26 27return 0; 28 }