您的位置:首页 > 其它

第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


学习心得:

通过这些简单的代码,理解了基本的程序运行,再碰到不理解的地方,我会运用单步执行理解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: