02 2007 档案

类定义小结
摘要:////////////////////默认构造函数////////////////////////1>默认构造函数要么没参数,要么参数在申明时都有默认值 2>如不显式定义编译器将自动创建一个空的默认构造函数 3>派生类构造函数没有显式的调用基类构造函数,就会自动调用基类默认构造函数 4>如果定义某构造函数,编译器不会再自动定义默认构造函数需要显式的定义 ///////////////////赋值... 阅读全文

posted @ 2007-02-26 08:03 Edward Xie 阅读(209) 评论(0) 推荐(0)

动态内存分配和使用友元(继承)
摘要://////////////////////////////////////////////////////////***************************************************////* 1>派生不使用new先调用派生析构函数对派生新成员进行处理,再调用基类的析构函数。(通常需要在基类中把析构函数定义为虚拟,因为在调用基类引用或指针的时候,析构函数就只能处... 阅读全文

posted @ 2007-02-26 04:43 Edward Xie 阅读(232) 评论(0) 推荐(0)

显示复制构造函数与赋值函数模型
摘要://///////////////赋值/////////////////////////////////baseDMA & baseDMA::operator=(const baseDMA & rs){ if (this == &rs) return *this; delete [] label; label = new char[strlen(rs.label) ... 阅读全文

posted @ 2007-02-26 04:43 Edward Xie 阅读(157) 评论(0) 推荐(0)

抽象基类(ABC)
摘要:被调用方法为虚拟,所以根据调方法的对象类型来判断,使用合适的成员函数 /////////////////////////////////////////////////////////////////////////main.cpp///////////////////////////// usebrass3.cpp -- polymorphic example, abstract base cl... 阅读全文

posted @ 2007-02-26 04:23 Edward Xie 阅读(333) 评论(0) 推荐(0)

简单基类 例子
摘要:///////////////////////////////////////////////////////////////////table.h//////////////////////#ifndef TABLE1_H_#define TABLE1_H_ class TableTennisplayer{private: enum {LIM=20}; char firstname[LIM]; ... 阅读全文

posted @ 2007-02-25 23:00 Edward Xie 阅读(227) 评论(0) 推荐(0)

队列类实例
摘要:///////////////////////////////////////////////////////////////////////////////queue.h////////////////////////////#ifndef QUEUE_H_#define QUEUE_H_ class Customer{private: long arrive; int processtime;... 阅读全文

posted @ 2007-02-23 08:28 Edward Xie 阅读(201) 评论(0) 推荐(0)

有关类对象的NEW布局操作符
摘要:#include #include #include //包含布局操作符原型using namespace std;const int BUF=512; class JustTesting{private: string words; int number;public: JustTesting (const string & s="just testing",int n=0) {words=... 阅读全文

posted @ 2007-02-22 03:33 Edward Xie 阅读(185) 评论(0) 推荐(0)

返回对象的有关说明
摘要:1.返回指向const对象的引用//*如函数返回传递给它的对象,可以通过传递引用提高效率ep:const Vector & Max(const Vector & v1,const Vector & v2){ if (v1.magval()>v2.magval()) return v1; else return v2;} 2.返回指向非const对象的引用//*重载赋... 阅读全文

posted @ 2007-02-21 02:52 Edward Xie 阅读(151) 评论(0) 推荐(0)

几个典型操作符重载例子
摘要://* char operator [] (int i); const operator [] (int i) const; //* String & operator = (const String &); String & operator = (const char *); //* friend bool operator (const String & st,const ... 阅读全文

posted @ 2007-02-20 08:02 Edward Xie 阅读(360) 评论(0) 推荐(0)

隐式成员函数
摘要:隐式成员函数.1 .默认构造函数声明 ClassName();ClassName::ClassName(){} 2.复制构造函数//*在参数传值时或返回对象时一定被调用声明 ClassName(const ClassName &);1>新建对象并将其初始化为同类对象时调用ClassName cla(cal2); //直接创建ClassName cla=cla2; ... 阅读全文

