﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-投其所好,坚持不懈,快乐人生-随笔分类-C++</title><link>http://www.cppblog.com/Csystemthink/category/6397.html</link><description>生活的天平本不平衡，只有通过努力改变其偏向~</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 02:54:51 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 02:54:51 GMT</pubDate><ttl>60</ttl><item><title>字节对齐详解</title><link>http://www.cppblog.com/Csystemthink/archive/2008/04/10/46773.html</link><dc:creator>snail</dc:creator><author>snail</author><pubDate>Thu, 10 Apr 2008 12:47:00 GMT</pubDate><guid>http://www.cppblog.com/Csystemthink/archive/2008/04/10/46773.html</guid><wfw:comment>http://www.cppblog.com/Csystemthink/comments/46773.html</wfw:comment><comments>http://www.cppblog.com/Csystemthink/archive/2008/04/10/46773.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Csystemthink/comments/commentRss/46773.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Csystemthink/services/trackbacks/46773.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 字节对齐详解&nbsp;&nbsp;<a href='http://www.cppblog.com/Csystemthink/archive/2008/04/10/46773.html'>阅读全文</a><img src ="http://www.cppblog.com/Csystemthink/aggbug/46773.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Csystemthink/" target="_blank">snail</a> 2008-04-10 20:47 <a href="http://www.cppblog.com/Csystemthink/archive/2008/04/10/46773.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++_Interview_Questions（1）</title><link>http://www.cppblog.com/Csystemthink/archive/2008/04/08/46529.html</link><dc:creator>snail</dc:creator><author>snail</author><pubDate>Tue, 08 Apr 2008 09:27:00 GMT</pubDate><guid>http://www.cppblog.com/Csystemthink/archive/2008/04/08/46529.html</guid><wfw:comment>http://www.cppblog.com/Csystemthink/comments/46529.html</wfw:comment><comments>http://www.cppblog.com/Csystemthink/archive/2008/04/08/46529.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/Csystemthink/comments/commentRss/46529.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Csystemthink/services/trackbacks/46529.html</trackback:ping><description><![CDATA[<div>
<p><font size=2>1.是不是一个父类写了一个virtual 函数，如果子类覆盖它的函数不加virtual ,也能实现多态?</font></p>
<p><font size=2>virtual修饰符会被隐形继承的。</font><font size=2>private 也被集成，只事派生类没有访问权限而已。</font><font size=2>virtual可加可不加。</font><font size=2>子类的空间里有父类的所有变量(static除外)。</font><font size=2>同一个函数只存在一个实体(inline除外)。</font><font size=2>子类覆盖它的函数不加virtual ,也能实现多态。</font><font size=2>在子类的空间里，有父类的私有变量。私有变量不能直接访问。</font></p>
<p><br><font size=2>--------------------------------------------------------------------------<br>2.输入一个字符串，将其逆序后输出。（使用C++，不建议用伪码）</font></p>
<p><font size=2>#include &lt;iostream&gt;<br>using namespace std;</font></p>
<p><br><font size=2>void main()<br>{<br>&nbsp; char a[50];memset(a,0,sizeof(a));<br>&nbsp; int i=0,j;<br>&nbsp; char t;<br>&nbsp; cin.getline(a,50,'\n');<br>&nbsp; for(i=0,j=strlen(a)-1;i&lt;strlen(a)/2;i++,j--)<br>&nbsp; {<br>&nbsp;&nbsp; t=a[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a[i]=a[j];<br>&nbsp;&nbsp; a[j]=t;<br>&nbsp; }<br>&nbsp; cout&lt;&lt;a&lt;&lt;endl;&nbsp; <br>}</font></p>
<p><font size=2>方法还有很多。</font><br><font size=2>--------------------------------------------------------------------------<br>3.请简单描述Windows内存管理的方法。</font></p>
<p><font size=2>内存管理是操作系统中的重要部分，两三句话恐怕谁也说不清楚吧～～<br>我先说个大概，希望能够抛砖引玉吧</font></p>
<p><font size=2>当程序运行时需要从内存中读出这段程序的代码。代码的位置必须在物理内存中才能被运行，由于现在的操作系统中有非常多的程序运行着，内存中不能够完全放下，所以引出了虚拟内存的概念。把哪些不常用的程序片断就放入虚拟内存，当需要用到它的时候在load入主存（物理内存）中。这个就是内存管理所要做的事。内存管理还有另外一件事需要做：计算程序片段在主存中的物理位置，以便CPU调度。</font></p>
<p><font size=2>内存管理有块式管理，页式管理，段式和段页式管理。现在常用段页式管理</font></p>
<p><font size=2>块式管理：把主存分为一大块、一大块的，当所需的程序片断不在主存时就分配一块主存空间，把程 序片断load入主存，就算所需的程序片度只有几个字节也只能把这一块分配给它。这样会造成很大的浪费，平均浪费了50％的内存空间，但时易于管理。</font></p>
<p><font size=2>页式管理：把主存分为一页一页的，每一页的空间要比一块一块的空间小很多，显然这种方法的空间利用率要比块式管理高很多。</font></p>
<p><font size=2>段式管理：把主存分为一段一段的，每一段的空间又要比一页一页的空间小很多，这种方法在空间利用率上又比页式管理高很多，但是也有另外一个缺点。一个程序片断可能会被分为几十段，这样很多时间就会被浪费在计算每一段的物理地址上（计算机最耗时间的大家都知道是I/O吧）。</font></p>
<p><font size=2>段页式管理：结合了段式管理和页式管理的优点。把主存分为若干页，每一页又分为若干段。好处就很明显，不用我多说了吧。</font></p>
<p><font size=2>各种内存管理都有它自己的方法来计算出程序片断在主存中的物理地址，其实都很相似。</font></p>
<p><font size=2>这只是一个大概而已，不足以说明内存管理的皮毛。无论哪一本操作系统书上都有详细的讲解</font></p>
<p><br><font size=2>--------------------------------------------------------------------------<br>4.<br>#include "stdafx.h"<br>#define SQR(X) X*X</font></p>
<p><font size=2>int main(int argc, char* argv[])<br>{<br>&nbsp;int a = 10;<br>&nbsp;int k = 2;<br>&nbsp;int m = 1;</font></p>
<p><font size=2>&nbsp;a /= SQR(k+m)/SQR(k+m); <br>&nbsp;printf("%d\n",a); </font></p>
<p><font size=2>&nbsp;return 0;<br>}<br>这道题目的结果是什么啊?</font></p>
<p><font size=2>define 只是定义而已，在编择时只是简单代换X*X而已，并不经过算术法则的</font></p>
<p><font size=2>a /= (k+m)*(k+m)/(k+m)*(k+m);<br>=&gt;a /= (k+m)*1*(k+m);<br>=&gt;a = a/9;<br>=&gt;a = 1;</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>5.<br>const 符号常量；<br>(1)const char *p<br>(2)char const *p<br>(3)char * const p<br>说明上面三种描述的区别；</font></p>
<p><br><font size=2>如果const位于星号的左侧，则const就是用来修饰指针所指向的变量，即指针指向为常量；<br>如果const位于星号的右侧，const就是修饰指针本身，即指针本身是常量。</font></p>
<p><font size=2>(1)const char *p</font></p>
<p><font size=2>一个指向char类型的const对象指针，p不是常量,我们可以修改p的值，使其指向不同的char，但是不能改变它指向非char对象，如：<br>const char *p;<br>char c1='a';<br>char c2='b';<br>p=&amp;c1;//ok<br>p=&amp;c2;//ok<br>*p=c1;//error</font></p>
<p><font size=2>(2)char const *p<br></font></p>
<font size=2>
<p><font size=2>1)2)是一样的，此时*p可以修改，而p不能修改。p为const pointer.即指向const 对象的指针，对象只读。</font><br>(3)char * const p<br></p>
<p>常量指针，指针本身所指向的地址不可改变。</p>
</font>
<p><font size=2>(4)const char * const p<br>这种是地址及指向对象都不能修改。</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>6.下面是C语言中两种if语句判断方式。请问哪种写法更好？为什么？<br>&nbsp;int n;<br>&nbsp;if (n == 10) // 第一种判断方式<br>&nbsp;if (10 == n) // 第二种判断方式</font></p>
<p><font size=2>如果少了个=号,编译时就会报错,减少了出错的可能行,可以检测出是否少了=</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>7.下面的代码有什么问题？<br>void DoSomeThing(...)<br>{<br>&nbsp;char* p;<br>&nbsp;...<br>&nbsp;p = malloc(1024);&nbsp; // 分配1K的空间<br>&nbsp;if (NULL == p)<br>&nbsp; return;<br>&nbsp;...<br>&nbsp;p = realloc(p, 2048); // 空间不够，重新分配到2K<br>&nbsp;if (NULL == p)<br>&nbsp; return;<br>&nbsp;...<br>}</font></p>
<p><font size=2>A:<br>p = malloc(1024);&nbsp;&nbsp;&nbsp;&nbsp; 应该写成： p = (char *) malloc(1024); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 没有释放p的空间，造成内存泄漏。</font></p>
<p><br><font size=2>--------------------------------------------------------------------------<br>8.下面的代码有什么问题？并请给出正确的写法。<br>void DoSomeThing(char* p)<br>{<br>&nbsp;char str[16];<br>&nbsp;int n;<br>&nbsp;assert(NULL != p);<br>&nbsp;sscanf(p, "%s%d", str, n);<br>&nbsp;if (0 == strcmp(str, "something"))<br>&nbsp;{<br>&nbsp; ...<br>&nbsp;}<br>}</font></p>
<p><font size=2>A:<br>sscanf(p, "%s%d", str, n);&nbsp;&nbsp; 这句该写成： sscanf(p, "%s%d", str, &amp;n);</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>9.下面代码有什么错误?<br>Void test1() <br>{ <br>&nbsp;char string[10]; <br>&nbsp;char *str1="0123456789";<br>&nbsp;strcpy(string, str1); <br>} </font></p>
<p><font size=2>数组越界</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>10.下面代码有什么问题?<br>Void test2() <br>{ <br>&nbsp; char string[10], str1[10]; <br>&nbsp; for(i=0; i&lt;10;i++) <br>&nbsp; { <br>&nbsp;&nbsp;&nbsp;&nbsp; str1[i] ='a'; <br>&nbsp; } <br>&nbsp; strcpy(string, str1); <br>} </font></p>
<p><font size=2>数组越界</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>11.下面代码有什么问题?<br>Void test3(char* str1) <br>{ <br>&nbsp; char string[10]; <br>&nbsp; if(strlen(str1)&lt;=10) <br>&nbsp; { <br>&nbsp;&nbsp;&nbsp; strcpy(string, str1); <br>&nbsp; } <br>} </font></p>
<p><font size=2>==数组越界<br>==strcpy拷贝的结束标志是查找字符串中的\0 因此如果字符串中没有遇到\0的话 会一直复制，直到遇到\0,上面的123都因此产生越界的情况<br>&nbsp;<br>建议使用 strncpy 和 memcpy</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>12.下面代码有什么问题?</font></p>
<p><font size=2>#define MAX_SRM 256 </font></p>
<p><font size=2>DSN get_SRM_no() <br>{ <br>&nbsp; static int SRM_no; //是不是这里没赋初值？<br>&nbsp; int I; <br>&nbsp; for(I=0;I&lt;MAX_SRM;I++,SRM_no++) <br>&nbsp; { <br>&nbsp;&nbsp;&nbsp; SRM_no %= MAX_SRM; <br>&nbsp;&nbsp;&nbsp; if(MY_SRM.state==IDLE) <br>&nbsp;&nbsp;&nbsp; { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <br>&nbsp;&nbsp;&nbsp; } <br>&nbsp; } <br>&nbsp; if(I&gt;=MAX_SRM) <br>&nbsp;&nbsp;&nbsp; return (NULL_SRM); <br>&nbsp; else <br>&nbsp;&nbsp;&nbsp; return SRM_no; <br>} </font></p>
<p><font size=2>系统会初始化static int变量为0,但该值会一直保存,所谓的不可重入...</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>13.写出运行结果:<br>{// test1<br>&nbsp;&nbsp;&nbsp; char str[] = "world"; cout &lt;&lt; sizeof(str) &lt;&lt; ": ";<br>&nbsp;&nbsp;&nbsp; char *p&nbsp;&nbsp;&nbsp; = str;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; sizeof(p) &lt;&lt; ": ";<br>&nbsp;&nbsp;&nbsp; char i&nbsp;&nbsp;&nbsp;&nbsp; = 10;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; sizeof(i) &lt;&lt; ": ";<br>&nbsp;&nbsp;&nbsp; void *pp&nbsp;&nbsp; = malloc(10);&nbsp; cout &lt;&lt; sizeof(p) &lt;&lt; endl;<br>}</font></p>
<p><font size=2>6：4：1：4</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>14.写出运行结果:<br>{// test2<br>&nbsp;&nbsp;&nbsp; union V {<br>&nbsp;struct X {<br>&nbsp;&nbsp;unsigned char s1:2;<br>&nbsp;&nbsp;unsigned char s2:3;<br>&nbsp;&nbsp;unsigned char s3:3;<br>&nbsp;} x;</font></p>
<p><font size=2>&nbsp;unsigned char c;<br>&nbsp;&nbsp;&nbsp; } v;</font></p>
<p><font size=2>&nbsp;&nbsp;&nbsp; v.c = 100;<br>&nbsp;&nbsp;&nbsp; printf("%d", v.x.s3); </font></p>
<p><font size=2>}</font></p>
<p><font size=2>3</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>15.用C++写个程序，如何判断一个操作系统是16位还是32位的？不能用sizeof()函数</font></p>
<p><font size=2>A1:<br>16位的系统下，<br>int i = 65536;<br>cout &lt;&lt; i; // 输出0；<br>int i = 65535;<br>cout &lt;&lt; i; // 输出-1；</font></p>
<p><font size=2>32位的系统下，<br>int i = 65536;<br>cout &lt;&lt; i; // 输出65536；<br>int i = 65535;<br>cout &lt;&lt; i; // 输出65535；</font></p>
<p><font size=2>A2:</font></p>
<p><font size=2>int a = ~0;<br>if( a&gt;65536 ) <br>{<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"32 bit"&lt;&lt;endl;<br>}<br>else<br>{<br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"16 bit"&lt;&lt;endl;<br>}</font></p>
<p><br><font size=2>--------------------------------------------------------------------------<br>16.C和C++有什么不同?</font></p>
<p><font size=2>从机制上：c是面向过程的（但c也可以编写面向对象的程序）；c++是面向对象的，提供了类。但是，<br>c++编写面向对象的程序比c容易</font></p>
<p><font size=2>从适用的方向：c适合要求代码体积小的，效率高的场合，如嵌入式；c++适合更上层的，复杂的；&nbsp; llinux核心大部分是c写的，因为它是系统软件，效率要求极高。</font></p>
<p><font size=2>从名称上也可以看出，c++比c多了+，说明c++是c的超集；那为什么不叫c+而叫c++呢，是因为c++比<br>c来说扩充的东西太多了，所以就在c后面放上两个+；于是就成了c++</font></p>
<p><font size=2>C语言是结构化编程语言，C++是面向对象编程语言。<br>C++侧重于对象而不是过程，侧重于类的设计而不是逻辑的设计。</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>17.在不用第三方参数的情况下，交换两个参数的值<br>#include &lt;stdio.h&gt;</font></p>
<p><font size=2>void main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int i=60;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int j=50;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i=i+j;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j=i-j;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i=i-j;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("i=%d\n",i);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("j=%d\n",j);<br>}</font></p>
<p><font size=2>方法二：<br>i^=j;<br>j^=i;<br>i^=j;</font></p>
<p><font size=2>方法三：<br>// 用加减实现，而且不会溢出<br>a = a+b-(b=a)</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>18.有关位域的面试题（为什么输出的是一个奇怪的字符）</font></p>
<p><font size=2>a.t = 'b';效果相当于 a.t= 'b' &amp; 0xf;</font></p>
<p><font size=2>'b' --&gt; 01100010<br>'b' &amp; 0xf --&gt;&gt;00000010<br>所以输出Ascii码为2的特殊字符</font></p>
<p><br><font size=2>char t:4;就是4bit的字符变量，同样<br>unsigned short i:8;就是8bit的无符号短整形变量</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>19.int i=10, j=10, k=3; k*=i+j; k最后的值是?</font></p>
<p><font size=2>60</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>20.进程间通信的方式有?</font></p>
<p><font size=2>进程间通信的方式有 共享内存， 管道 ，Socket ，消息队列 , DDE等</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>21.<br>struct A <br>{ <br>char t:4; <br>char k:4; <br>unsigned short i:8; <br>unsigned long m; <br>} <br>sizeof(A)=?（不考虑边界对齐）</font></p>
<p><font size=2>7</font></p>
<p><font size=2>struct CELL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Declare CELL bit field<br>{<br>&nbsp;&nbsp; unsigned character&nbsp; : 8;&nbsp; // 00000000 ????????<br>&nbsp;&nbsp; unsigned foreground : 3;&nbsp; // 00000??? 00000000<br>&nbsp;&nbsp; unsigned intensity&nbsp; : 1;&nbsp; // 0000?000 00000000<br>&nbsp;&nbsp; unsigned background : 3;&nbsp; // 0???0000 00000000<br>&nbsp;&nbsp; unsigned blink&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1;&nbsp; // ?0000000 00000000<br>} screen[25][80];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Array of bit fields<br>二、位结构 <br>&nbsp;&nbsp;&nbsp; 位结构是一种特殊的结构, 在需按位访问一个字节或字的多个位时, 位结构 <br>比按位运算符更加方便。 <br>&nbsp;&nbsp;&nbsp; 位结构定义的一般形式为: <br>&nbsp;&nbsp;&nbsp;&nbsp; struct位结构名{ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 数据类型 变量名: 整型常数; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 数据类型 变量名: 整型常数; <br>&nbsp;&nbsp;&nbsp;&nbsp; } 位结构变量; <br>&nbsp;&nbsp;&nbsp; 其中: 数据类型必须是int(unsigned或signed)。 整型常数必须是非负的整 <br>数, 范围是0~15, 表示二进制位的个数, 即表示有多少位。 <br>&nbsp;&nbsp;&nbsp; 变量名是选择项, 可以不命名, 这样规定是为了排列需要。 <br>&nbsp;&nbsp;&nbsp; 例如: 下面定义了一个位结构。 <br>&nbsp;&nbsp;&nbsp;&nbsp; struct{ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned incon: 8;&nbsp; /*incon占用低字节的0~7共8位*/ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned txcolor: 4;/*txcolor占用高字节的0~3位共4位*/ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned bgcolor: 3;/*bgcolor占用高字节的4~6位共3位*/ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned blink: 1;&nbsp; /*blink占用高字节的第7位*/ <br>&nbsp;&nbsp;&nbsp;&nbsp; }ch; <br>&nbsp;&nbsp;&nbsp; 位结构成员的访问与结构成员的访问相同。 <br>&nbsp;&nbsp;&nbsp; 例如: 访问上例位结构中的bgcolor成员可写成: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch.bgcolor <br>&nbsp; <br>&nbsp;&nbsp;&nbsp; 注意: <br>&nbsp;&nbsp;&nbsp; 1. 位结构中的成员可以定义为unsigned, 也可定义为signed,&nbsp; 但当成员长 <br>度为1时, 会被认为是unsigned类型。因为单个位不可能具有符号。 <br>&nbsp;&nbsp;&nbsp; 2. 位结构中的成员不能使用数组和指针, 但位结构变量可以是数组和指针, <br>如果是指针, 其成员访问方式同结构指针。 <br>&nbsp;&nbsp;&nbsp; 3. 位结构总长度(位数), 是各个位成员定义的位数之和,&nbsp; 可以超过两个字 <br>节。 <br>&nbsp;&nbsp;&nbsp; 4. 位结构成员可以与其它结构成员一起使用。 <br>&nbsp;&nbsp;&nbsp; 例如: <br>&nbsp;&nbsp;&nbsp;&nbsp; struct info{ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char name[8]; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int age; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct addr address; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float pay; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned state: 1; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned pay: 1; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }workers;<br>&nbsp;&nbsp;&nbsp; 上例的结构定义了关于一个工人的信息。其中有两个位结构成员, 每个位结 <br>构成员只有一位, 因此只占一个字节但保存了两个信息, 该字节中第一位表示工 <br>人的状态, 第二位表示工资是否已发放。由此可见使用位结构可以节省存贮空间。</font></p>
<p><br><font size=2>--------------------------------------------------------------------------<br>22.下面的函数实现在一个固定的数上加上一个数，有什么错误，改正 <br>int add_n(int n) <br>{ <br>&nbsp; static int i=100; <br>&nbsp; i+=n; <br>&nbsp; return i; <br>} </font></p>
<p><font size=2>答:<br>因为static使得i的值会保留上次的值。<br>去掉static就可了</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>23.下面的代码有什么问题?<br>class A <br>{ <br>public: <br>&nbsp; A() { p=this; } <br>&nbsp; ~A() { if(p!=NULL) { delete p; p=NULL; } } </font></p>
<p><font size=2>&nbsp; A* p; <br>};</font></p>
<p><font size=2>答:<br>会引起无限递归</font></p>
<p><font size=2>--------------------------------------------------------------------------<br>24.<br>union a {<br>&nbsp;int a_int1;<br>&nbsp;double a_double;<br>&nbsp;int a_int2;<br>};</font></p>
<p><font size=2>typedef struct<br>{<br>&nbsp;a a1;<br>&nbsp;char y;<br>} b;</font></p>
<p><font size=2>class c<br>{<br>&nbsp;double c_double;<br>&nbsp;b b1;<br>&nbsp;a a2;</font></p>
<p><font size=2>};</font></p>
<p><font size=2>输出cout&lt;&lt;sizeof(c)&lt;&lt;endl;的结果?</font></p>
<p><font size=2>答:<br>VC6环境下得出的结果是32</font></p>
<p><font size=2>另:<br>我(sun)在VC6.0+win2k下做过试验:<br>short - 2<br>int-4<br>float-4<br>double-8<br>指针-4</font></p>
<p><font size=2>sizeof(union)，以结构里面size最大的为union的size</font></p>
<p><font size=2>&nbsp;----------------------------------------------------------------------------------</font></p>
<p><font size=2>25.i最后等于多少?<br>int i = 1;<br>int j = i++;<br>if((i&gt;j++) &amp;&amp; (i++ == j)) i+=j;</font></p>
<p>答:<br>i = 5</p>
<p>--------------------------------------------------------------------------<br>26.<br>unsigned short array[]={1,2,3,4,5,6,7};<br>int i = 3;<br>*(array + i) = ?</p>
<p>答:<br>4</p>
<p>--------------------------------------------------------------------------<br>27.<br>class A<br>{<br>&nbsp; virtual void func1()；<br>&nbsp; void func2();<br>}<br>Class B: class A<br>{<br>&nbsp; void func1(){cout &lt;&lt; "fun1 in class B" &lt;&lt; endl;}<br>&nbsp; virtual void func2(){cout &lt;&lt; "fun2 in class B" &lt;&lt; endl;}<br>} <br>A, A中的func1和B中的func2都是虚函数.<br>B, A中的func1和B中的func2都不是虚函数.<br>C, A中的func2是虚函数.，B中的func1不是虚函数.<br>D, A中的func2不是虚函数，B中的func1是虚函数.</p>
<p>答:<br>A</p>
<p>--------------------------------------------------------------------------<br>28.<br>数据库：抽出部门，平均工资，要求按部门的字符串顺序排序，不能含有"human resource"部门， </p>
<p>employee结构如下：employee_id, employee_name, depart_id,depart_name,wage</p>
<p>答:<br>select depart_name, avg(wage)<br>from employee<br>where depart_name &lt;&gt; 'human resource'<br>group by depart_name<br>order by depart_name</p>
<p>--------------------------------------------------------------------------<br>29.<br>给定如下SQL数据库：Test(num INT(4)) 请用一条SQL语句返回num的最小值，但不许使用统计功能，如MIN，MAX等</p>
<p>答:<br>select top 1 num<br>from Test<br>order by num desc</p>
<p>--------------------------------------------------------------------------<br>30.<br>输出下面程序结果。 </p>
<p>#include &lt;iostream.h&gt; </p>
<p>class A <br>{ <br>public:<br>&nbsp;virtual void print(void) <br>&nbsp;{ <br>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"A::print()"&lt;&lt;endl; <br>&nbsp;} <br>};<br>class B:public A <br>{ <br>public:<br>&nbsp;virtual void print(void) <br>&nbsp;{ <br>&nbsp;&nbsp; cout&lt;&lt;"B::print()"&lt;&lt;endl;<br>&nbsp;}; <br>}; <br>class C:public B<br>{<br>public:<br>&nbsp;virtual void print(void)<br>&nbsp;{<br>&nbsp;&nbsp;cout&lt;&lt;"C::print()"&lt;&lt;endl;<br>&nbsp;}<br>};<br>void print(A a) <br>{ <br>&nbsp;&nbsp; a.print(); <br>} <br>void main(void) <br>{ <br>&nbsp;&nbsp; A a, *pa,*pb,*pc; <br>&nbsp;&nbsp; B b; <br>&nbsp;&nbsp; C c; <br>&nbsp;&nbsp; <br>&nbsp;&nbsp; pa=&amp;a; <br>&nbsp;&nbsp; pb=&amp;b; <br>&nbsp;&nbsp; pc=&amp;c; <br>&nbsp;&nbsp; <br>&nbsp;&nbsp; a.print(); <br>&nbsp;&nbsp; b.print(); <br>&nbsp;&nbsp; c.print(); <br>&nbsp;&nbsp; <br>&nbsp;&nbsp; pa-&gt;print(); <br>&nbsp;&nbsp; pb-&gt;print(); <br>&nbsp;&nbsp; pc-&gt;print(); <br>&nbsp;&nbsp; <br>&nbsp;&nbsp; print(a); <br>&nbsp;&nbsp; print(b); <br>&nbsp;&nbsp; print(c); <br>}</p>
<p>A:<br>A::print()<br>B::print()<br>C::print()<br>A::print()<br>B::print()<br>C::print()<br>A::print()<br>A::print()<br>A::print()</p>
<p>--------------------------------------------------------------------------<br>31.<br>试编写函数判断计算机的字节存储顺序是开序(little endian)还是降序(bigendian)</p>
<p>答:<br>bool IsBigendian()<br>{<br>&nbsp;unsigned short usData = 0x1122;<br>&nbsp;unsigned char&nbsp; *pucData = (unsigned char*)&amp;usData;</p>
<p>&nbsp;return (*pucData == 0x22);<br>}</p>
<p>--------------------------------------------------------------------------<br>32.简述Critical Section和Mutex的不同点</p>
<p>答:<br>对几种同步对象的总结<br>1.Critical Section<br>A.速度快<br>B.不能用于不同进程<br>C.不能进行资源统计(每次只可以有一个线程对共享资源进行存取)</p>
<p>2.Mutex<br>A.速度慢<br>B.可用于不同进程<br>C.不能进行资源统计</p>
<p>3.Semaphore<br>A.速度慢<br>B.可用于不同进程<br>C.可进行资源统计(可以让一个或超过一个线程对共享资源进行存取)</p>
<p>4.Event<br>A.速度慢<br>B.可用于不同进程<br>C.可进行资源统计</p>
<p>&nbsp;</p>
<p>--------------------------------------------------------------------------<br>33.一个数据库中有两个表:<br>一张表为Customer，含字段ID,Name;<br>一张表为Order，含字段ID,CustomerID（连向Customer中ID的外键）,Revenue；<br>写出求每个Customer的Revenue总和的SQL语句。</p>
<p>建表<br>create table customer<br>(<br>ID int primary key,Name char(10)<br>)</p>
<p>go</p>
<p>create table [order]<br>(<br>ID int primary key,CustomerID&nbsp; int foreign key references customer(id) , Revenue float<br>)</p>
<p>go</p>
<p>--查询<br>select Customer.ID, sum( isnull([Order].Revenue,0) )<br>from customer full join [order] <br>on( [order].customerid=customer.id )<br>group by customer.id</p>
<p>--------------------------------------------------------------------------<br>34.请指出下列程序中的错误并且修改<br>void GetMemory(char *p){<br>&nbsp; p=(char *)malloc(100);<br>}<br>void Test(void){<br>&nbsp; char *str=NULL;<br>&nbsp; GetMemory=(str);<br>&nbsp; strcpy(str,"hello world");<br>&nbsp; printf(str);<br>}</p>
<p>A:错误--参数的值改变后，不会传回<br>GetMemory并不能传递动态内存，Test函数中的 str一直都是 NULL。<br>strcpy(str, "hello world");将使程序崩溃。</p>
<p>修改如下:<br>char *GetMemory(){<br>&nbsp; char *p=(char *)malloc(100);<br>&nbsp; return p;<br>}<br>void Test(void){<br>&nbsp; char *str=NULL;<br>&nbsp; str=GetMemory(){<br>&nbsp; strcpy(str,"hello world");<br>&nbsp; printf(str);<br>}</p>
<p>方法二:void GetMemory2(char **p)变为二级指针.<br>void GetMemory2(char **p, int num)<br>{<br>&nbsp;*p = (char *)malloc(sizeof(char) * num);<br>}</p>
<p>--------------------------------------------------------------------------<br>35.程序改错<br>class mml<br>{<br>&nbsp; private:<br>&nbsp;&nbsp;&nbsp; static unsigned int x;<br>&nbsp; public:<br>&nbsp;&nbsp;&nbsp; mml(){ x++; }<br>&nbsp;&nbsp;&nbsp; mml(static unsigned int &amp;) {x++;}<br>&nbsp;&nbsp;&nbsp; ~mml{x--;}<br>&nbsp; pulic:<br>&nbsp;&nbsp;&nbsp; virtual mon() {} = 0;<br>&nbsp;&nbsp;&nbsp; static unsigned int mmc(){return x;}<br>&nbsp;&nbsp;&nbsp; ......&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;<br>};<br>class nnl:public mml<br>{<br>&nbsp; private:<br>&nbsp;&nbsp;&nbsp; static unsigned int y;<br>&nbsp; public:<br>&nbsp;&nbsp;&nbsp; nnl(){ x++; }<br>&nbsp;&nbsp;&nbsp; nnl(static unsigned int &amp;) {x++;}<br>&nbsp;&nbsp;&nbsp; ~nnl{x--;}<br>&nbsp; public:<br>&nbsp;&nbsp;&nbsp; virtual mon() {};<br>&nbsp;&nbsp;&nbsp;&nbsp; static unsigned int nnc(){return y;}<br>&nbsp;&nbsp;&nbsp; ......&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>};</p>
<p>代码片断:<br>mml* pp = new nnl;<br>..........<br>delete pp;</p>
<p><br>A:<br>基类的析构函数应该为虚函数<br>virtual ~mml{x--;}</p>
<p>--------------------------------------------------------------------------<br>36.101个硬币100真、1假，真假区别在于重量。请用无砝码天平称两次给出真币重还是假币重的结论。</p>
<p>答:<br>101个先取出2堆,<br>33,33<br>第一次称,如果不相等,说明有一堆重或轻<br>那么把重的那堆拿下来,再放另外35个中的33<br>如果相等,说明假的重,如果不相等,新放上去的还是重的话,说明假的轻(不可能新放上去的轻)</p>
<p>第一次称,如果相等的话，这66个肯定都是真的,从这66个中取出35个来,与剩下的没称过的35个比<br>下面就不用说了</p>
<p>方法二:<br>第3题也可以拿A(50),B(50)比一下，一样的话拿剩下的一个和真的比一下。<br>如果不一样，就拿其中的一堆。比如A(50)再分成两堆25比一下，一样的话就在<br>B(50)中，不一样就在A(50)中，结合第一次的结果就知道了。</p>
<p>--------------------------------------------------------------------------<br>37.static变量和static 函数各有什么特点？</p>
<p>答:<br>static变量：在程序运行期内一直有效，如果定义在函数外，则在编译单元内可见，如果在函数内，在在定义的block内可见；<br>static函数：在编译单元内可见；</p>
<p>--------------------------------------------------------------------------<br>38.用C 写一个输入的整数,倒着输出整数的函数,要求用递归方法 ;</p>
<p>答:<br>void fun( int a )<br>{<br>&nbsp;printf( "%d", a%10 );<br>&nbsp;a /= 10;<br>&nbsp;if( a &lt;=0 )return;</p>
<p>&nbsp;fun( a );<br>}</p>
<p>--------------------------------------------------------------------------<br>39.写出程序结果:<br>void Func(char str[100])<br>{<br>&nbsp; printf("%d\n", sizeof(str));<br>}</p>
<p>答:<br>4<br>分析:<br>指针长度</p>
<p>--------------------------------------------------------------------------<br>40.int id[sizeof(unsigned long)];<br>&nbsp;&nbsp;&nbsp; 这个对吗？为什么??</p>
<p>答:对<br>这个 sizeof是编译时运算符，编译时就确定了<br>可以看成和机器有关的常量。</p>
</div>
<br>
<img src ="http://www.cppblog.com/Csystemthink/aggbug/46529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Csystemthink/" target="_blank">snail</a> 2008-04-08 17:27 <a href="http://www.cppblog.com/Csystemthink/archive/2008/04/08/46529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>堆和栈的区别</title><link>http://www.cppblog.com/Csystemthink/archive/2008/04/08/46512.html</link><dc:creator>snail</dc:creator><author>snail</author><pubDate>Tue, 08 Apr 2008 07:15:00 GMT</pubDate><guid>http://www.cppblog.com/Csystemthink/archive/2008/04/08/46512.html</guid><wfw:comment>http://www.cppblog.com/Csystemthink/comments/46512.html</wfw:comment><comments>http://www.cppblog.com/Csystemthink/archive/2008/04/08/46512.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Csystemthink/comments/commentRss/46512.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Csystemthink/services/trackbacks/46512.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 堆和栈的区别&nbsp;&nbsp;<a href='http://www.cppblog.com/Csystemthink/archive/2008/04/08/46512.html'>阅读全文</a><img src ="http://www.cppblog.com/Csystemthink/aggbug/46512.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Csystemthink/" target="_blank">snail</a> 2008-04-08 15:15 <a href="http://www.cppblog.com/Csystemthink/archive/2008/04/08/46512.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)Polymorphism(多态性)很黄很强大，大家注意少儿不宜</title><link>http://www.cppblog.com/Csystemthink/archive/2008/03/17/44680.html</link><dc:creator>snail</dc:creator><author>snail</author><pubDate>Mon, 17 Mar 2008 03:21:00 GMT</pubDate><guid>http://www.cppblog.com/Csystemthink/archive/2008/03/17/44680.html</guid><wfw:comment>http://www.cppblog.com/Csystemthink/comments/44680.html</wfw:comment><comments>http://www.cppblog.com/Csystemthink/archive/2008/03/17/44680.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/Csystemthink/comments/commentRss/44680.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Csystemthink/services/trackbacks/44680.html</trackback:ping><description><![CDATA[<p><font face=Verdana size=2>Polymorphism shouldn't be a new concept to anybody. You deal with it every day in the real world. There's more than one class of cat to skin, but you skin 'em the same way, even if the specific instance is completely new to you. Let's say for example you want to fuck a hole. You fuck all holes the same. You don't care if that hole happens to be a mouth, an ass, or a pussy, you're gonna fuck it the same way regardless. However, the mouth, pussy, or ass may respond differently to the fucking. </font></p>
<p>So you have a common abstract class named 'Hole' and 3 concrete classes Pussy, Ass, and Mouth which all extend from Hole: </p>
<p><code><br>class Pussy extends Hole {}<br>class Mouth extends Hole {}<br>class Ass extends Hole {}<br></code></p>
<p>So, now let's say you have a Penis.Fuck(Hole h) method. The Penis class is unconcerned about what the specific Hole instance is, it's gonna fuck it the same regardless. Specificly we thrust the Hole with a Penis until the Penis is spent. Finally, we give the hole the Penis' load. </p>
<p><code><br>class Penis {<br>&nbsp; public Fuck(Hole h) {<br>&nbsp; &nbsp; while(!this.isSpent) {<br>&nbsp; &nbsp; &nbsp; h.TakeAThrust(this);<br>&nbsp; &nbsp; &nbsp; this.arousal++;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; h.TakeALoad(this.load);<br>&nbsp; }<br>}<br></code></p>
<p>Now here's where polymorphism gets fun. The Hole will respond different to the thrusting and load depending on what specific type of Hole we're implementing. </p>
<p>First we must implement an abstract class which defines an abstract interface. </p>
<p><code><br>abstract class Hole {<br>&nbsp; public abstract void TakeAThrust(Penis p); </code></p>
<p>&nbsp; public abstract void TakeALoad(Load l);<br>}<br></p>
<p>Now all that's left is the varying implementations of these methods in the seperate concrete classes. For example, an Ass' implementation of TakeAThrust could look something like: </p>
<p><code><br>public void TakeAThrust(Penis p) {<br>&nbsp; if(!enoughLube &amp;&amp; p.Circumference &gt; 6) {<br>&nbsp; &nbsp; analFissureCount++;<br>&nbsp; }<br>}<br></code></p>
<p>See, the beauty of it is... the Penis doesn't even need to know it's fucking an Ass for the Ass to behave like a proper Ass. </p>
<p>Now, let's see how we might implement TakeALoad differently for Mouth and Pussy: </p>
<p><code><br>//in Pussy<br>public void TakeALoad(Load l) {<br>&nbsp; //randomly determine whether to cause a pregnancy with a 10% chance...<br>&nbsp; if(Math.RandomNumber() % 10 == 0) {<br>&nbsp; &nbsp; this.Woman.EggFactory.GetEgg().Inseminate(l);<br>&nbsp; }<br>} </code></p>
<p>//in Mouth<br>public void TakeALoad(Load l) {<br>&nbsp; //50-50 chance of spitting or swallowing<br>&nbsp; if(Math.RandomNumber() % 1 == 0) {<br>&nbsp; &nbsp; this.Spit(l);<br>&nbsp; } else {<br>&nbsp; &nbsp; this.Swallow(l);<br>&nbsp; }<br>}<br></p>
<p>Putting it all together with client code </p>
<p>Now that we have our classes well planned out with polymorphism in mind, we can see the kind of luxury it is for the client programmer to work with. </p>
<p><code><br>//create an array of 4 women<br>Woman[] women = new Woman[]{new Woman(), new Woman(), new Woman(), new Woman()}; </code></p>
<p>//create a hole array to reference the holes of all 4 women, plus two additional holes.<br>Hole[] holes = new Hole[4*3 + 2]; </p>
<p>for(int i = 0; i &lt; women.Length; i++) { <br>&nbsp; holes[3 * i + 0] = women[i].Mouth;<br>&nbsp; holes[3 * i + 1] = women[i].Pussy;<br>&nbsp; holes[3 * i + 2] = women[i].Ass;<br>} </p>
<p>//additional holes (so the faggy programmers don't feel left out)<br>Man m = new Man();<br>holes[12] = m.Mouth;<br>holes[13] = m.Ass; </p>
<p>//now we loop through the holes and fuck them all with the same Penis </p>
<p>Penis p = new Man().Penis; </p>
<p>foreach(Hole h in holes) {<br>&nbsp; p.Fuck(h);<br>}<br></p>
<p>See how easy it makes it for the client programmer? </p>
<img src ="http://www.cppblog.com/Csystemthink/aggbug/44680.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Csystemthink/" target="_blank">snail</a> 2008-03-17 11:21 <a href="http://www.cppblog.com/Csystemthink/archive/2008/03/17/44680.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>