c++程序设计第3版谭浩强课后答案
在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value。上机调试并运行。
以下是此题的【c++源代码】
**********************************
//xt8-4.h(student.h)
class Student
{ public:
void display( );
void set_value();
private:
int num;
char name[20];
char sex ;
};
*************************************
//xt8-4-1.cpp(main.cpp)
#include <iostream>
using namespace std;
#include "xt8-4.h"
int main()
{Student stud;
stud.set_value();
stud.display();
return 0;
}
*************************************
//xt8-4-2.cpp(即student.cpp)
#include "xt8-4.h" //在此文件中进行函数的定义
#include <iostream>
using namespace std; //不要漏写此行
void Student::display( )
{ cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
void Student::set_value()
{ cin>>num;
cin>>name;
cin>>sex;
}
本文链接:http://www.wb98.com/cjia/post/cjia_8.4.html
本站文章搜索: