




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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#include<iostream>using namespace std;int main()cout<<"this is a
2、 c+ program."return 0;程序功能:输出一行字符:this is a c+ program.程序注释:(1)预处理命令#include<iostream> iostream输入、输出流(2)using namespace std; 使用命名空间std。 第一行和第二行是每个c+都有的语句。(3)c+的主函数名与c一样,都是main。(4)c+的输出使用cout<< cout是输出流对象,<<是插入运算符。 若要输出一个字符串,将要输出的字符串写在双引号中;若要输出一个整数,<<1;若要输出一个变量,<<a;
3、例如:#include<iostream>using namespace std;int main()int a=2;cout<<"this is a c+ program."<<1<<a;return 0;2、例题1.2#include<iostream>using namespace std;int main()int a,b,sum;cin>>a>>b;sum=a+b;cout<<"a+b="<<sum<<endl;return
4、0;程序功能:求a和b两个数之和。程序注释:(1)输入语句:cin>>a>>b; cin:输入流对象>>:提取运算符c+中的输入、输出比c更简洁,无需格式控制。输入时用空格或者回车分隔都可以。若想输入一个整数,一个实数,一个字符怎么写?#include<iostream>using namespace std;int main()int a;float b;char c;cin>>a>>b>>c;cout<<"a="<<a<<endl;cout<&l
5、t;"b="<<b<<endl;cout<<"c="<<c<<endl;return 0;或者#include<iostream>using namespace std;int main()int a;float b;char c;cin>>a>>b>>c;cout<<"a="<<a<<endl<<"b="<<b<<endl<<
6、;"c="<<c<<endl;return 0;(2)/c+的注释符,若注释内容较少,一行即可,那么可以使用/,若注释内容较多,需要多行,那么使用/* */。(3)endl是回车换行符,与n的作用一样。3、例题1.3#include<iostream>using namespace std;int max(int x, int y)int z;if(x>y) z=x;else z=y;return(z);int main()int a,b,m;cin>>a>>b;m=max(a,b);cout<<
7、"max="<<m<<'n'return 0;程序功能:给两个数x和y,求两数中的大者。程序解释:(1)与c完全一致。涉及到子函数和主函数。4、例题1.4#include<iostream>using namespace std;class studentprivate: int num; int score;public: void setdata() cin>>num; cin>>score; void display() cout<<"num="<<
8、num<<endl; cout<<"score="<<score<<endl; ;student stud1,stud2;int main()stud1.setdata();stud2.setdata();stud1.display();stud2.display();return 0;程序功能:定义一个学生类和两个学生对象,输入并显示这两个学生的学号和成绩。程序解释:(1)class student 类的定义class是类定义的关键字。 student是我们自定义的类名。(2)private和public private后定
9、义的内容(包括数据和函数)只允许类的成员函数使用,类外不能使用。例如: int main() cout<<stud1.num; stud1.setdata(); stud2.setdata(); stud1.display(); stud2.display(); return 0;错误提示:error c2248:num: cannot access private member declared in class student(3)类中包含了数据和函数 与结构体的最大区别。(4)student stud1,stud2; 定义两个学生对象 类是抽象的,而对象是具体的。(5)stu
10、d1.setdata(); stud2.setdata(); stud1.display(); stud2.display();调用学生类的成员函数,对两个学生对象赋值并显示。(6)思考:如何定义新学生对象stud3,学号1003,分数80。四、c+对c的扩充1、函数的重载(1)重载是什么意思?(2)为什么c+要增加重载?(3)重载分为函数重载:在同一作用域中用同一函数名定义多个函数,这些函数的参数个数和参数类型不相同。运算符重载:第十章介绍(4)实例:求两个/三个数中最大的数(数值类型可以为整型、实型、长整型)。用c实现必须定义6个子函数。® int max1(int a, int
11、 b, int c);® float max2(float a, float b, float c);® long max3(long a, long b, long c);® int max4(int a, int b);® float max5(float a, float b);® long max6(long a, long b);int max1(int a, int b, int c)if(b>a) a=b;if(c>a) a=c;return a;int max4(int a, int b)if(a>b) ret
12、urn a; else return b;而c+通过函数重载,可以使用同一个函数名,实现上述六个子函数的功能。® 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);程序代码:#include<iostream>
13、using namespace std;int max(int a, int b, int c)if(b>a) a=b;if(c>a) a=c;return a;float max(float a, float b, float c)if(b>a) a=b;if(c>a) a=c;return a;int main()int a,b,c;float d,e,f;cin>>a>>b>>c;cout<<max(a,b,c);cout<<endl;cin>>d>>e>>f;cout
14、<<max(d,e,f);/cout<<max(1.1,1.2,1.3);return 0;注意:语句cout<<max(1.1,1.2,1.3); 编译器提示错误。error c2668: 'max' : ambiguous call to overloaded function作业:p16p175、6、7、8、9、10第二讲第8章 类和对象一、面向过程的程序设计方法和面向对象的程序设计方法1、程序功能 对学生基本信息(学号、姓名、性别)进行输入输出。2、面向过程的程序设计方法#include<iostream>using na
15、mespace std;struct studentint num;char name20;char sex;student get_information()student stud;int i;cout<<"num="cin>>stud.num;cout<<"name="i=0;cin>>0;while(i!='#')i+;cin>>i;cout<<"sex="cin>>stud
16、.sex;return stud;void display(student stud)int i;cout<<"num:"<<stud.num<<endl;cout<<"name:"i=0; while(i!='#') cout<<i; i+; cout<<endl;cout<<"sex:"<<stud.sex<<endl;int main()student stud1;stu
17、d1=get_information();display(stud1);return 0;程序运行结果:3、面向对象的程序设计方法#include<iostream>using namespace std;class studentprivate:int num; char name20; char sex;public: void get_information() int i; cout<<"num=" cin>>num; cout<<"name=" i=0; cin>>name0; whi
18、le(namei!='#') i+; cin>>namei; cout<<"sex=" cin>>sex; void display( ) int i; cout<<"num:"<<num<<endl; cout<<"name:" i=0; while(namei!='#') cout<<namei; i+; cout<<endl; cout<<"sex:"<
19、<sex<<endl; ;int main()student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:程序解释:(1)类如何定义(包括数据和对数据的操作,数据的操作用函数来实现,它们之间的关系更加紧密。)(2)private和public的作用(3)类与结构体的区别(4)定义对象的方法(类是抽象的,而对象是具体的)(5)面向过程的程序设计中,数据与数据的操作是分离的,而面向对象的程序设计中,封装的思想用类来实现。二、类的成员函数1、类的成员函数与一般函数的区别 它是属于一个类的成员,出现在类体
20、中。 它可以被指定为私有的,也可以被指定为公用的。将需要被外界调用的成员函数指定为public。无需或不能被外界调用的成员函数指定为私有的。例如:#include<iostream>using namespace std;class studentprivate:int num; char name20; char sex; void get_name() int i; i=0; cin>>name0; while(namei!='#') i+; cin>>namei; public: void get_information() cout&
21、lt;<"num=" cin>>num; cout<<"name=" get_name(); cout<<"sex=" cin>>sex; void display( ) int i; cout<<"num:"<<num<<endl; cout<<"name:" i=0; while(namei!='#') cout<<namei; i+; cout<<e
22、ndl; cout<<"sex:"<<sex<<endl; ;int main()student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:或者:#include<iostream>using namespace std;class studentprivate:int num; char name20; char sex; void get_name() int i; i=0; cin>>name0; while(namei!=
23、39;#') i+; cin>>namei; void display_name() int i; i=0; while(namei!='#') cout<<namei; i+; public: void get_information() cout<<"num=" cin>>num; cout<<"name=" get_name(); cout<<"sex=" cin>>sex; void display( ) cout<
24、;<"num:"<<num<<endl; cout<<"name:" display_name(); cout<<endl; cout<<"sex:"<<sex<<endl; ;int main()student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:思考:对于姓名的输入、输出很麻烦,如果有已经设计好的字符串类,可以直接进行输入和输出就非常方便。例如:#incl
25、ude<iostream>#include<string>using namespace std;class studentprivate:int num; string name; char sex; public: void get_information() cout<<"num=" cin>>num; cout<<"name=" cin>>name; cout<<"sex=" cin>>sex; void display( ) co
26、ut<<"num:"<<num<<endl; cout<<"name:" <<name<<endl; cout<<"sex:"<<sex<<endl; ;int main()student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:注意与未使用string类的区别。2、类外定义成员函数#include<iostream>#include
27、<string>using namespace std;class studentprivate:int num; string name; char sex;public: void get_information();void display( );void student:get_information() cout<<"num=" cin>>num; cout<<"name=" cin>>name; cout<<"sex=" cin>>sex;
28、 void student:display( ) cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; int main()student stud1;stud1.get_information();stud1.display();return 0;程序运行结果:3、若增加一个教师类,教师信息包括教工号、姓名、性别,程序需要对教工
29、信息进行输入、输出。#include<iostream>#include<string>using 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 display( );void student:get_informati
30、on() cout<<"student_num=" cin>>num; cout<<"student_name=" cin>>name; cout<<"student_sex=" cin>>sex; void student:display( ) cout<<"student_num:"<<num<<endl; cout<<"student_name:"<<name
31、<<endl; cout<<"student_sex:"<<sex<<endl; void teacher:get_information() cout<<"teacher_num=" cin>>num; cout<<"teacher_name=" cin>>name; cout<<"teacher_sex=" cin>>sex; void teacher:display( ) cout<&
32、lt;"teacher_num:"<<num<<endl; cout<<"teacher_name:"<<name<<endl; cout<<"teacher_sex:"<<sex<<endl; int main()student stud1;teacher tech1;stud1.get_information();cout<<endl;tech1.get_information();cout<<endl;stud
33、1.display();cout<<endl;tech1.display();return 0;程序运行结果:三、对象成员的引用1、访问对象中的成员可以有3种方法:(1)通过对象名和成员运算符访问对象中的成员访问对象中成员的一般形式为:对象名. 成员名例如:stud1.num=1001;stud1.display( );(2)通过指向对象的指针访问对象中的成员class time public: int hour; int minute;time t, *p;p=&t;cout<<p->hour;(3)通过对象的引用变量访问对象中的成员class time
34、 public: int hour; int minute;time t1;time &t2=t1;cout<<t2.hour;第三讲四、类和对象的简单应用举例1、例题8.1 最简单的例子 程序功能:输入输出时间(包括时、分、秒)。 源代码:#include<iostream>using namespace std;class timepublic: int hour; int minute; int sec;int main()time t1;cin>>t1.hour;cin>>t1.minute;cin>>t1.sec;c
35、out<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;return 0;运行结果:2、例题8.2 输入、输出多个时间。源程序:#include<iostream>using namespace std;class timepublic: int hour; int minute; int sec;int main()time t1;cin>>t1.hour;cin>>t1.minute;cin>>
36、;t1.sec;cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;time t2;cin>>t2.hour;cin>>t2.minute;cin>>t2.sec;cout<<t2.hour<<":"<<t2.minute<<":"<<t2.sec<<endl;return 0;程序运行结果:
37、上述程序中有相同的代码段,因此使用函数对时间进行输入、输出。源程序:#include<iostream>using 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);return 0;void set_time(time &t)cin&
38、gt;>t.hour;cin>>t.minute;cin>>t.sec;void show_time(time &t)cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;运行结果:新知识点:有默认参数的函数定义:源程序:#include<iostream>using namespace std;class timepublic: int hour; int minute; int sec;int
39、 main()void set_time(time &t, int hour=0, int minute=0, int sec=0);void show_time(time &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
40、 &t)cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;运行结果:3、例题8.3(类的定义和使用)源程序:#include<iostream>using namespace std;class timepublic: void set_time(); void show_time();private: int hour; int minute; int sec;int main()time t1;t1.set_time(
41、);t1.show_time();time t2;t2.set_time();t2.show_time();return 0;void time:set_time()cin>>hour;cin>>minute;cin>>sec;void time:show_time()cout<<hour<<":"<<minute<<":"<<sec<<endl;运行结果:4、例题8.4 找出一个整型数组中的元素的最大值。源程序:#include<iostr
42、eam>using 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;i<10;i+)cin>>arrayi;void array_max:max_value()int i;max=array0;for(i=1;i<10;i+)if(arrayi>max) max=arrayi;void a
43、rray_max:show_value()cout<<"max="<<max;int main()array_max arrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return 0;运行结果:5、输入、输出点的坐标#include<iostream>using namespace std;class pointprivate: float xcoord, ycoord;public: void setx(float x) xcoord=x; void se
44、ty(float y) ycoord=y; float getx() return xcoord; float gety() return ycoord; ;int main()point p1;p1.setx(1.1);p1.sety(2.2);cout<<p1.getx()<<endl;cout<<p1.gety()<<endl;return 0; 运行结果:6、设计一个栈。#include<iostream>using namespace std;const int maxsize=6;#define false 0#defin
45、e true 1class stackprivate: float datamaxsize; int top; public: int set_null_stack(void); int empty(void); void push(float a); float pop(void); void show(); ;int stack:set_null_stack(void)top=-1;cout<<"stack initialized."<<endl;return 0;int stack:empty(void)if (top<0) cout&l
46、t;<"stack is empty."return true;else cout<<"stack is not empty."return false;void stack:push(float a)if(top=maxsize-1)cout<<"stack overflow!"<<endl;else datatop+1=a; top+; float stack:pop(void)if(top=-1)cout<<"an empty stack!"<<
47、;endl;return 0;else top-; cout<<"stack top:"return datatop+1; void stack:show()for(int i=0;i<=top;i+)cout<<datai<<endl;int main()stack s1;s1.set_null_stack();for( int i=0; i<=maxsize-1; i+) s1.push(2*i);cout<<endl;s1.show();cout<<s1.pop()<<endl;ret
48、urn 0; 第四讲 习题讲解第五讲第9章 关于类和对象的进一步讨论9.1 构造函数9.1.1 对象的初始化1、类的数据成员不能在声明类的时候初始化。例如,下面的定义是错误的:class timehour=0;minute=0;sec=0; 原因:类是一种抽象类型。9.1.2 构造函数的作用1、构造函数:处理对象的初始化2、构造函数: (1)一种特殊的成员函数。 (2)不需要用户调用,在建立对象时自动执行。 (3)名字与类名相同。 (4)不具有任何类型,不返回任何值。3、例题#include<iostream>using namespace std;class timepublic
49、: time() hour=0; minute=0; sec=0; void set_time(); void show_time();private: int hour; int minute; int sec;void time:set_time()cin>>hour;cin>>minute;cin>>sec;void time:show_time()cout<<hour<<":"<<minute<<":"<<sec<<endl;int mai
50、n()time t1;t1.set_time();t1.show_time();time t2;t2.show_time();return 0;运行结果:类外定义构造函数:#include<iostream>using namespace std;class timepublic: time(); 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()cin>>hou
51、r;cin>>minute;cin>>sec;void time:show_time()cout<<hour<<":"<<minute<<":"<<sec<<endl;int main()time t1;t1.set_time();t1.show_time();time t2;t2.show_time();return 0;4、有关构造函数的使用说明p2659.1.3 带参数的构造函数1、构造函数不带参数,在函数体中对各数据成员赋初值,结果是该类的每个对象都得
52、到同一组初值。 如果对不同的对象能给予不同的初值,怎么做?2、带参数的构造函数 构造函数的首部: 构造函数名(类型1 形参1,类型2 形参2,)3、实参在哪里给出? 定义对象的时候给出。 定义对象的格式: 类名 对象名(实参1,实参2,)4、例如 例题9.2#include<iostream>using namespace std;class boxpublic: box(int, int, int); int volume();private: int height; int width; int length;box:box(int h, int w, int len)heig
53、ht=h;width=w;length=len;int box:volume()return(height*width*length);int main()box box1(12,25,30);cout<<"the volume of box is "<<box1.volume()<<endl;box box2(15,30,21);cout<<"the volume of box is "<<box2.volume()<<endl;return 0;9.1.4 用参数初始化表对数据成员初始化#include<iostream>using namespac
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 四级人力资源管理师-2018年11月四级人力资源管理师考试《理论知识》真题
- 上海市嘉定区封浜高中2017-2018学年第二学期高二化学期中试卷
- 2024-2025学年教案语文(必修上册)131《读书目的和前提》
- 山西省晋中市高三1月高考适应性调研考试理综生物试题
- 河南省正阳县第二高级中学高三下学期理科数学周练(二)
- 高三化学总复习练习第三章金属及其化合物3-8
- 2018年高考物理一轮训练(11)及详细解析
- 基于DBR的再制造时间缓冲控制方法研究
- 基于ANSYS软件的新型户外可自主拼装拼花地板研发
- 锌指蛋白A20在Behcet病和Vogt-小柳原田综合征发病机制中作用的研究
- 女神节花艺沙龙活动
- 大剧院音视频系统工程调试方案
- 社区商业招商与运营管理方案
- 人教PEP版(2024)三年级上册英语Unit 6《Useful numbers》单元作业设计
- 魔发奇缘电影中英文对白
- 浙江省宁波市九校2023-2024学年高二下学期期末联考数学试题2
- 事业单位公开招聘分类考试公共科目笔试考试大纲2022年版
- 8 歌曲 《邮递员叔叔来了》课件(13张内嵌视频)
- 网络数据安全风险治理与防护项目需求说明
- GB/T 14020-2024氢化松香
- 中医护理学 课件 模块七 中医护理操作 项目四麦粒灸技术
评论
0/150
提交评论