posted @ 2007-02-20 06:00 Edward Xie 阅读(192) 评论(0) 推荐(0)

类的 自动 和 强转
摘要://*数字转对象 由单参数构造函数实现//*关闭单参构造函数的自动强制转换特性 explicit //*对象转数字 需要转换函数//*1>转换函数必须是类方法 2>转换函数不能指定转换返回类型 3>转换函数没有参数//*operater typename() ep:operator double();/////////////////////////////////... 阅读全文

posted @ 2007-02-19 02:53 Edward Xie 阅读(152) 评论(0) 推荐(0)

矢量类例子
摘要://*如果方法得到一个新的对象 考虑用构造函数去完成ep:vector Vector::operator + (const Vector & b) const{ return Vector(x+b.x,y+b.y); //返回一个构造函数实例} //*ctime包含time()原型 time(0)当前时间 //*rand()通过初始种子活的随机数 srand()可以覆盖默认种子 ... 阅读全文

posted @ 2007-02-18 21:23 Edward Xie 阅读(454) 评论(0) 推荐(0)

重载操作符 与 友元
摘要:///////////////////////////////////////////////////////////////////////////////* 友元函数在类申明中用friend前缀 函数定头不加 //*重载operator操作符 (argment-list)//*通常在重载#include "mytime.h" int main(){ using std::cout; usi... 阅读全文

posted @ 2007-02-18 01:15 Edward Xie 阅读(383) 评论(0) 推荐(0)

数组堆栈 (例子)
摘要://////////////////////////////////////stack.h///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#ifndef STACK_H_#define STAC... 阅读全文

posted @ 2007-02-14 23:36 Edward Xie 阅读(246) 评论(0) 推荐(0)

类定义使用 (初步)
摘要://///////////inline内联函数/////////////////////////////*类里面定义的为内联 //*外定义需加inline使其内联 //////////////构造函数//////////////////////////////////*与类同名 且没有返回类型//*使用:1> Stock gar=Stock("hello",10,20.2);//会产生临时对象... 阅读全文

posted @ 2007-02-14 02:34 Edward Xie 阅读(213) 评论(0) 推荐(0)

宽字符相关 (wchar_t)
摘要:#include int main(void){ using std::cout; cout>b; std::wcout<<b<<std::endl; std::wcout<<L"hello"<<std::endl; return 0;} 阅读全文

posted @ 2007-02-13 23:28 Edward Xie 阅读(178) 评论(0) 推荐(0)

名称空间 (相关) (例子)
摘要:名称空间是开放的,所以可以把名称加入到已有的名称空间中///////////////////////////////////////////////////// 名称空间内可包含名称空间ep:namespace elem{ double add(int x) int pal; struct{…}; namespace fire { int flame; ... 阅读全文

posted @ 2007-02-11 05:45 Edward Xie 阅读(169) 评论(0) 推荐(0)

布局new操作符 (例子)
摘要:#include #include //布局new操作符原型const int buf=512;const int n=5;char buffer[buf];//////////////////main( )///////////////////////////int main(void){ using namespace std; double *pd1,*pd2; pd1=new do... 阅读全文

posted @ 2007-02-11 05:23 Edward Xie 阅读(325) 评论(0) 推荐(0)

静态存储持续性 无链接性 (例子)
摘要:#include const int ArSize=10; void strcount(const char *str);//////////////////////////////////////////////////////////////////////////int main(void){ using namespace std; char input[ArSize]; char ne... 阅读全文

posted @ 2007-02-11 01:48 Edward Xie 阅读(126) 评论(0) 推荐(0)

头文件说明
摘要:名称空间定义函数原型使用#define或const定义的符号常量结构声明类声明模板声明内联函数////////////////////////////////////////// 阅读全文

posted @ 2007-02-10 21:07 Edward Xie 阅读(104) 评论(0) 推荐(0)

