C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章 C++ 概述

上传人:E**** 文档编号:89155478 上传时间:2019-05-19 格式:PPT 页数:54 大小:182.51KB
返回 下载 相关 举报
C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章  C++ 概述_第1页
第1页 / 共54页
C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章  C++ 概述_第2页
第2页 / 共54页
C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章  C++ 概述_第3页
第3页 / 共54页
C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章  C++ 概述_第4页
第4页 / 共54页
C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章  C++ 概述_第5页
第5页 / 共54页
点击查看更多>>
资源描述

《C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章 C++ 概述》由会员分享,可在线阅读,更多相关《C++面向对象程序设计 教学课件 ppt 作者 张德慧 周元哲 主编 第1章 C++ 概述(54页珍藏版)》请在金锄头文库上搜索。

1、C+面向对象程序设计,An Introduction to Object-Oriented Programming Using C+,C+面向对象程序设计教学内容,第1章 C+概述 第2章 类和对象 第3章 面向对象程序设计概述 第4章 进一步学习类和对象 第5章 堆与复制构造函数 第6章 继承性:派生类 第7章 运算符重载 第8章 虚函数和多态性 第9章 模板 第10章 类库和C+的标准模板库STL 第11章 输入输出流 第12章 异常处理,第一章,第1章 C+概述 C+ A Better C,1.1 C+起源和特点,1.1.1 C+的起源 1.1.2 C+的特点,1.2 C+程序的结构,1

2、.2.1 C程序与C+程序比较 1.2.2 C+程序结构 1.2.3 C+程序的编辑、编译和运行,C程序与C+程序比较之一,main( ) int a, b, sum; /* 定义三个整型变量 */ a = 123; b = 456; sum = a + b; printf(“sum is %dn“, sum); ,main( ) int a, b, sum; /定义三个整型变量 a = 123; b = 456; sum = a + b; cout sum; ,C程序与C+程序比较之二,#include “stdio.h“ main( ) char a,b,c; a = B; b = O;

3、c = Y; putchar(a); putchar(b); putchar(c); ,#include “iostream.h“ void main( ) char a,b,c; a = B; b = O; c = Y; coutabc; ,1.3 C+的一些新特性,1.3.1 单行注释和新的I/O流 1.3.2 const修饰符 1.3.3 内联函数 1.3.4 函数原型 1.3.5 带缺省参数的函数 1.3.6 函数名重载 1.3.7 new和delete运算符 1.3.8 引用(reference),1.3.1 单行注释和新的I/O流,/ I/O stream #include mai

4、n( ) int i; float f; char s80; cout ifs; cout “Heres your data:“i fendlsn; return 0; ,单行注释和新的I/O流(续),cout 是预定义的输出流对象,类似于C语言中的stdout。 输出运算符,可用于输出C+语言中任何基本类型的数据。 cin 是预定义的输入流对象,类似于C语言中的stdin。 输入运算符,可用于输入C+语言中任何基本类型的数据。 (注意:输入和输出并不是C+语言的组成部分,它们由流库iostream支持。),输入含有空格的字符串,/ Use getline() to read a string

5、 that contains spaces. #include #include using namespace std; int main() char str80; cout “Enter your name: “; cin.getline(str, 79); cout str n; return 0; ,1.3.2 const 存取修饰符,对象 A:亲爱的,你千万不能变心? 对象 B:放心吧!亲爱的。 对象 A:你发誓! 对象 B:不用发誓,因为我是const ! const 对象 B;,常量 Constants,在C中,可以使用#define来定义符号常量 。 C+提供了一种更灵活、更

6、安全的方式来定义常量,即使用关键字const来定义符号常量。,常量例子 Constant examples,const float PI = 3.1415926; / PI是一个常量 const int v =1,2,3,4; /数组元素vi是常量 const int x; / error: no initializer /定义常量时应初始化,否则出错。,void f( ) model =200; / error v2+; / error ,值替换 value substitution,#define BUFSIZE 100,const int bufsize = 100;,Because o

7、f subtle bugs that the preprocessor might introduce, you should always use const instead of #define value substitution.,常量const和指针,指针所指向的对象为常量 Pointer to const 指针本身为常量 const pointer The const modifies the thing it is “closest to.”,指向常量的指针 Pointer to const,const int* u; / pointer to constant *u=18; /

