c程序设计(part2).ppt
《c程序设计(part2).ppt》由会员分享,可在线阅读,更多相关《c程序设计(part2).ppt(78页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、C+程序设计(part 2)OOPnWhatnnon-OO Solution#include#define STACK_SIZE 100struct Stack int top;int bufferSTACK_SIZE;void main()Stack st1,st2;st1.top=-1;st2.top=-1;int x;push(st1,12);pop(st1,x);bool push(Stack&s,int i)if (s.top=STACK_SIZE-1)printf(“Stack is overflow.n”);return false;else s.top+;s.buffers.t
2、op=i;return true;bool pop(Stack&s,int&i)if (s.top=-1)printf(“Stack is empty.n”);return false;else i=s.buffers.top;s.top-;return true;st1.buffer2=-1;st2.buffer2+;_OOPnOO Solution#include#define STACK_SIZE 100class Stack private:int top;int bufferSTACK_SIZE;public:Stack()top=-1;bool push(int i);bool p
3、op(int&i);void main()Stack st1,st2;int x;st1.push(12);st1.pop(x);bool Stack:push(int i);if (top=STACK_SIZE-1)cout “Stack is overflow.n”;return false;else top+;buffer top=i;return true;bool Stack:pop(int&i)if (top=-1)cout “Stack is empty.n”;return false;else i=buffer top;top-;return true;#include#def
4、ine STACK_SIZE 100struct Stack int top;int bufferSTACK_SIZE;void main()Stack st1,st2;st1.top=-1;st2.top=-1;int x;push(&st1,12);pop(&st1,x);Xbool push(Stack*const this,int i)bool pop(Stack*const this,int&i)this-this-this-this-this-this-this-this-st1.buffer2=-1;EncapsulationInformation HiddingOOPnConc
5、eptsnProgramObject1+Object2+ObjectnnObject:Data+OperationnMessage:function callnClassnObject-OrientednObject-BasednOnly Provide EncapsulationnWithout InheritancenAda,etcOOPnWhyn评价标准nEfficency of DevelopmentEfficency of DevelopmentnQualityQualitynExternalExternal Correctness、Efficiency、Robustness、Rel
6、iabilityReliabilityUsabilityUsability、ReusabilityReusabilitynInternalInternal Readability、Maintainability、PortabilityHUIUX产品在规定的条件下和规定的时间内完成规定功能的能力产品在规定的条件下和规定的时间内完成规定功能的能力(概率度量:可靠度)概率度量:可靠度)需求需求架构设计架构设计构建模式构建模式代码代码测试用例测试用例项目组织结构项目组织结构OOPnAdvantages 提高提高开发效率开发效率和和软件质量软件质量n更高层次的抽象数据抽象n数据封装n更好的模块化支持(高
7、内聚、低耦合)n软件复用(部分重用)n对需求变更具有更好的适应性C+vs Java类n类n定义 class ;n组成n成员:成员变量、成员函数n成员变量:在类定义中给出的是声明n成员函数:在类定义中给出的是定义或声明n类成员的访问控制描述类class TDate public:void SetDate(int y,int m,int d);int IsLeapYear();void Print();private:int year,month,day;类定义不完整类n成员变量n声明 在类定义中对成员变量的说明是声明n不能赋值 在类定义中声明成员变量时不能初始化n引用或指针类型成员变量类clas
8、s A;class B A a;/Error B b;/Error A *p;/OK B *q;/OK A&aa;/OK B&bb;/OK 类型还未定义/未定义完全 只能声明成指针或引用类n成员函数n在类定义中定义成员函数(inline)class TDate public:void SetDate(int y,int m,int d)year=y;month=m;day=d;int IsLeapYear()return(year%4=0&year%100!=0)|(year%400=0);private:int year,month,day;类n在类定义外定义成员函数class TDate
9、public:void SetDate(int y,int m,int d);void Print();private:int year,month,day;void TDate:SetDate(int y,int m,int d)year=y;month=m;day=d;void TDate:Print()coutyear.month.dayendl;a.ha.cpp类n成员函数的重载 遵循一般函数的重载规则class A public:void f();int f(int i);double f(double d);void main()A a;a.f();a.f(1);a.f(1.0);
10、类n对象n属性和行为的封装体n类的实例,属于值的范畴 类属于类型范畴,用于描述对象的属性构造函数n对象的初始化n描述n与类同名、无返回类型n自动调用,不可直接调用n可重载n默认构造函数:无参数的构造函数n系统提供的默认构造函数n当类中未提供构造函数时,编译系统自动提供n参与对象的构造过程n通常定义为 publicn有时也声明为私有的,其作用是限制创建该类对象的范围,即只能在本类和友元中创建该类对象构造函数n构造函数的调用n创建对象时,构造函数将自动被调用,所调用的构造函数在创建对象时指定 class A public:A();A(int i);A(char*p);A a1=A(1);A a1(
11、1);A a1=1;/调A(int i)A a2=A();A a2;/调A(),注意:不能写成:A a2();A a3=A(“abcd”);A a3(“abcd”);A a3=“abcd”;/调A(char*)A a4;/调用a0、a1、a2、a3的A()A b5=A(),A(1),A(abcd),2,xyz“;成员初始化表n成员初始化表n定义构造函数时,函数头和函数体之间可以加入一个对数据成员进行初始化的表,用于对数据成员进行初始化 class A int x;const int y;int&z;public:A():y(1),z(x),x(0);成员初始化表class A int m;pu
12、blic:A()m=0;A(int m1)m=m1;class B int x;A a;public:B()x=0;B(int x1)x=x1;B(int x1,int m1):a(m1)x=x1;void main()B b1;/调用调用B:B()和和A:A()B b2(1);/调用调用B:B(int)和和A:A()B b3(1,2);/调用调用B:B(int,int)和和A:A(int)成员初始化表n在构造函数中尽量使用成员初始化表取代赋值动作nconst 成员/reference 成员/对象成员n效率高n数据成员太多时,不采用本条准则n降低可维护性成员初始化表n成员初始化表中的声明次序n
13、成员初始化的次序取决于它们在类定义中的声明次序,与它们在成员初始化表中的次序无关n减轻编译系统的负担n注意类成员定义的次序class CString char *p;int size;public:CString(int x):size(x),p(new charsize);?析构函数n析构函数n()n对象消亡时,系统自动调用class String char*str;public:String()str=NULL;String(char*p)str=new charstrlen(p)+1;strcpy(str,p);String()delete str;int length()return
14、strlen(str);char get_char(int i)return stri;Java:finalize()RAII vs GCResource Acquisition Is Initialization析构函数 void set_char(int i,char value)stri=value;char&char_at(int i)return stri;char*get_str()return str;char*strcpy(char*p)delete str;str=new charstrlen(p)+1;strcpy(str,p);return str;String&strc
15、py(String&s)delete str;str=newcharstrlen(s.str)+1;strcpy(str,s.str);return*this;char*strcat(char*p);String&strcat(String&s);拷贝构造函数nCopy Constructorn创建一个对象时,用另一个同类的对象对其初始化n自动调用 class A int x,y;char *p;public:A();A(const A&a);/拷贝构造函数,const可有可无拷贝构造函数和n三种情况将调拷贝构造函数ncase1A a;A b=a;ncase2f(A a);.A b;f(b);
16、拷贝构造函数ncase3 A f()A a;return a;.f();n如果类定义中没有给出拷贝构造函数,则编译系统提供一个默认拷贝构造函数n默认拷贝构造函数的行为n逐个成员初始化(member-wise initialization)n如果相应的类有对象成员,则该定义是递归的拷贝构造函数class string char*p;public:string(char*str)p=new charstrlen(str)+1;strcpy(p,str);string()delete p;string s1(“abcd”);string s2=s1;abcdpps1s2string:string(c
17、onst string&s)p=new charstrlen(s.p)+1;strcpy(p,s.p);deep copy悬挂指针何时需要copy constructor?abcd动态对象n动态对象n在堆(Heap)中创建的对象称为动态对象nnew/deleten单个动态对象的创建与撤消 class A public:A();A(int);为什么要引入new、delete操作符?constructor/destructor动态对象A*p,*q;p=new A;n 在程序的堆中申请一块大小为sizeof(A)的空间n调用A的默认构造函数对该空间上的对象初始化n返回创建的对象的地址并赋值给pq=n
18、ew A(1);nn调用A的另一个构造函数 A:A(int)ndelete p;n调用p所指向的对象的析构函数n释放对象空间 delete q;动态对象np=(A*)malloc(sizeof(A)free(p)malloc 不调用构造函数 free 不调析构函数nnew 操作符n自动分配足够的空间n自动返回指定类型的指针n可以重载 动态对象数组n动态对象数组的创建与撤消A*p;p=new A100;delete p;n注意n不能显式初始化,相应的类必须有默认构造函数ndelete中的不能省Const 成员nconst 成员nconst 成员变量n在成员变量的声明中加上const,则表示是常量
19、class A const int x;n初始化放在构造函数的成员初始化表中进行class A const int x;public:A(int c):x(c)Const 成员nconst 成员函数class A int x,y;public:void f();const A a;na.f()合法?na是常量,在程序中不应改变a的值(改变a的成员变量的值)n如果A:f中没有改变a的成员变量的值,则是合法的,否则是不合法的Const 成员n编译程序往往无法知道函数f中是否改变了a的成员变量的值n为了安全,编译程序认为a.f()不合法na.f()合法n在A:f中不能改变a的成员变量的值n在f的定义
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计 part2
限制150内