模板&重载 (例子) (具体化相关)
摘要:template void Swap(T & a,T & b); //模板定义/////////////////////////////////////////////// template void Swap(int,int); //显示实例化//////////////////////////////////////////////// template vo... 阅读全文

posted @ 2007-02-09 02:32 Edward Xie 阅读(133) 评论(0) 推荐(0)

取字符串长 合理申请空间
摘要:int len=strlen(str);n=(n<len)?n:len;char * p=new char[n+1];//////////////////////////////////////// int m=0;while(m<n&&str[m]!='\0') m++;char * p=new char[m+1];///////////////////////////////////... 阅读全文

posted @ 2007-02-08 23:50 Edward Xie 阅读(203) 评论(0) 推荐(0)

临时变量 引用 const (左值)
摘要:引用参数是const例: double refcube(const double & ra){ return ra*ra*ra;}1.实参类型正确,但不为左值*。2.实参类型不正确但可以转换为正确类型。*左值 :变量 数组元素 结构元素 引用 被解除引用指针 非左值:字面常量 多项的表达式产生作用为:生成临时变量,那么函数调用只对'临时变量'操作,对'实参'没有影响.再需要用引用... 阅读全文

posted @ 2007-02-07 02:32 Edward Xie 阅读(453) 评论(0) 推荐(0)

交换变量值问题
摘要://引用法 OKvoid swapr(int & a,int & b){ int temp; temp=a; a=b; b=temp;} //指针法 OKvoid swapp(int * a,int * b){ int temp; temp=*a; *a=*b; *b=temp;} //值传递 Fail 型参实参之间是单向'传值',使用拷贝数据操作,对原... 阅读全文

posted @ 2007-02-07 02:13 Edward Xie 阅读(113) 评论(0) 推荐(0)

递归函数
摘要:void recurs(argumentlist){ statements1 if(test) recurs(arguments) statements2}在满足条件是 statements1 按调用顺序执行 n 次然后退出if后 statements2按调用相反方向执行n次////////////////////////////////////... 阅读全文

posted @ 2007-02-05 02:25 Edward Xie 阅读(166) 评论(0) 推荐(0)

函数和结构
摘要://传递返回结构////////////////////////////////////////////////////////////////////////////////////#includestruct str_times{ int hours; int mins;};const int mins_per_hr=60;str_times sum(str_times t1,str_time... 阅读全文

posted @ 2007-02-05 01:41 Edward Xie 阅读(148) 评论(0) 推荐(0)

返回c风格字符串函数
摘要:#include char * buildstr(char c,int n);int main(void){ using namespace std; int times; char ch; cout>ch; cout>times; char *ps=buildstr(ch,times); cout0) { pstr[n]=c; } return pstr;}/////////... 阅读全文

posted @ 2007-02-05 00:23 Edward Xie 阅读(255) 评论(0) 推荐(0)

c风格字符串做参数函数(计算字符出现次数)
摘要:#include int c_in_str(const char * str,char ch);int main(){ using namespace std; char mmm[15]="minimum"; char * wail="ululata"; int ms=c_in_str(mmm,'m'); int us=c_in_str(wail,'u'); cout<<ms<<" m char... 阅读全文

posted @ 2007-02-05 00:00 Edward Xie 阅读(757) 评论(0) 推荐(0)

二维数组函数定义
摘要:int sum( int(*ar2)[5] , int size ); //ar2指向指针的指针///////////////////////////////////////////////////////////int sum( int arr[][5], int size ); 阅读全文

posted @ 2007-02-04 23:59 Edward Xie 阅读(331) 评论(0) 推荐(0)

数组函数 一例(数组填充)
摘要:#include const int aa=8;int fill_array(double ar[],int limit); int main(){ using namespace std; double arrr[aa]; int kk=fill_array(arrr,aa); cout>temp; ... 阅读全文

posted @ 2007-02-04 03:23 Edward Xie 阅读(267) 评论(0) 推荐(0)