第4周项目1-阅读程序(2)
2015-04-06 14:00
267 查看
/* *copyright (c)2015,烟台大学计算机学院 *All rights reserved *文件名称:project.cpp *作者:孙春红 *完成日期:2015年4月5日 *版本号:v1.0 * *问题描述:阅读程序; *输入描述:略。 *程序输出:略。 */ (3) 阅读程序,写出程序的运行结果并理解 #include <iostream> using namespace std; class Time { public: Time(int,int,int); void output_time( ); int hour; //以下全为私有 int minute; int sec; }; Time::Time(int h,int m,int s) { hour=h; minute=m; sec=s; } //类外定义 void Time::output_time( ) { cout<<hour<<":"; cout<<minute<<":" <<sec<<endl; } int main( ) { Time t1(10,13,56); int *p1=&t1.hour; //指向数据成员的指针 cout<<*p1<<endl; //输出小时 t1.output_time( ); Time *p2=&t1; //指向对象的指针 p2->output_time( ); void (Time::*p3)( ); //指向成员函数的指针 p3=&Time::output_time; (t1.*p3)( ); return 0; } 结果: 10 10:13:56 10:13:56 10:13:56 (4) 请写出程序中const出现的语法现象及其所起的作用 [cpp] view plaincopyprint? #include <iostream> #include <string> using namespace std; class Student { public: Student() {} Student( const string& nm, int sc = 0 ): name(nm), score(sc) {} //(1)下面的const干神马?___定义nm为常数据成员__________ void set_student( const string& nm, int sc = 0 ) { name = nm; score = sc; } //(2)下面的const分别干神马?_定义get_name为常成员函数,便于引用常变量nm__________ const string& get_name() const { return name; } int get_score() const { return score; } private: string name; int score; }; //(3)下面的const干神马?__定义常对象,便于引用常数据函数以及数据成员___________ void output_student(const Student& student ) { cout << student.get_name() << "\t"; cout << student.get_score() << endl; } int main() { Student stu( "Wang", 85 ); output_student( stu ); return 0; } 结果: wang 85
学习心得:
通过这些简单的代码,理解了基本的程序运行,再碰到不理解的地方,我会运用单步执行理解
相关文章推荐
- 第4周项目--程序阅读2
- 第4周项目--程序阅读4
- 第4周项目--构造函数、析构函数程序阅读1
- 第4周项目--程序阅读3对象是数组
- 第4周项目1-阅读程序(1)
- 第4周-项目0-阅读程序
- 第4周项目1-阅读程序(3)
- 第10周项目1-程序填充与阅读
- 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- fw: 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- 第十周项目一程序填充与阅读
- 谈C程序员修养及大型项目源码阅读与学习
- 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- 第十六周项目一----阅读程序
- 第十周项目一程序填充与阅读(1)
- 第10周项目1-程序填充与阅读-1
- 第十周项目一 (程序填充与阅读) 1:
- C++第4周(春)项目3 程序的多文件组织
- 第10周项目1 程序的填充与阅读
- 专访许鹏:谈C程序员修养及大型项目源码阅读与学习