8、 error: u points to constant u= p; / OK,常指针 const pointer,int d = 1; int* const w = / error: w is const,const修饰函数参数,void print_salary (const float *salary) coutsalaryn; const可以阻止参数被函数修改,const 的其他用途 Other uses of const,return types, class objects and member functions,volatile 存取修饰符,修饰符volatile通知编译器,变

9、量值可能由程序中没有显示说明的方式所改变,因此在作编译优化时,不要通过将该变量放入寄存器来提高速度。 Volatile原意是:可变的,不稳定的 extern volatile clock; extern const volatile clock;,1.3.3 内联函数(内嵌函数, 内置函数 inline function),在函数声明或定义的前面加上关键字“inline”,该函数就被声明为内联函数。 inline int max(int x, int y) / max被说明为内联函数 int z; if(xy) z=x; else z=y; return (z); 内联函数的调用方法与普通函数

10、没有区别。,类中定义的内联函数Inline function,Any function defined within a class body is automatically inline, class Date int day, month, year; public: void init_date(int dd,int mm,int yy) day=dd; month=mm; year=yy; ;,Here init_date is a inline function,函数调用时的时间开销,1函数调用时的时间开销:保护现场,恢复现场。 2用关键字inline说明内嵌函数。编译器直接用内嵌

11、函数体的编译代码插入在函数调用语句处,这一过程称为函数的嵌入扩展。利用内嵌函数减少了调用普通函数时的压栈和弹栈操作,从而提高程序的运行速度。 3内嵌函数比带参数的宏的好处。 一般情况下,只有较短的函数才定义为内嵌函数。使用内嵌函数实际上是一种增加空间开销以减小时间开销的方法。,为什么使用内联函数,Efficiency 效率 在C程序中,可使用宏macros达到同样的目的,但是宏是通过预处理来处理的,不进行类型检查,容易造成难以发现的错误。 宏macros在类的内部不能使用,宏不能作为类的成员。,为什么使用内联函数(cont.),为了克服宏的上述缺陷, C+ 引入了内联函数。内联函数具有高效率,

12、而且: 进行类型检查,避免出现类型不匹配的错误。 可以作为类的成员函数。 To retain the efficiency of the preprocessor macro, but to add the safety and class scoping of true functions, C+ has the inline function.,How do inline functions work 编译器处理内联函数的过程,类型检查 Type checking (To assure safety ) 将函数代码插入到函数调用处 then substitutes the function

13、 body for the function call 这样函数代码将占据更所得存储空间 The inline code does occupy space The short,small and frequently called functions are suitable for inline functions.,1.3.4 函数原型 ( function prototype ),什么是函数原型? 描述函数原型的三大要素: 函数名 参数类型 函数返回值类型 函数原型的例子: int translate(float x, float y, float z); int translate(

14、float, float, float);,【例1.7】,void sqr_it( ); /* function declaration */ int main( ) int x; x=10; sqr_it(x); printf(“The square of x is %dn“,x); return 0; void sqr_it(int *i) *i=(*i)*(*i); ,运行时出错,【例1.7】本例的C程序能够成功通过诸如Turbo C这样的C编译器的检查,但会在运行阶段发生错误。 该程序运行后的结构显示如下: The square of x is 10 Null pointer assi

15、gnment,使用函数原型执行强类型检查【例1.8】,void sqr_it(int *i); /函数原型 int main( ) int x; x=10; sqr_it(x); cout“The square of x is “xn; return 0; void sqr_it(int *i) *i=(*i)*(*i); ,type mismatch 类型不匹配,函数原型的作用,C+语言是强类型化语言,任何函数在使用以前必须有该函数的原型说明,以便进行实际参数与形式参数之间的类型匹配检查。 函数返回值的类型和函数参数的类型、个数、次序在函数声明,函数定义和函数调用时必须匹配。 C+语言的编译

16、器执行上述检查能显著减少很多隐藏的错误。,函数原型与C语言的函数类型说明,函数原型是在C语言的函数类型说明(函数声明)的基础上进行扩充后形成的, 它不但说明了函数返回值的类型,还要确定函数参数的类型、个数、次序及缺省值。,1.3.5 带缺省参数的函数,例如:以下函数带有一个缺省值为0的参数。 void myfunc(double d=0.0) myfunc(198.234); / pass an explicit value myfunc( ); / let function use default,缺省参数的例子,void DrawCircle(int x,int y,int r=10); DrawCircle(50,20); DrawCi

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号