C++66页作业
第1——第6题
#include<iostream>//66页1题
using namespace std;
class Time{
public:
void set_time(void);
void show_time(void);
int hour; int minute; int sec;
};Time t;
int main(){
t.set_time();
t.show_time();
return 0;
}
void Time:: set_time(void){
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void Time:: show_time(void){
cout<<t.hour<<":"<<minute<<":"<<sec<<endl;
}
#include<iostream>//66页2题
using namespace std;
class Time{
private:
int hour;
int minute;
int sec;
public:
void set_time(){
cin>>hour>>minute>>sec;
}
void show_time(){
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
};
int main(){
Time t;
t.set_time();
t.show_time();
return 0;
}
#include<iostream>//66页3题
using namespace std;
class Time{
private:
int hour;
int minute;
int sec;
public:
void set_time();
void show_time();
};
void Time::set_time(){
cin>>hour>>minute>>sec;
}
void Time::show_time(){
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main(){
Time t;
t.set_time();
t.show_time();
return 0;
}
#include <iostream>//头文件67页4题
using namespace std;
class Student {
public:
void display();
void set_value();
private:int num;
string name;
char sex;
};
#include <iostream>//主函数,67页4题
#include"student.h"
using namespace std;
void Student::display(){
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
void Student::set_value(){
cin>>num>>name>>sex;
}
#include <iostream>//主函数,67页4题
#include "student.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
Student stud1,stud2;
stud1.set_value();
stud1.display() ;
stud2.set_value() ;
stud2.display() ;
return 0;
}
#include<iostream>//头文件,67页第5题
using namespace std;
class Array_max{
public:
void set_value();
void max_value();
void show_value();
private:
int array[10];
int max;
};
#include<iostream> //成员函数定义, 67页第5题
#include"arraymax.h"
using namespace std;
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;
}
#include <iostream>//主函数,67页第5题
#include"arraymax.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
Array_max arrmax; //定义对象arrmax
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}
#include<iostream> //67页第6题
using namespace std;
class Volume{
private:
int length;
int width;
int height;
public:
void set_volume(void){
cin>>length;
cin>>width;
cin>>height;}
void result_volume(void) {
cout<<length*width*height<<endl;
}
};
Volume v1,v2,v3;
int main(){
v1.set_volume();
v1.result_volume();
v2.set_volume();
v2.result_volume();
v3.set_volume();
v3.result_volume();
return 0;
}