最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

谭浩强C++ 第八章

IT圈 admin 1浏览 0评论

谭浩强C++ 第八章

第八章课后习题

1、找出程序错误
#include<iostream>
using namespace std;
class Time{//主要错误,若类中不指定成员访问属性,默认私有,不能被外界调用, 而如果是普通函数不可调用类的私有成员,所以两种修改方法
//1)将数据成员修改为公有,函数声明为类外普通函数  2) 数据成员声明为私有,函数声明为类的公有成员函数以在类外调用 void set_time(void);void show_time(void);int hour;int minute;int sec;
}; 
Time t;
int main(){ set_time();show_time();				return 0;
}
int set_time(void){cin>>t.hour;cin>>t.minute;cin>>t.sec;
}
int show_time(void){cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;
}
#include<iostream>
using namespace std;
//修改1 将数据成员修改为公有,函数声明为类外普通函数 
class Time{public: int hour;int minute;int sec;
}; 
Time t;
int main(){ void set_time(void);//使用函数前先声明 void show_time(void);set_time();show_time();				return 0;
}
void set_time(void){//void 类型 cin>>t.hour;cin>>t.minute;cin>>t.sec;
}
void show_time(void){cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;
}
#include<iostream>
using namespace std;
//修改2 数据成员声明为私有,函数声明为类的公有成员函数以在类外调用
class Time{public: void set_time(void);void show_time(void);private: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<<" "<<t.minute<<" "<<t.sec<<endl;
}
2、按要求修改上面的类
#include<iostream>
using namespace std; 
class Time{public: void set_time(void){cin>>hour;cin>>minute;cin>>sec;}void show_time(void){cout<<hour<<" "<<minute<<" "<<sec<<endl;	}private:int hour;int minute;int sec;
}; 
Time t;
int main(){ t.set_time();t.show_time();				return 0;
}
3、按要求修改上面类
//即第一题修改2
#include<iostream>
using namespace std;
class Time{public: void set_time(void);void show_time(void);private: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<<" "<<t.minute<<" "<<t.sec<<endl;
}
4、对类增加set_value()函数
//8.3.3没有给出相关程序! 参考书上的类,以及答案,完成类定义头文件,成员函数定义和main定义
//建立项目,包含student.h student.cpp  main.cpp//student.h
class Student{public: void display();void set_value();private:int num;char name[20];char sex;
}; //student.cpp
#include "student.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(){cout<<"please input num,name,sex(F/M):"<<endl;cin>>num>>name>>sex;
}//main.cpp
#include "student.h"
int main(){Student stu;stu.set_value();stu.display();return 0;
}
5、对例题8.4改写成多文件程序
//与上一题类似
//建立项目,包含arraymax.h arraymax.cpp  file1.cpp//arraymax.h
class Array_max{public: void set_value();void max_value();void show_value();private:int array[10];int max;
}; //arraymax.cpp
#include "arraymax.h"
#include<iostream>
using namespace std;
void Array_max::set_value(){int i;cout<<"input ten integers:"<<endl;for(i=0;i<10;i++){cin>>array[i];}
}
void Array_max::max_value(){int i;max=array[0];for(i=0;i<10;i++){if(array[i]>max) max=array[i];}
}
void Array_max::show_value(){cout<<"max="<<max<<endl;
}//file1.cpp
#include "arraymax.h"
int main(){Array_max am;am.set_value();am.max_value();am.show_value();return 0;
}
6、写一个长方体类
#include<iostream>
using namespace std;
class Cuboid{public: void set_value();double volume();void show_v();private:double length;double width;double height;
}; 
void Cuboid::set_value(){cout<<"input lenght,width,height:"<<endl;cin>>length>>width>>height;
}
double Cuboid::volume(){return length*width*height;
}
void Cuboid::show_v(){cout<<"volume="<<volume();
}
int main(){ Cuboid r;r.set_value() ;r.show_v() ;				return 0;
}

