2016.5.27第5章上机内容概要

上传人:今*** 文档编号:108057853 上传时间:2019-10-22 格式:PPT 页数:303 大小:1.30MB
返回 下载 相关 举报
2016.5.27第5章上机内容概要_第1页
第1页 / 共303页
2016.5.27第5章上机内容概要_第2页
第2页 / 共303页
2016.5.27第5章上机内容概要_第3页
第3页 / 共303页
2016.5.27第5章上机内容概要_第4页
第4页 / 共303页
2016.5.27第5章上机内容概要_第5页
第5页 / 共303页
点击查看更多>>
资源描述

《2016.5.27第5章上机内容概要》由会员分享,可在线阅读,更多相关《2016.5.27第5章上机内容概要(303页珍藏版)》请在金锄头文库上搜索。

1、2016.5.27,上机内容,3,编程题(1) 定义一个类Score, 它含有私有数据成员engscore(英语分数), 公有成员函数setscore() 和 printscore(); 其中setscore() 用来设置engscore的值。 在主程序中声明类Score的两个对象stu1和stu2,其英语成绩分别为85.5 和75.8 , 输出这两个分数。,编程题(1) 要求每个同学都能上机完成,下面2道思考题不要求同学现在能做。 可以带着问题去预习第6章。,6,思考题 预习了第6章数组和指针的同学可以思考一下,思考题1:改写例题6_2 . 函数求出每行数据的和,但是又不破坏数组里的内容,怎

2、么做?,8,思考题 预习了第6章数组和指针的同学可以思考一下,思考题2:能够推测出下面程序的结果并说明理由吗? #include using namespace std; void main() int a34 = 1,3,5,7,9,11,13,15,17,19,21,23; cout *(*(a+1)endl; cout*(*a+2)endl; cout*(*(a+2)+2)endl; ,下面为阅读复习内容。,11,5.3 类的静态成员,静态数据成员 用关键字static声明 该类的所有对象维护该成员的同一个拷贝 必须在类外定义和初始化,用(:)来指明所属的类。 静态成员函数 类外代码可以

3、使用类名和作用域操作符来调用静态成员函数。 静态成员函数只能引用属于该类的静态数据成员或静态成员函数。,12,例5-4 具有静态数据成员的 Point类,13,#include using namespace std; class Point public: Point(int x=0, int y=0):x(x),y(y) count+; Point(Point ,14,int Point:count =0; void main() Point a(4,5); cout“Point a:“a.getX()“,“a.getY(); a.showCount() ; Point b(a); cou

4、t“Point b:“b.getX()“,“b.getY(); b.showCount() ; ,15,16,17,修改上面的程序,会不会出错? 把 int Point:count =0; 改为 int Point:count ; /会不会出错,18,#include using namespace std; class Point public: Point(int x=0, int y=0):x(x),y(y) count+; Point(Point ,19,int Point:count ; /会不会出错 void main() Point a(4,5); cout“Point a:“a

5、.getX()“,“a.getY(); a.showCount() ; Point b(a); cout“Point b:“b.getX()“,“b.getY(); b.showCount() ; ,20,21,22,23,24,25,5.3 类的静态成员,26,静态成员,静态数据成员 用关键字static声明 该类的所有对象维护该成员的同一个拷贝 必须在类外定义和初始化,用(:)来指明所属的类。,27,int Point:count =0; 注意:静态数据成员的初始化在类外进行。,28,修改main()程序,写出结果,void main() Point a(4,5); cout“Point

6、a:“a.getX()“,“a.getY(); a.showCount() ; Point b(a); cout“Point b:“b.getX()“,“b.getY(); b.showCount() ; a.showCount() ; ,29,30,31,问题: 在对象创建之前 想输出的初值? 怎么办? 下面的程序行不行?,32,#include using namespace std; class Point public: Point(int x=0, int y=0):x(x),y(y) count+; Point(Point ,33,aaf1.cpp(34) : error C224

