C++程序设计谭浩强 第八章(类和对象)习题答案(部分有改进)

8.1 正确代码

#include 
using namespace std;
class Time
 {public:                    //成员改为公用的
    int hour;
    int minute;
    int sec;
  };
Time t;
void set_time(void)          //在main函数之前定义
 {
  cin>>t.hour;
  cin>>t.minute;
  cin>>t.sec;
 }
 
void show_time(void)         //在main函数之前定义
 {
  cout<

8.2 将上题 :数据成员改为私有,输入输出由成员函数实现,在类体内定义成员函数

#include 
using namespace std;
class Time
  {public:
    void set_time(void)
     {cin>>hour;
      cin>>minute;
      cin>>sec;
     }
    void show_time(void)
     {cout<

8.3 类体内声明成员函数,类外定义成员函数

#include 
using namespace std;
class Time
  {public:
    void set_time(void);
    void show_time(void);
   private:
    int hour;
    int minute;
    int sec;
  };
  
void Time::set_time(void)
  {cin>>hour;
   cin>>minute;
   cin>>sec;
  }
     
void Time::show_time(void)
{cout<

8.4 包含类定义的头文件 student.h  包含成员函数定义的源文件 student.cpp 包含主函数的源文件main.cpp

完善程序,增加对数据成员赋初值的成员函数 set_value

//main.cpp
#include 
using namespace std;
#include "student.h"
int main()
{Student stud;
 stud.set_value();
 stud.display();
 return 0;
}
//student.cpp
#include "student.h"                      //在此文件中进行函数的定义
#include   
using namespace std;                   //不要漏写此行
void Student::display( )              
{ cout<<"num:"<>num;
  cin>>name;
  cin>>sex;
}

//student.h                           
class Student                      
{ public:
    void display( );  
	void set_value();               
  private:
    int num;
    char name[20];
    char sex ;
  };

8.5 将类定义放在头文件arraymax.h中,将成员函数定义放在源文件arraymac.cpp中,主函数放在原文进件file1.cpp中

//file1.cpp
#include 
#include "arraymax.h"
int main()
 {Array_max  arrmax;
  arrmax.set_value();
  arrmax.max_value();
  arrmax.show_value();
  return 0;
 }
//arraymax.cpp
#include 
using namespace std;
#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="<
//arraymax.h
class Array_max
{public:
   void set_value();
   void max_value();
   void show_value();
 private:
   int array[10];
   int max;
};

8.6    需要求3个长方柱的体积,编一个基于对象的程序。数据成员包括length  width  height

         由键盘分别输入3个长方柱的长宽高,计算体积,输出三个体积

#include 
using namespace std;
class Box
{
public:
	void get_value();
	void volume();
	void display();
public:
	float lengh;
	float width;
	float height;
	float vol;
};

void Box::get_value()
{
	cout << "please input lengh, width,height:";
	cin >> lengh;
	cin >> width;
	cin >> height;
}

void Box::volume()
{
	vol = lengh * width * height;
}

void Box::display()
{
	cout << vol << endl;
}

int main()
{
	Box box1, box2, box3;
	box1.get_value();
	box1.volume();
	cout << "volmue of bax1 is ";
	box1.display();
	box2.get_value();
	box2.volume();
	cout << "volmue of bax2 is ";
	box2.display();
	box3.get_value();
	box3.volume();
	cout << "volmue of bax3 is ";
	box3.display();
	return 0;
}

你可能感兴趣的:(C++,c++,程序设计,类,面向对象编程,谭浩强)