目录
分享原因:
本人就在继续走C++的路上,感觉c++的类和对象对新手来讲比较抽象,所以特此分享给大家一些正在做的作业实验题目,希望能对刚开始学习c++的小伙伴有点帮助吧
实验内容:
二.实验内容
1、请检查下面程序,找出其中的错误(先不要上机,自己先检查),并改正之。然后上机调试,使之能正常运行,运行时从键盘输入时、分、秒的值,检查输出是否正确。
#include <iostream>
using namespace std;
class Time
{
int hour;
int minute;
int sec;
void set_time();
void show_time();
};
Time t;
int main( )
{
set_time();
show_time();
return 0;
}
void set_time()
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void show_time()
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
2、分别给出如下的3个文件:
(1)、含类定义的头文件student.h,
class Student //类声明
{ public:
void display( ); //公用成员函数原型声明
private:
int num;
char name[20];
char sex;
};
(2)、包含成员函数定义的源文件student.cpp,
#include <iostream>
using namespace std;
#include "student.h" //不要漏写此行,否则编译通不过
void Student::display( ) //在类外定义display类函数
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
(3)、包含主函数的源文件main.cpp,
为了组成一个完整的源程序,应当有包含主函数的源文件:
#include "student.h" //将类声明头文件包含进来
int main( )
{Student stud; //定义对象
stud.display( ); //执行stud对象的display函数
return 0;
}
请完善该程序,在类中增加一个对数据成员赋予初值的成员函数 set_value。上机调试并运行。
3. 将本章中的例2.4 改写为一个多文件的程序:(代码放下面)
#include <iostream>
using namespace std;
class Array_max //声明类
{
public: //以下3行为成员函数原型声明
void set_value();//对数组元素设置值
void max_value();//找出数组中的最大元素
void show_value();//输出最大值
private:
int array[10];//整型数组
int max;//max用来存放最大值的
};
void Array_max::set_value()//成员函数定义,向数组元素输人数值有
{
int i;
for (i = 0;i < 10;i++)
cin >> array[i];
}
void Array_max::max_value()//成员函数定义,找数组元素中的最大值
{
int i;
max = array[0];
for (i = 1; i < 10; i++)
if (array[i] > max)
max = array[i];
}
void Array_max::show_value()//成员函数定义,输出最大值
{cout<<"max="<<max;}
int main()
{
Array_max arrmax;//定义对象
arrmax.set_value();//调用arrmax的set value函数,向数组元素输入数值
arrmax.max_value();//调用arrmax的max_value函数,找出数组元素中的最大值
arrmax.show_value();//调用arrmax的show_value函数,输出数组元素中的最大值
return 0;
}
(1)将类定义放在头文件arraymax.h中;
(2)将成员函数定义放在源文件arraymax.cpp中;
(3)主函数放在源文件file1.cpp中。
请写出完整的程序,上机调试并运行。
4、需要求三个长方柱的体积,请编写一个基于对象的程序,数据成员包括length(长)、width(宽)、height(高)。
要求用成员函数实现以下功能:
(1)、由键盘输入三个长方柱的长、宽、高;
(2)、计算三个长方柱的体积;
(3)、输出三个长方柱的体积;
解答:
1.答案:
#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int sec;
public:
void set_time();
void show_time();
};
Time t;
int main()
{
t.set_time();
t.show_time();
return 0;
}
void Time::set_time()
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void Time::show_time()
{
cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
2.答案
2.1 student.h
#include<iostream>
using namespace std;
class Student //类声明
{
public:
void display(); //公用成员函数原型声明
void set_value();
private:
int num;
char name[20];
char sex[1];
};
2.2 student.cpp
//(2)、包含成员函数定义的源文件student.cpp,
#include <iostream>
using namespace std;
#include "student.h" //不要漏写此行,否则编译通不过
void Student::display() //在类外定义display类函数
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
void Student::set_value()
{
cin >> num;
cin >> name;
cin >> sex;
}
2.3 main.cpp
//(3)、包含主函数的源文件main.cpp,
//为了组成一个完整的源程序,应当有包含主函数的源文件:
#include "student.h" //将类声明头文件包含进来
int main()
{
Student stud; //定义对象
stud.set_value();
stud.display(); //执行stud对象的display函数
return 0;
}
3.答案
3.1 arraymax.h
#include <iostream>
using namespace std;
class Array_max //声明类
{
public: //以下3行为成员函数原型声明
void set_value();//对数组元素设置值
void max_value();//找出数组中的最大元素
void show_value();//输出最大值
private:
int array[10];//整型数组
int max;//max用来存放最大值的
};
3.2 arraymax.cpp
#include"arraymax.h"
void Array_max::set_value()//成员函数定义,向数组元素输人数值有
{
int i;
for (i = 0; i < 10; i++)
cin >> array[i];
}
void Array_max::max_value()//成员函数定义,找数组元素中的最大值
{
int i;
max = array[0];
for (i = 1; i < 10; i++)
if (array[i] > max)
max = array[i];
}
void Array_max::show_value()//成员函数定义,输出最大值
{
cout << "max=" << max;
}
3.3 file.cpp
#include<iostream>
#include"arraymax.h"
int main()
{
Array_max arrmax;//定义对象
arrmax.set_value();//调用arrmax的set value函数,向数组元素输入数值
arrmax.max_value();//调用arrmax的max_value函数,找出数组元素中的最大值
arrmax.show_value();//调用arrmax的show_value函数,输出数组元素中的最大值
return 0;
}
4.答案
#include<iostream>
using namespace std;
class V
{
public:
void set_value() {
cout << "请分别输入长方形的长、宽、高:" << endl;
cin >> length;
cin >> width;
cin >> height;
};
void count_display() {
cout <<length * width * height << endl;
};
private:
double length;
double width;
double height;
};
int main() {
V v1, v2, v3;
v1.set_value();
cout << "长方形1的体积" << endl;
v1.count_display();
v2.set_value();
cout << "长方形2的体积" << endl;
v2.count_display();
v3.set_value();
cout << "长方形3的体积" << endl;
v3.count_display();
}