面向对象程序设计教案1

上传人:沈*** 文档编号:69957345 上传时间:2022-04-06 格式:DOC 页数:173 大小:2.70MB
收藏 版权申诉 举报 下载
面向对象程序设计教案1_第1页
第1页 / 共173页
面向对象程序设计教案1_第2页
第2页 / 共173页
面向对象程序设计教案1_第3页
第3页 / 共173页
资源描述:

《面向对象程序设计教案1》由会员分享,可在线阅读,更多相关《面向对象程序设计教案1(173页珍藏版)》请在装配图网上搜索。

1、第一讲第1章 C+的初步知识一、面向对象的基本概念v 对象/实例(object/instance)v 类(class)v 封装(encapsulation)v 继承(inheritance)v 多态(polymorphism)v 重载(overload)v 消息(message)二、C+的开发运行环境1、Visual C+2、GCC:是一个用于Linux系统下编程的编译器3、WinTC4、Dev-C+三、最简单的C+程序1、例题1.1#includeusing namespace std;int main()coutThis is a C+ program.;return 0;程序功能:输出一

2、行字符:This is a C+ program.程序注释:(1)预处理命令#include iostream输入、输出流(2)using namespace std; 使用命名空间std。 第一行和第二行是每个C+都有的语句。(3)C+的主函数名与C一样,都是main。(4)C+的输出使用cout cout是输出流对象,是插入运算符。 若要输出一个字符串,将要输出的字符串写在双引号中;若要输出一个整数,1;若要输出一个变量,a;例如:#includeusing namespace std;int main()int a=2;coutThis is a C+ program.1a;return

3、 0;2、例题1.2#includeusing namespace std;int main()int a,b,sum;cinab;sum=a+b;couta+b=sumab; cin:输入流对象:提取运算符C+中的输入、输出比C更简洁,无需格式控制。输入时用空格或者回车分隔都可以。若想输入一个整数,一个实数,一个字符怎么写?#includeusing namespace std;int main()int a;float b;char c;cinabc;couta=aendl;coutb=bendl;coutc=cendl;return 0;或者#includeusing namespace

4、 std;int main()int a;float b;char c;cinabc;couta=aendlb=bendlc=cendl;return 0;(2)/C+的注释符,若注释内容较少,一行即可,那么可以使用/,若注释内容较多,需要多行,那么使用/* */。(3)endl是回车换行符,与n的作用一样。3、例题1.3#includeusing namespace std;int max(int x, int y)int z;if(xy) z=x;else z=y;return(z);int main()int a,b,m;cinab;m=max(a,b);coutmax=mn;retur

5、n 0;程序功能:给两个数x和y,求两数中的大者。程序解释:(1)与C完全一致。涉及到子函数和主函数。4、例题1.4#includeusing namespace std;class Studentprivate: int num; int score;public: void setdata() cinnum; cinscore; void display() coutnum=numendl; coutscore=scoreendl; ;Student stud1,stud2;int main()stud1.setdata();stud2.setdata();stud1.display();

6、stud2.display();return 0;程序功能:定义一个学生类和两个学生对象,输入并显示这两个学生的学号和成绩。程序解释:(1)class Student 类的定义class是类定义的关键字。 Student是我们自定义的类名。(2)private和public private后定义的内容(包括数据和函数)只允许类的成员函数使用,类外不能使用。例如: int main() couta) a=b;if(ca) a=c;return a;int max4(int a, int b)if(ab) return a; else return b;而C+通过函数重载,可以使用同一个函数名,实

7、现上述六个子函数的功能。 int max(int a, int b, int c); float max(float a, float b, float c); long max(long a, long b, long c); int max(int a, int b); float max(float a, float b); long max(long a, long b);程序代码:#includeusing namespace std;int max(int a, int b, int c)if(ba) a=b;if(ca) a=c;return a;float max(float

8、a, float b, float c)if(ba) a=b;if(ca) a=c;return a;int main()int a,b,c;float d,e,f;cinabc;coutmax(a,b,c);coutdef;coutmax(d,e,f);/coutmax(1.1,1.2,1.3);return 0;注意:语句coutmax(1.1,1.2,1.3); 编译器提示错误。error C2668: max : ambiguous call to overloaded function作业:P16P175、6、7、8、9、10第二讲第8章 类和对象一、面向过程的程序设计方法和面向对象

