1.有以下程序:

请改写程序,要求:
将数据成员改为私有的;
将输入和输出功能改为由函数实现;
在类体内声明成员函数,在类外定义。
然后编译运行
#include<iostream>
using namespace std;
class Time {
private:
int hour;
int minute;
int sec;
public:
void get(int a,int b,int c);
void T_show();
};
void Time::get(int a,int b,int c)
{
hour = a;
minute = b;
sec = c;
}
void Time::T_show()
{
cout << hour << ":" << minute << ":" << sec << endl;
}
int main()
{
Time t1;
int a, b, c;
cin >> a >> b >> c;
t1.get(a, b, c);
t1.T_show();
return 0;
}

2、需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长),width(宽),height(高)。要求用成员函数实现以下功能:
由键盘分别输入3个长方体的长、宽、高;
计算长方体的体积;
输出长方体的体积。
编程上机调试并运行。
#include<iostream>
using namespace std;
class Area {
private:
double length;
double width;
double height;
public:
void get_leh(int l, int w, int h);
void A_show();
};
void Area::get_leh(int l, int w, int h)
{
length = l;
width = w;
height = h;
}
void Area::A_show()
{
double volume;
volume = length * width * height;
cout << "volume=" << volume << endl;
}
int main()
{
Area a;
double l, w, h;
cin >> l >> w >> h;
a.get_leh(l, w, h);
a.A_show();
return 0;
}
3、将如下代码改成多文件的程序,要求:
将类定义放在头文件student.h中;
将成员函数定义放在源文件student.cpp中;
将主函数的源文件放在main1.cpp中。
在类中增加一个对数据成员赋初值的成员函数set_value.
上机调试并运行。提示代码:

student.h头文件:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include<string>
using namespace std;
class Student {
public:
void display();
void set_value(int n, const string& nm, char s);
private:
int num;
string name;
char sex;
};
#endif // STUDENT_H
(2)student.cpp源文件:
#include "student.h"
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
void Student::set_value(int n, const string& nm, char s) {
num = n;
name = nm;
sex = s;
}
(3)main1.cpp源文件:
#include <iostream>
#include"student.h"
using namespace std;
int main() {
Student stud1;
stud1.set_value(1, "hahaha", 'l');
stud1.display();
return 0;
}
4352





