写在前面
1、基于2022级计算机类实验指导书
2、代码仅提供参考
3、如果代码不满足你的要求,请寻求其他的途径
运行环境
window11家庭版
CLion 2023.2.2
实验要求、源代码和运行结果
1、有以下程序:
#include <iostream>
using namespace std;
class Time{
public:
int hour;
int minute;
int sec;
};
int main(){
Time t1;
cin>>t1.hour;
cin>>t1.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
return 0;
}
请改写程序,要求:
将数据成员改为私有的;
将输入和输出功能改为由函数实现;
在类体内声明成员函数,在类外定义。
然后编译运行。
#include <iostream>
using namespace std;
class Time {
private:
int hour;
int minute;
int sec;
public:
void setTime(int h, int m, int s);
void displayTime();
};
void Time::setTime(int h, int m, int s) {
hour = h;
minute = m;
sec = s;
}
void Time::displayTime() {
cout << hour << ":" << minute << ":" << sec << endl;
}
int main() {
Time t1;
int h, m, s;
cout << "请输入小时:\n";
cin >> h;
cout << "请输入分钟:\n";
cin >> m;
cout << "请输入秒数:\n";
cin >> s;
t1.setTime(h, m, s);
cout << "时间为:\n";
t1.displayTime();
return 0;
}
2、需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长),width(宽),height(高)。要求用成员函数实现以下功能:
由键盘分别输入3个长方体的长、宽、高;
计算长方体的体积;
输出长方体的体积。
编程上机调试并运行。
#include <iostream>
using namespace std;
class Cuboid {
private:
double length;
double width;
double height;
public:
void setDimensions(double l, double w, double h);
double calculateVolume();
void displayVolume();
};
void Cuboid::setDimensions(double l, double w, double h) {
length = l;
width = w;
height = h;
}
double Cuboid::calculateVolume() {
return length * width * height;
}
void Cuboid::displayVolume() {
cout << "长方体的体积为:" << calculateVolume() << endl;
}
int main() {
Cuboid cuboid1, cuboid2, cuboid3;
double l, w, h;
// 输入第一个长方体的长、宽、高
cout << "请输入第一个长方体的长、宽、高:" << endl;
cin >> l >> w >> h;
cuboid1.setDimensions(l, w, h);
// 输入第二个长方体的长、宽、高
cout << "请输入第二个长方体的长、宽、高:" << endl;
cin >> l >> w >> h;
cuboid2.setDimensions(l, w, h);
// 输入第三个长方体的长、宽、高
cout << "请输入第三个长方体的长、宽、高:" << endl;
cin >> l >> w >> h;
cuboid3.setDimensions(l, w, h);
// 输出三个长方体的体积
cout << "三个长方体的体积分别为:" << endl;
cuboid1.displayVolume();
cuboid2.displayVolume();
cuboid3.displayVolume();
return 0;
}
3、将如下代码改成多文件的程序,要求:
将类定义放在头文件student.h中;
将成员函数定义放在源文件student.cpp中;
将主函数的源文件放在main1.cpp中。
在类中增加一个对数据成员赋初值的成员函数set_value.
上机调试并运行。提示代码:
#include <iostream> using namespace std; class Student{ public: void display(); private: int num; char name[20]; char sex; }; void Student::display() { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex"<<sex<<endl; } int main(){ Student stud1; stud1.display(); return 0; }
#ifndef 实验二_3_STUDENT_H
#define 实验二_3_STUDENT_H
class Student {
public:
void display();
void set_value(int n, const char* na, char s); //新增的成员函数
private:
int num;
char name[20];
char sex;
};
#endif //实验二_3_STUDENT_H
#include "3_student.h"
#include <iostream>
#include <cstring>
using namespace std;
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
void Student::set_value(int n, const char* na, char s) {
num = n;
strcpy(name, na);
sex = s;
}
#include "3_student.cpp"
using namespace std;
int main() {
Student stud1;
stud1.set_value(1234, "亚索", '1');
stud1.display();
return 0;
}