9、的程序设计方法1、程序功能 对学生基本信息(学号、姓名、性别)进行输入输出。2、面向过程的程序设计方法#includeusing namespace std;struct Studentint num;char name20;char sex;Student get_information()Student stud;int i;coutstud.num;coutstud.name0;while(stud.namei!=#)i+;cinstud.namei;coutstud.sex;return stud;void display(Student stud)int i;coutnum:stud

10、.numendl;coutname:;i=0; while(stud.namei!=#) coutstud.namei; i+; coutendl;coutsex:stud.sexendl;int main()Student stud1;stud1=get_information();display(stud1);return 0;程序运行结果:3、面向对象的程序设计方法#includeusing namespace std;class Studentprivate:int num; char name20; char sex;public: void get_information() in

11、t i; coutnum; coutname0; while(namei!=#) i+; cinnamei; coutsex; void display( ) int i; coutnum:numendl; coutname:; i=0; while(namei!=#) coutnamei; i+; coutendl; coutsex:sexendl; ;int main()Student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:程序解释:(1)类如何定义(包括数据和对数据的操作,数据的操作用函数来实现,它们之

12、间的关系更加紧密。)(2)private和public的作用(3)类与结构体的区别(4)定义对象的方法(类是抽象的,而对象是具体的)(5)面向过程的程序设计中,数据与数据的操作是分离的,而面向对象的程序设计中,封装的思想用类来实现。二、类的成员函数1、类的成员函数与一般函数的区别 它是属于一个类的成员,出现在类体中。 它可以被指定为私有的,也可以被指定为公用的。将需要被外界调用的成员函数指定为public。无需或不能被外界调用的成员函数指定为私有的。例如:#includeusing namespace std;class Studentprivate:int num; char name20;

13、 char sex; void get_name() int i; i=0; cinname0; while(namei!=#) i+; cinnamei; public: void get_information() coutnum; coutname=; get_name(); coutsex; void display( ) int i; coutnum:numendl; coutname:; i=0; while(namei!=#) coutnamei; i+; coutendl; coutsex:sexendl; ;int main()Student stud1;stud1.get_

14、information();stud1.display();return 0;程序运行结果:或者:#includeusing namespace std;class Studentprivate:int num; char name20; char sex; void get_name() int i; i=0; cinname0; while(namei!=#) i+; cinnamei; void display_name() int i; i=0; while(namei!=#) coutnamei; i+; public: void get_information() coutnum;

15、 coutname=; get_name(); coutsex; void display( ) coutnum:numendl; coutname:; display_name(); coutendl; coutsex:sexendl; ;int main()Student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:思考:对于姓名的输入、输出很麻烦,如果有已经设计好的字符串类,可以直接进行输入和输出就非常方便。例如:#include#includeusing namespace std;class Studen

16、tprivate:int num; string name; char sex; public: void get_information() coutnum; coutname; coutsex; void display( ) coutnum:numendl; coutname: nameendl; coutsex:sexendl; ;int main()Student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:注意与未使用string类的区别。2、类外定义成员函数#include#includeusing

17、namespace std;class Studentprivate:int num; string name; char sex;public: void get_information();void display( );void Student:get_information() coutnum; coutname; coutsex; void Student:display( ) coutnum:numendl; coutname:nameendl; coutsex:sexendl; int main()Student stud1;stud1.get_information();stu

18、d1.display();return 0;程序运行结果:3、若增加一个教师类,教师信息包括教工号、姓名、性别,程序需要对教工信息进行输入、输出。#include#includeusing namespace std;class Studentprivate:int num; string name; char sex;public: void get_information();void display( );class Teacherprivate:int num; string name; char sex;public: void get_information();void disp

19、lay( );void Student:get_information() coutnum; coutname; coutsex; void Student:display( ) coutStudent_num:numendl; coutStudent_name:nameendl; coutStudent_sex:sexendl; void Teacher:get_information() coutnum; coutname; coutsex; void Teacher:display( ) coutTeacher_num:numendl; coutTeacher_name:nameendl

20、; coutTeacher_sex:sexendl; int main()Student stud1;Teacher tech1;stud1.get_information();coutendl;tech1.get_information();coutendl;stud1.display();coutendl;tech1.display();return 0;程序运行结果:三、对象成员的引用1、访问对象中的成员可以有3种方法:(1)通过对象名和成员运算符访问对象中的成员访问对象中成员的一般形式为:对象名. 成员名例如:stud1.num=1001;stud1.display( );(2)通过指

21、向对象的指针访问对象中的成员class Time public: int hour; int minute;Time t, *p;p=&t;couthour;(3)通过对象的引用变量访问对象中的成员class Time public: int hour; int minute;Time t1;Time &t2=t1;coutt2.hour;第三讲四、类和对象的简单应用举例1、例题8.1 最简单的例子 程序功能:输入输出时间(包括时、分、秒)。 源代码:#includeusing namespace std;class Timepublic: int hour; int minute; int

22、sec;int main()Time t1;cint1.hour;cint1.minute;cint1.sec;coutt1.hour:t1.minute:t1.secendl;return 0;运行结果:2、例题8.2 输入、输出多个时间。源程序:#includeusing namespace std;class Timepublic: int hour; int minute; int sec;int main()Time t1;cint1.hour;cint1.minute;cint1.sec;coutt1.hour:t1.minute:t1.sect2.hour;cint2.minut

23、e;cint2.sec;coutt2.hour:t2.minute:t2.secendl;return 0;程序运行结果:上述程序中有相同的代码段,因此使用函数对时间进行输入、输出。源程序:#includeusing namespace std;class Timepublic: int hour; int minute; int sec;int main()void set_time(Time &t);void show_time(Time &t);Time t1;set_time(t1);show_time(t1);Time t2;set_time(t2);show_time(t2);re

24、turn 0;void set_time(Time &t)cint.hour;cint.minute;cint.sec;void show_time(Time &t)coutt.hour:t.minute:t.secendl;运行结果:新知识点:有默认参数的函数定义:源程序:#includeusing namespace std;class Timepublic: int hour; int minute; int sec;int main()void set_time(Time &t, int hour=0, int minute=0, int sec=0);void show_time(T

25、ime &t);Time t1;set_time(t1,12,23,34);show_time(t1);Time t2;set_time(t2);show_time(t2);return 0;void set_time(Time &t,int hour, int minute, int sec)t.hour=hour;t.minute=minute;t.sec=sec;void show_time(Time &t)coutt.hour:t.minute:t.secendl;运行结果:3、例题8.3(类的定义和使用)源程序:#includeusing namespace std;class Ti

26、mepublic: void set_time(); void show_time();private: int hour; int minute; int sec;int main()Time t1;t1.set_time();t1.show_time();Time t2;t2.set_time();t2.show_time();return 0;void Time:set_time()cinhour;cinminute;cinsec;void Time:show_time()couthour:minute:secendl;运行结果:4、例题8.4 找出一个整型数组中的元素的最大值。源程序:

27、#includeusing namespace std;class Array_maxpublic: void set_value(); void max_value(); void show_value();private: int array10; int max;void Array_max:set_value()int i;for(i=0;iarrayi;void Array_max:max_value()int i;max=array0;for(i=1;imax) max=arrayi;void Array_max:show_value()coutmax=max;int main()

28、Array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;运行结果:5、输入、输出点的坐标#includeusing namespace std;class pointprivate: float Xcoord, Ycoord;public: void SetX(float x) Xcoord=x; void SetY(float y) Ycoord=y; float GetX() return Xcoord; float GetY() return Ycoord; ;int main

29、()point p1;p1.SetX(1.1);p1.SetY(2.2);coutp1.GetX()endl;coutp1.GetY()endl;return 0; 运行结果:6、设计一个栈。#includeusing namespace std;const int maxsize=6;#define false 0#define true 1class stackprivate: float datamaxsize; int top; public: int set_null_stack(void); int empty(void); void push(float a); float po

30、p(void); void show(); ;int stack:set_null_stack(void)top=-1;coutstack initialized.endl;return 0;int stack:empty(void)if (top0) coutstack is empty.;return true;else coutstack is not empty.;return false;void stack:push(float a)if(top=maxsize-1)coutStack overflow!endl;else datatop+1=a; top+; float stac

31、k:pop(void)if(top=-1)coutAn empty stack!endl;return 0;else top-; coutStack top:;return datatop+1; void stack:show()for(int i=0;i=top;i+)coutdataiendl;int main()stack s1;s1.set_null_stack();for( int i=0; i=maxsize-1; i+) s1.push(2*i);coutendl;s1.show();couts1.pop()endl;return 0; 第四讲 习题讲解第五讲第9章 关于类和对象

32、的进一步讨论9.1 构造函数9.1.1 对象的初始化1、类的数据成员不能在声明类的时候初始化。例如,下面的定义是错误的:class Timehour=0;minute=0;sec=0; 原因:类是一种抽象类型。9.1.2 构造函数的作用1、构造函数:处理对象的初始化2、构造函数: (1)一种特殊的成员函数。 (2)不需要用户调用,在建立对象时自动执行。 (3)名字与类名相同。 (4)不具有任何类型,不返回任何值。3、例题#includeusing namespace std;class Timepublic: Time() hour=0; minute=0; sec=0; void set_t

33、ime(); void show_time();private: int hour; int minute; int sec;void Time:set_time()cinhour;cinminute;cinsec;void Time:show_time()couthour:minute:secendl;int main()Time t1;t1.set_time();t1.show_time();Time t2;t2.show_time();return 0;运行结果:类外定义构造函数:#includeusing namespace std;class Timepublic: Time();

34、void set_time(); void show_time();private: int hour; int minute; int sec;Time:Time() hour=0; minute=0; sec=0; void Time:set_time()cinhour;cinminute;cinsec;void Time:show_time()couthour:minute:secendl;int main()Time t1;t1.set_time();t1.show_time();Time t2;t2.show_time();return 0;4、有关构造函数的使用说明P2659.1.

35、3 带参数的构造函数1、构造函数不带参数,在函数体中对各数据成员赋初值,结果是该类的每个对象都得到同一组初值。 如果对不同的对象能给予不同的初值,怎么做?2、带参数的构造函数 构造函数的首部: 构造函数名(类型1 形参1,类型2 形参2,)3、实参在哪里给出? 定义对象的时候给出。 定义对象的格式: 类名 对象名(实参1,实参2,)4、例如 例题9.2#includeusing namespace std;class Boxpublic: Box(int, int, int); int volume();private: int height; int width; int length;Bo

36、x:Box(int h, int w, int len)height=h;width=w;length=len;int Box:volume()return(height*width*length);int main()Box box1(12,25,30);coutThe volume of box is box1.volume()endl;Box box2(15,30,21);coutThe volume of box is box2.volume()endl;return 0;9.1.4 用参数初始化表对数据成员初始化#includeusing namespace std;class Bo

37、xpublic: Box(int, int, int); int volume();private: int height; int width; int length;Box:Box(int h, int w, int len):height(h),width(w),length(len) int Box:volume()return(height*width*length);int main()Box box1(12,25,30);coutThe volume of box is box1.volume()endl;Box box2(15,30,21);coutThe volume of

38、box is box2.volume()endl;return 0;9.1.5 构造函数的重载#includeusing namespace std;class Boxpublic: Box(); Box(int h, int w, int len):height(h),width(w),length(len) int volume();private: int height; int width; int length;Box:Box()height=10;width=10;length=10;int Box:volume()return(height*width*length);int m

39、ain()Box box1;coutThe volume of box is box1.volume()endl;Box box2(15,30,21);coutThe volume of box is box2.volume()endl;return 0;练习:#includeusing namespace std;class Boxpublic: Box(); Box(int h, int w, int len):height(h),width(w),length(len) Box(int h); Box(int h,int w); int volume();private: int hei

40、ght; int width; int length;Box:Box()height=10;width=10;length=10;Box:Box(int h)height=h;width=10;length=10;Box:Box(int h,int w)height=h;width=w;length=10;int Box:volume()return(height*width*length);int main()Box box1;coutThe volume of box is box1.volume()endl;Box box2(15,30,21);coutThe volume of box is box2.volume()endl;Box box3(10);coutThe volume of box is box3.volume()endl;Box box4(10,10);coutThe volume of box is box4.volume()endl;return 0;9.1.6 使用默认参数的构造函数#includeusing namespace std;class Boxpublic: Box(int h=10, int w=10, in

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!