7、8: count : cannot access private member declared in class Point,34,35,5.3.2 静态函数成员,函数showCount() 是专门用来输出静态成员count的 void showCount() cout“ Object count=“countendl;,36,37,#include using namespace std; class Point public: Point(int x=0, int y=0):x(x),y(y) count+; Point(Point ,38,int Point:count =0; voi

8、d main() Point:showCount();/ Point a(4,5); cout“Point a:“a.getX()“,“a.getY(); Point:showCount(); Point b(a); cout“Point b:“b.getX()“,“b.getY(); Point:showCount(); ,39,40,静态成员函数 也可以通过对象调用。,41,#include using namespace std; class Point public: Point(int x=0, int y=0):x(x),y(y) count+; Point(Point ,42,i

9、nt Point:count =0; void main() Point:showCount(); Point a(4,5); cout“Point a:“a.getX()“,“a.getY(); Point:showCount(); a.showCount() ; Point b(a); cout“Point b:“b.getX()“,“b.getY(); Point:showCount(); b.showCount() ; ,43,44,45,读程序,写结果。,46,#include using namespace std; class Cat public: Cat(int w = 0)

10、 :w(w) +count; int getCount() return count; int getWeight() return w; private: int w; static int count; ; int Cat:count=0 ; / 初始化静态数据成员 void main() Cat c1(5),c2(6),c3(7); cout c1.getWeight()“ “; cout c1.getCount()endl; cout c2.getWeight()“ “; cout c2.getCount()endl; cout c3.getWeight()“ “; cout c3.g

11、etCount()endl; ,47,48,#include using namespace std; class Cat public: Cat(int w = 0) :w(w) +count; int getCount() return count; int getWeight() return w; private: int w; static int count; ; int Cat:count=0 ; / 初始化静态数据成员 void main() Cat c1(5) ; cout c1.getWeight()“ “; cout c1.getCount()endl; Cat c2(6

12、); cout c2.getWeight()“ “; cout c2.getCount()endl; Cat c3(7); cout c3.getWeight()“ “; cout c3.getCount()endl; ,49,50,51,5.4 类的友元,下面的程序是否出错?,52,#include #include using namespace std; class Point /Point类声明 public: /外部接口 Point(int x=0, int y=0):x(x),y(y) int getX() return x; int getY() return y; privat

13、e: /私有数据成员 int x, y; ; void show( Point ,53,lit.cpp(16) : error C2248: x : cannot access private member declared in class Point,54,想实现上述程序的功能应该如何设计?,#include #include using namespace std; class Point /Point类声明 public: /外部接口 Point(int x=0, int y=0):x(x),y(y) int getX() return x; int getY() return y;

14、void show( ) ; private: /私有数据成员 int x,y; ; void Point:show( ) cout x“ “ yendl; int main() Point myp1(4, 5) ; myp1.show(); return 0; ,56,但是,如果我们一定要在外面的函数中访问对象的私有成员,可以怎么做呢?,57,#include #include using namespace std; class Point /Point类声明 public: /外部接口 Point(int x=0, int y=0):x(x),y(y) int getX() return

15、 x; int getY() return y; private: /私有数据成员 int x,y; ; void show( Point ,58,59,修改方法2: 把类的成员都 声明成为public,60,#include #include using namespace std; class Point /Point类声明 public: /外部接口 Point(int x=0, int y=0):x(x),y(y) int getX() return x; int getY() return y; public: int x,y; ; void show( Point ,61,把类的成员都 声明成为public ,然而这样做带来的问题 是任何外部函数都可以毫无约束的访问它操作它, 降低了 数据成员的安全性。 c+利用friend修饰符,可以让一些你设定的函数能够对这些私有数据进行操作,避免把类成员全部设置成public,最大限度的保护数据成员的安全。,62,63,#include #include using namespace std; class Point /Point类声明

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

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

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