算法与数据结构:lecture 1 面向对象程序设计_第1页
算法与数据结构:lecture 1 面向对象程序设计_第2页
算法与数据结构:lecture 1 面向对象程序设计_第3页
算法与数据结构:lecture 1 面向对象程序设计_第4页
算法与数据结构:lecture 1 面向对象程序设计_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、面向对象程序设计从C到C+C语言的特点面向过程的:结构化和模块化的语言既有高级语言的优点,也有低级语言的许多特点C+是对C的增强对C语言的功能做了不少扩充。增加了面向对象的机制。对象:把数据和处理数据的过程当成一个整体。最简单的C+程序#include using namespace std;int main( )coutThis is a C+ program.endl;return 0;1、如何输出“hello world!”2、如何输出* * *输出串输出换行另一个简单的C+程序#include using namespace std;int main( )int a, b;coutpl

2、ease input a and bab;if(b!=0)couta/b=a/bendl;elsecoutb should not be zeroendl;return 0;思考:如何输出以下图形(高度为N20)P16习题6#include using namespace std;int main( )int a, b, c;a = 10;b = 23;c = a + b;couta+b=;coutc;couta+b=a+b;return 0;g:tempex1.cpp(3) : error C2065: c : undeclared identifierg:tempex1.cpp(4) :

3、error C2065: cout : undeclared identifierg:tempex1.cpp(4) : error C2297: : illegal, right operand has type char 5程序的调试int main( )int a,b,c;c = a+b;couta+b=a+b;return 0;g:tempex1.cpp(4) : error C2065: cout : undeclared identifierg:tempex1.cpp(4) : error C2297: : illegal, right operand has type char 5

4、#include using namespace std;int main( )int a,b,c;c = a+b;couta+b=a+b;return 0;g:tempex1.cpp(6) : error C2676: binary : class std:basic_ostreamchar,struct std:char_traits does not define this operator or a conversion to a type acceptable to the predefined operator#include using namespace std;int mai

5、n( )int a,b,c;c = a+b;couta+b=a+b;return 0;g:tempex1.cpp(5) : warning C4700: local variable a used without having been initializedg:tempex1.cpp(5) : warning C4700: local variable b used without having been initialized#include using namespace std;int main( )int a=15,b=17,c;c = a+b;couta+b=a+b;return

6、0;包含类的C+程序#include using namespace std;class Studentprivate:int num; float score;public:void setdata( )cinnumscore;void display( ) coutnum=numendlscore=scorestud1.numstud1.score 不可替代分析以下程序的输出#include using namespace std;class Bookpublic:int id;public:void display( )switch(id)case 1: coutGone with th

7、e windendl; break;case 2: coutThe thorn birdsendl; break;case 3: coutPride and prejudiceendl; break;default: coutUnknownendl; break;int main( )Book mybook;mybook.id=1;mybook.display( );mybook.id=3;mybook.display( );mybook.id=10;mybook.display( );return 0;常量数值常量:也称常数整型常量短整数:-32768 32767长整数:-214748364

8、8 2147483647unsigned:非负表示方法:十进制(20)、八进制(020)、十六进制(0X20)浮点数十进制小数:21.456, 21.456f, 21.456L指数:0.314159e1, 3.14159E0#include using namespace std;int main( )int a=030, b=30, c=0 x30, d=0 x1f;couta=a,b=b,c=c;cout,d=dendl;return 0;思考:int a=038; 对不对?常量(2)字符常量普通字符常量:用单撇号括起来的一个字符转义字符常量:以开头的字符序列以ASCII码存储字符串常量用

9、双撇号括起来的部分在字符串最后会自动加上0作为字符串的结束标志符号常量:以标识符形式出现的常量#include using namespace std;int main( )int i, j;char c1, c2;i = A;j = B;c1 = i+32;c2 = j+32;couti jn;coutc1 c2endl;cout(int) A (char) (i+32)endl;return 0;将(char) (i+32) 改成:(char ) i+32会怎么样?变量在程序运行期间,其值可以改变的量有一个名字在内存中占据一定的存储单元,以存放值常变量(只读变量):定义变量时,加上关键字c