谭浩强C++ 第八章

第八章课后习题

1、找出程序错误
#include<iostream>
using namespace std;
class Time{//主要错误,若类中不指定成员访问属性,默认私有,不能被外界调用, 而如果是普通函数不可调用类的私有成员,所以两种修改方法
//1)将数据成员修改为公有,函数声明为类外普通函数  2) 数据成员声明为私有,函数声明为类的公有成员函数以在类外调用 void set_time(void);void show_time(void);int hour;int minute;int sec;
}; 
Time t;
int main(){ set_time();show_time();				return 0;
}
int set_time(void){cin>>t.hour;cin>>t.minute;cin>>t.sec;
}
int show_time(void){cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;
}
#include<iostream>
using namespace std;
//修改1 将数据成员修改为公有,函数声明为类外普通函数 
class Time{public: int hour;int minute;int sec;
}; 
Time t;
int main(){ void set_time(void);//使用函数前先声明 void show_time(void);set_time();show_time();				return 0;
}
void set_time(void){//void 类型 cin>>t.hour;cin>>t.minute;cin>>t.sec;
}
void show_time(void){cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;
}
#include<iostream>
using namespace std;
//修改2 数据成员声明为私有,函数声明为类的公有成员函数以在类外调用
class Time{public: void set_time(void);void show_time(void);private: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<<" "<<t.minute<<" "<<t.sec<<endl;
}
2、按要求修改上面的类
#include<iostream>
using namespace std; 
class Time{public: void set_time(void){cin>>hour;cin>>minute;cin>>sec;}void show_time(void){cout<<hour<<" "<<minute<<" "<<sec<<endl;	}private:int hour;int minute;int sec;
}; 
Time t;
int main(){ t.set_time();t.show_time();				return 0;
}
3、按要求修改上面类
//即第一题修改2
#include<iostream>
using namespace std;
class Time{public: void set_time(void);void show_time(void);private: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<<" "<<t.minute<<" "<<t.sec<<endl;
}
4、对类增加set_value()函数
//8.3.3没有给出相关程序! 参考书上的类,以及答案,完成类定义头文件,成员函数定义和main定义
//建立项目,包含student.h student.cpp  main.cpp//student.h
class Student{public: void display();void set_value();private:int num;char name[20];char sex;
}; //student.cpp
#include "student.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(){cout<<"please input num,name,sex(F/M):"<<endl;cin>>num>>name>>sex;
}//main.cpp
#include "student.h"
int main(){Student stu;stu.set_value();stu.display();return 0;
}
5、对例题8.4改写成多文件程序
//与上一题类似
//建立项目,包含arraymax.h arraymax.cpp  file1.cpp//arraymax.h
class Array_max{public: void set_value();void max_value();void show_value();private:int array[10];int max;
}; //arraymax.cpp
#include "arraymax.h"
#include<iostream>
using namespace std;
void Array_max::set_value(){int i;cout<<"input ten integers:"<<endl;for(i=0;i<10;i++){cin>>array[i];}
}
void Array_max::max_value(){int i;max=array[0];for(i=0;i<10;i++){if(array[i]>max) max=array[i];}
}
void Array_max::show_value(){cout<<"max="<<max<<endl;
}//file1.cpp
#include "arraymax.h"
int main(){Array_max am;am.set_value();am.max_value();am.show_value();return 0;
}
6、写一个长方体类
#include<iostream>
using namespace std;
class Cuboid{public: void set_value();double volume();void show_v();private:double length;double width;double height;
}; 
void Cuboid::set_value(){cout<<"input lenght,width,height:"<<endl;cin>>length>>width>>height;
}
double Cuboid::volume(){return length*width*height;
}
void Cuboid::show_v(){cout<<"volume="<<volume();
}
int main(){ Cuboid r;r.set_value() ;r.show_v() ;				return 0;
}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论