第2周 阅读程序-初识对象(二)
来源:互联网 发布:电视直播软件 知乎 编辑:程序博客网 时间:2024/06/04 00:57
第二个面向对象的程序——同样功能的程序,提倡以下面的形式书写,这是专业人员更愿意使用的方式。
#include <iostream>#include <cstring>using namespace std;class Student{public: void set_data(int n,char *p,char s); void display();private: int num; char name[20]; char sex;};void Student::set_data(int n,char *p,char s){ num=n; strcpy(name,p); sex=s;}void Student::display(){ cout<<"num: "<<num<<'\n'; cout<<"name: "<<name<<'\n'; cout<<"sex: "<<sex<<'\n';}int main(){ Student s1,s2; s1.set_data(1,"He",'f'); s2.set_data(2,"She",'h'); s1.display(); s2.display(); cout<<sizeof(s1); return 0;}
问题:
- 概括这种写法的特点________。
- 将公共成员写在私有成员前;在类外定义公共成员函数。
- 在类定义中,公共成员在前,私有成员在后,有何好处?___
- 方便查看该类所能实现的算法。
- 成员函数的实现写在类定义之外,有何好处?________
- 方便查看算法,查找问题,体现结构框架;便于多人操作,提高了程序的质量。
- 将第5行public: 去掉,记录出现的问题______,原因是_____。加上public,将程序改回正确状态。
- 出现的问题是提示成员为私有不能被调用。原因是类中默认成员为私有。
cpp:14:6: error: 'void Student::set_data(int, char*, char)' is private
cpp:29:30: error: within this context
:29:30: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
cpp:14:6: error: 'void Student::set_data(int, char*, char)' is private
cpp:30:31: error: within this context
cpp:30:31: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
cpp:20:6: error: 'void Student::display()' is private
cpp:31:19: error: within this context
cpp:20:6: error: 'void Student::display()' is private
cpp:32:19: error: within this context
- 将第18行void Student::display( )写作为void display( ),即去掉Student::,结果会是_____?Student::的作用是_____。将程序改回正确状态。
- 结果是提示类的属性没有被定义声明,student::是作用域限定符,作用是调用类内的成员。
cpp: In function 'void display()':
cpp:22:20: error: 'num' was not declared in this scope
cpp:23:22: error: 'name' was not declared in this scope
cpp:24:21: error: 'sex' was not declared in this scope
- 在第30行后加一句:stud1.num=3,记录出现的情况,并解释原因。______
- 提示私有。私有函数不能在类外被调用。
cpp:10:9: error: 'int Student::num' is private
cpp:31:11: error: within this context
- 去掉刚加的那一行,将第31行stud1.display();中的stud1.去掉,记录出现的情况,并解释原因。________
- 提示函数未声明。 因为display是stud1中的成员函数
cpp:31:13: error: 'display' was not declared in this scope
- 在32行后增加cout<<sizeof(stud1)语句,看输出的结果,请做出解释___?
结果在原有的输出上增加了输出了28。
解释:
将stud1的属性--num,name,sex所占的字节长度输出出来了。
28=1*4+20*1+1*1+1*1+1*1+1*1
- 初学者常将类定义后的分号丢掉,试将13行最后的分号去掉,记录出现的提示,并做出解释。_________
- cpp:13:1: error: expected ';' after class definition
- 类的定义本身就是语句,以分号作为结束。
- 你能想到的其他“坏”点子继续折腾吗?折腾出真知!
(3)在阅读上面两个程序的基础上再次深入体会下面的基本概念
声明一个类,可以定义出结构相同的一组对象——类是对象的抽象,对象是类的实例;
类/对象中包括数据成员和成员函数两部分——类/对象实现了对程序设计中要描述的实体的封装处理:将有关的数据和操作代码封装在一个对象中,形成一个基本单位,各个对象(如程序中stud1和stud2)之间相对独立,互不干扰。
信息隐蔽(imformation hiding)——将对象中某些部分(通过private限定符)对外隐蔽,即隐蔽其内部细节,只留下少量接口,以便与外界联系,接收外界的消息。
封装和信息隐蔽的优点:把对象的一部分属性和功能对外界屏蔽,从外界是看不到的,甚至是不可知的,有利于数据安全,防止无关的人了解和修改数据。同时,大大降低了操作对象的复杂程度。
- 第2周 阅读程序-初识对象(二)
- 第2周 阅读程序-初识对象(一)
- 第一周项目 程序阅读——初识对象 1.1
- 第一周项目 程序阅读——初识对象 1.2
- 第2周项目--初见对象程序阅读
- 第2周项目--初见对象程序阅读
- 第15周 程序阅读二
- 第2周阅读程序(2)
- 第13周程序阅读(2)
- 第14周程序阅读(2)
- 第2周阅读程序(1)
- 第13周-程序阅读(2)
- 第11周阅读程序(2)
- 第12周-阅读程序(2)
- 第12周阅读程序(2)
- 第13周-阅读程序(2)
- 第12周阅读程序(2)
- 第14周-阅读程序(2)
- leetcode-75 Sort Colors
- 命题作文:在一棵IPv4地址树中彻底理解IP路由表的各种查找过程
- 黑马程序员-OC语言Block
- PHP + Jquery 实现异步传输验证
- HDU 5186
- 第2周 阅读程序-初识对象(二)
- 【高效算法设计——双向扫描】 UVa 1442 Cave
- hdu 1062 Text Reverse 字符串处理。简单水题
- Rotate Array
- 项目一 用“类”解决旱冰场造价的问题
- LeetCode--Count and Say
- 把书读薄之《面向对象葵花宝典》阅读笔记-面向对象基础
- 微博系统,集成新浪开发,有需要的朋友下载
- auto keyword demo