10、onst,在程序运行期间值不能改变的量。P40 习题4#include using namespace std;int main( )char c1=C, c2=+, c3=+;coutI say: c1c2c3;coutttHe says:C+ is very interesting! n;return 0;char前能否加const,会有什么影响?P40 习题7#include using namespace std;int main()int i,j,m,n;i=8;j=10;m=+i+j+;n=(+i)+(+j)+m;coutitjtmtnendl;return 0;1.输出什么?2.

11、将j+改成(j+)有什么影响?C+程序和语句一个程序包含一个或多个程序单位,每个程序单位由以下部分组成预处理命令: #include, #define 声明部分:对数据类型和函数的声明,对变量的声明和定义函数:包括函数首部和函数体,在函数体中可以包含若干声明和执行语句。#include using namespace std;#define MAX 10int main( )const int K=5;int aMAX, bK, i, j ; for(i=0; iMAX; i+) ai = i;for(i=0; iK; i+) bi = i*i;for(i=0; iMAX; i+)for(j=

12、0; jK; j+)coutai+bj;if(j = K-1)coutendl; elsecoutt;return 0;输出什么?C+的输入与输出在C语言中,I/O功能是通过scanf函数和printf函数来实现的。非类型安全:所期望的参数个数与类型取决于包含在第一个参数中的信息,而这一信息对编译器是没有用的。不可扩充性:只能输入和输出已知的基本数据类型值,但C+中大量的类对象,其输入输出格式是未预先定义的。#include void main( )int j=10;float f=2.3;printf(%dn, f);scanf(%d, &f);scanf(%d,j);printf(%dn,

13、abcde);非类型安全问题C+的输入与输出(2)在C+中,I/O功能是通过输入输出流库中的流对象cin和cout实现的。流:来自设备或者传给设备的一个数据流。cin是输入流对象的名称,是流提取运算符cout是输出流对象的名称,是流插入运算符使用cin,cout和流运算符时,必须: #include using namespace std;输入流与输出流的基本操作cout语句:一般格式: cout 表达式1 表达式2 变量1 变量2 变量n;系统会根据变量的类型从输入流中提取相应长度的字节。一个提取运算符只能提取一项。在输入流和输出流中使用控制符程序中应当包含iomanip文件。常用控制符se

14、tprecision(n): 设置浮点数的精度为n位。setw(n): 设置字段宽度为n位。浮点数的表示:setiosflags(ios:fixed), setiosflags(ios:scientific)对齐:setiosflags(ios:left), setiosflags(ios:right)进制: dec, hex, oct#include #include using namespace std;int main()double a=123.456789012345;coutaendl;coutsetprecision(9)aendl;coutsetprecision(5)set

15、iosflags(ios:fixed)a;coutresetiosflags(ios:fixed)endl;coutsetiosflags(ios:scientific)aendl;coutsetiosflags(ios:scientific)setprecision(4)aendl;return 0;能不能去除resetiosflags#include #include using namespace std;int main()float a=123.456; int b=123456;coutb hexbendl;coutsetiosflags(ios:uppercase)b;cout

16、setw(10)bendl;coutsetw(10)a setw(10)bendl;coutsetiosflags(ios:left)setw(10)a setw(10)bendl;coutb octb decbendl;return 0;P58 例3.5#include #include using namespace std;int main()float a,b,c,x1,x2;cinabc;x1=(-b+sqrt(b*b-4*a*c)/(2*a);x2=(-b-sqrt(b*b-4*a*c)/(2*a);coutx1=x1endl;coutx2=x2endl;return 0;本程序可

17、能存在什么问题?ab=pow(a,b)ln(a)=log(a)#include #include using namespace std;int main( ) int s=1;double n=1, t=1, pi=0;while(fabs(t)1e-7) pi += t;n += 2;s *= -1;t = s/n;pi *= 4;coutpi=piendl;return 0;P81 例3.12n可以为int型吗?#include using namespace std;int main( ) long s=1;long sum=0;for(int i=1; i21; i+) s*=i;sum += s;coutsumendl;return 0;P86 练习18求#include using namespace std;bool check(int id) int a3;for(int i=2; i=0; i-)ai=id % 10;if(ai=0 | ai3) return false;id /= 10;if(a0=a1 | a0=a2 | a1=a2) return false;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论