梦想成真(C++版)

学以致用 while (efforts) {gains++;}

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  5 Posts :: 6 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

2009年5月19日 #

讨论一下结构(struct)与类(class)的区别,你认为结构(struct)有些什么作用,可以完全用类(class)取代么?? --------------------------------------------------------------- 首先,讨论这个问题应该仅从语法上讨论,如果讨论不同人之间编程风格上的差异,那这个问题是没有答案的。毕竟不同的人偏好不同。 从语法上,在C++中(只讨论C++中)。class和struct做类型定义时只有两点区别: (一)默认继承权限。如果不明确指定,来自class的继承按照private继承处理,来自struct的继承按照public继承处理; (二)成员的默认访问权限。class的成员默认是private权限,struct默认是public权限。 除了这两点,class和struct基本就是一个东西。语法上没有任何其它区别。 不能因为学过C就总觉得连C++中struct和class都区别很大,下面列举的说明可能比较无聊,因为struct和class本来就是基本一样的东西: (1)都可以有成员函数;包括各类构造函数,析构函数,重载的运算符,友元类,友元结构,友元函数,虚函数,纯虚函数,静态函数; (2)都可以有一大堆public/private/protected修饰符在里边; (3)虽然这种风格不再被提倡,但语法上二者都可以使用大括号的方式初始化:A a = {1, 2, 3};不管A是个struct还是个class,前提是这个类/结构足够简单,比如所有的成员都是public的,所有的成员都是简单类型,没有显式声明的构造函数。 (4)都可以进行复杂的继承甚至多重继承,一个struct可以继承自一个class,反之亦可;一个struct可以同时继承5个class和5个struct,虽然这样做不太好。 (5)如果说class的设计需要注意OO的原则和风格,那么没任何理由说设计struct就不需要注意。 (6)再次说明,以上所有说法都是指在C++语言中,至于在C里的情况,C里是根本没有“class”,而C的struct从根本上也只是个包装数据的语法机制。 (7) 不管是定义class还是struct, 大括号后面的分号是必需的,因为在类定义之后可以接一个对象定义列表
posted @ 2009-05-19 17:23 Betty 阅读(370) | 评论 (0)编辑 收藏

Some Useful Run comands which I use frequently as below: Note: win+R 打开run命令框 appwiz.cpl添加删除程序 mspaint画图程序 cmd命令窗口 notepad记事本 wordpad\write写字板 calc计算器 taskmgr任务管理器 narrator电脑叙述员 control控制面板 regedit注册表 msconfig系统配置 mstsc远程控制 shutdown -r 重启 nslookup网络IP地址查询 compmgmt.msc我的电脑管理 Winver系统版本 logoff注销 explorer我的文件~win+e
posted @ 2009-05-19 17:19 Betty 阅读(226) | 评论 (0)编辑 收藏

1 调用成员函数时为什么有的地方要用->有的地方用.呢? If the virable is a Pointer, then we have to use -> to call it's member functions else, we use . eg: class Student{.....}; void main() { Student *A[3]; for(int j=0;jgetscore();//declared in class Student } } 2 iomanip.h 是流控制 头文件,常见与iostream配合使用的,如果使用还 要 using namespace std; 包含的是一些 流格式控制 的函数、方法 它是I/O流控制头文件, 就像C里面的格式化输出一样.以下是一些常见的控制函数的:主要是对 cin,cout之类的一些操纵运算子, 比如 setw ,left right,setfill,,setbase,setprecision等等。 .以下是一些常用的: dec 置基数为10 相当于"%d" hex 置基数为16 相当于"%X" oct 置基数为8 相当于"%o" setfill(c) 设填充字符为c setprecision(n) 设显示小数精度为n位 setw(n) 设域宽为n个字符 setioflags(ios::fixed) 固定的浮点显示 setioflags(ios::scientific) 指数表示 setiosflags(ios::left) 左对齐 setiosflags(ios::right) 右对齐 setiosflags(ios::skipws 忽略前导空白 setiosflags(ios::uppercase) 16进制数大写输出 setiosflags(ios::lowercase) 16进制小写输出 setiosflags(ios::showpoint) 强制显示小数点 setiosflags(ios::showpos) 强制显示符号 举例:   #include   #include   using namespace std;   int main()   {   cout<<12345.0<
posted @ 2009-05-19 17:18 Betty 阅读(137) | 评论 (0)编辑 收藏

平时学习过程中遇到的一些小问题,简单回顾一下: 1 C/C++预处理器区分大小写吗? 答:C系语言都区别,如C,C++,C#,Java等;VB.Net不区分大小写 2 运行时最好将前面的运行黑色窗体关掉;不然可能会有异常 3 两个Double类型数据相减比较, double 一般不直接==比较 4 Int a ; Sqrt(a);中 a不能为int类型; 可以用sqrt(double (a)) 5 Do clean you folder before zipping a project. You should remove "Debug" folder , *.ncb and *.suo file. So the size will become much smaller 6? #include When I run in win32 console it should be Without “.h”just like #include I don’t know why some books have this phrase with .h , What differences are there between the two? 7 Using namespace std; This is essencial ,But we must know that in the CLR of VC++ projcet there is a sentence named “using namespace system”, They are different 8 #include If it is neglected : The included headfiles such as #include will be skipped when look for precompiled header 9 Write 2==i instead of i==2put the virable behand of the number int * p; write null==p instead of p==null It doesn't look strange, indead, in company we have to do like that 10 The function of srand (垂头剪子布) If there is no srand but only rand ,then it will output the same data for N times “如果不用srand(),而直接用rand(),每次运行产生的随机数都是一样的。这个你写一段代码就可以知道。 srand()起设置随机种子的作用. 用srand((unsigned)time(NULL))可以产生伪随机数列,一般情况都可以满足要求. 详细情况你查MSDN就知道了,上面也有范例,本来想拷过来,算了,你自己看吧,自己动手要好些。” Sometimes it will remind me that it will lead loss of data when execute srand(time (NULL)), but sometimes once I close the C command window there is no errors in fact ,I don’t know why 11 The use of “_itoa” function “_Itoa:”can Converts an integer to a string.but for safe you’d better use “_itoa_s” 12 The style of code as a engineer the code should be tide up. E.g. int main() {int aCnt = 0... It must be as this: int main() { int aCnt = 0... You can select all text in the .cpp file and call Edit/Advanced/Format Selection. it will tide up the code. 13 It's not a good way to declare virables in one line. E.g. int aCnt = 0,eCnt = 0,iCnt = 0,oCnt = 0,uCnt = 0, also case 'a':case'A': ++aCnt;break; should be case 'a': case 'A': ++aCnt; break;
posted @ 2009-05-19 17:17 Betty 阅读(177) | 评论 (0)编辑 收藏

size_t主要的用处是这样:size_t sizeof(...) 明白吗?如果在一个表示“数据类型的大小”的地方使用unsigned int或者BYTE,别人很容易看不明白;但是如果在这里写上size_t,就比较容易明白了 eg: void main() { size_t i,j; int k=0; const size_t rsize = 3; const size_t csize = 4; int A[rsize][csize]; for( i=0; i!=rsize;i++) for( j=0;j!=csize;j++) { A[i][j]=k++; cout<
posted @ 2009-05-19 17:10 Betty 阅读(3337) | 评论 (0)编辑 收藏