有对象的程序结构(规范版)
2014-03-16 09:23
316 查看
/* * 程序的版权和版本声明部分 * Copyright (c)2013, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称:a.cpp * 作 者:孔云 * 完成日期:2014年3月16日 * 版 本 号: 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; if(sex=='m') cout<<"sex:"<<"男"<<endl; else if(sex=='f') cout<<"sex: " <<"女"<<endl; else cout<<"性别不存在!"<<endl; } int main() { Student stud1,stud2; stud1.set_data(1,"He",'m'); stud2.set_data(2,"She",'f'); stud1.display(); stud2.display(); return 0; }
实践和思考:
概括这种写法的特点:在类外定义成员函数。
在类定义中,公共成员在前,私有成员在后,好处:若没有private或public对其成员作显示声明,系统将全部成员
默认为private(私有的),外界不可以引用其中的数据成员和成员函数,具有隐蔽性特征。public在前可以对一些
成员显示声明为公共的。
成员函数的实现写在类定义之外,好处:不仅可以减少类体的长度,使类体清晰易阅读,而且可以使类的接口和类
的实现细节分离。
将第5行public: 去掉,记录出现的问题:D:\aew\main.cpp||In function 'int main()':|
D:\aew\main.cpp|14|error: 'void Student::set_data(int, char*, char)' is private|
D:\aew\main.cpp|34|error: within this context|
D:\aew\main.cpp|34|warning: deprecated conversion from string constant to 'char*'|
D:\aew\main.cpp|14|error: 'void Student::set_data(int, char*, char)' is private|
D:\aew\main.cpp|35|error: within this context|
D:\aew\main.cpp|35|warning: deprecated conversion from string constant to 'char*'|
D:\aew\main.cpp|20|error: 'void Student::display()' is private|
D:\aew\main.cpp|36|error: within this context|
D:\aew\main.cpp|20|error: 'void Student::display()' is private|
D:\aew\main.cpp|37|error: within this context|
||=== Build finished: 8 errors, 2 warnings ===|
原因是去掉public,系统将成员函数默认为私有的,而在下面定义成员函数作为公有实现。加上public,将程序改回正确状态。
将第18行void Student::display( )写作为void display( ),即去掉Student::,结果会变成全局函数,不是student类中的
成员函数。Student::的作用是表示student类的中函数,将程序改回正确状态。
在第30行后加一句:stud1.num=3,记录出现的情况:D:\aew\main.cpp|10|error: 'int Student::num' is private|
D:\aew\main.cpp|36|error: within this context|并解释原因:num是私有的,在main函数中作为公有的使用了。
去掉刚加的那一行,将第31行stud1.display();中的stud1.去掉,记录出现的情况:
D:\aew\main.cpp|36|error: 'display' was not declared in this scope|并解释原因:去掉stud1.表明display
()为未声明全局函数,不是student类。
在32行后增加cout<<sizeof(stud1)语句,看输出的结果,请做出解释:输出结果的最后多一个数字28,sizeof()的功能是计算
数据占据的字节数,28说明stud1占据空间字节数为28。
初学者常将类定义后的分号丢掉,试将13行最后的分号去掉,记录出现的提示:
D:\aew\main.cpp|4|error: new types may not be defined in a return type|
D:\aew\main.cpp|14|error: two or more data types in declaration of 'set_data'|,并做出解释:去掉分号,类student的声明将不存在。
心得体会:折腾折腾,脑子清醒多了、、、
相关文章推荐
- 基于分布式对象的网游程序结构设计(1)
- 第三周项目1--有对象的程序结构
- 基于分布式对象的网游程序结构设计(4)-SRP分布式对象的概念
- 例1.1 演示使用结构对象的示例程序。
- (转)在.NET程序运行过程中,什么是堆,什么是栈?什么情况下会在堆(栈)上分配数据?它们有性能上的区别吗?“结构”对象可能分配在堆上吗?什么情况下会发生,有什么需要注意的吗?
- 掌握有对象的程序结构
- 开发宝典:基于分布式对象的网游程序结构设计
- 2013级C++第3周【项目1-掌握有对象的程序结构】
- 掌握对象的程序结构
- 掌握有对象的程序结构
- 开发宝典:基于分布式对象的网游程序结构设计
- 利用类名创建对象来简化程序结构2
- 消除switch/case语句,不破坏代码的封闭性,使程序结构更符合面向对象思想(一)
- JAVA进阶 面向对象程序设计——第5周 设计原则(耦合和聚合,代码结构设计,程序维护拓展发展)
- NCL基于顺序结构的绘图程序和基于对象的绘图程序对比
- 第三周项目3-掌握有对象的程序结构
- 掌握对象的程序结构2
- 第三周项目1-1掌握有对象的结构程序
- 基于分布式对象的网游程序结构设计(3) - 分布式组件模型DCOM/COBRA
- 开发宝典:基于分布式对象的网游程序结构设计