第二周 课后实践 阅读程序2
来源:互联网 发布:经济学的思维方式知乎 编辑:程序博客网 时间:2024/05/16 11:16
/* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:徐洪祥 * 完成日期:2015年 3 月 18日 * 版 本 号:v1.0 * * 问题描述:阅读第二个面向对象的程序,程序结构是所有成员函数都在类内定义,阅读程序,回答相关问题; * 输入描述: * 程序输出: #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<<endl; cout<<"name: " <<name<<endl; cout<<"sex: " <<sex<<endl; } int main() { Student stud1,stud2; stud1.set_data(1,"He",'f'); stud2.set_data(2,"She",'m'); stud1.display(); stud2.display(); return 0; } }问题:•概括这种写法的特点________。•在类定义中,公共成员在前,私有成员在后,有何好处?这样可以使用户将注意力集中在能被外界调用的成员上,使阅读者的思路更清晰一些___•成员函数的实现写在类定义之外,有何好处?_减少了程序设计的工作量_______•将第5行public: 去掉,记录出现的问题_函数无法调用_____,原因是_系统默认更改为private____。加上public,将程序改回正确状态。•将第18行void Student::display( )写作为void display( ),即去掉Student::,结果会是_cpp: In function 'void display()':cpp:22:20: error: 'num' was not declared in this scopecpp:23:22: error: 'name' was not declared in this scopecpp:24:21: error: 'sex' was not declared in this scope____?Student::的作用是Student::中,Student是类名,::是作用域限定符(作用域运算符)用它声明函数是属于那个类的。_____。将程序改回正确状态。•在第30行后加一句:stud1.num=3,记录出现的情况,并解释原因。出现的情况:cpp:10:9: error: 'int Student::num' is privatecpp:31:11: error: within this context原因:num是此类中的私有的数据成员,不能在类外被引用。______•去掉刚加的那一行,将第31行stud1.display();中的stud1.去掉,记录出现的情况,并解释原因。出现的情况:cpp:31:13: error: 'display' was not declared in this scope原因:display()是类Student中的成员函数,而不是类外直接定义的函数,使用它时需要指定对象________•初学者常将类定义后的分号丢掉,试将13行最后的分号去掉,记录出现的提示,并做出解释。提示:cpp:13:1: error: expected ';' after class definition
0 0
- 第二周 课后实践 阅读程序2
- 第二周课后实践-阅读程序
- 第二周课后实践--阅读程序
- 第二周课后实践阅读程序
- 第二周--课后实践:阅读程序
- 第二周 课后实践 程序阅读
- 第二周 课后实践:阅读程序(2)
- 第二周课后实践:阅读程序(2)
- 第二周--课后实践:阅读程序(2)
- 第二周 课后实践 程序阅读(2)
- 第二周 课后实践:阅读程序(1)
- 第二周课后实践:阅读程序(1)
- 第二周--课后实践:阅读程序(1)
- 第二周项目1-课后实践:阅读程序
- 第十三周 课后实践:阅读程序2
- 第十五周 课后实践:阅读程序2
- 第2周项目1课后实践,阅读程序
- 第五周 课后实践--阅读程序(2)
- 长方柱类
- xml解析的技术(dom,sax)
- 使用c3p0连接池checkoutTimeout
- Workspace at 'E:/android_worksapce' in use or cannot be created, choose a different one
- HashMap实现原理以及源码阅读
- 第二周 课后实践 阅读程序2
- 第二学期第二周项目一
- ASP.NET获取不到Kindeditor的内容解决方法
- 第二周项目三时间.1
- 第二周 项目一 旱冰场造价
- Python 中的dir()函数 以及简单的数据结构
- 第二周项目一 三角形1
- 让Team Foundation Server/TFS自动记住用户名密码解决方案
- Google语法(怎样使用Google)