C++面向对象的程序设计谭浩强 第二章课后题

以往章节

C++面向对象的程序设计谭浩强 第三章课后题

C++面向对象的程序设计谭浩强 第四章课后题

C++面向对象的程序设计谭浩强 第五章课后题

C++面向对象的程序设计谭浩强 第六章课后题

C++面向对象的程序设计谭浩强 第七章课后题

C++面向对象的程序设计谭浩强 第八章课后题

1. (简答题)

 请检查下面程序,找出其中的错误(先不要上机,在纸面上作人工检查),并改正之。然后上机调试,使之能正常运行。运行时从键盘输入时、分、秒的值,检查输出是否正确。

#include 
using namespace std;
class Time
{ void set_time(void);
void show_time(void);
int hour;
int minute;
int sec;
};
Time t;
int main( )
{
set_time( );
show_time( );
}
int set_time(void)
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
int show_time(void)
{
 cout<
#include 
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();
}
void Time::set_time(void)
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl;
}

2. (简答题)

 改写本章例2.1程序,要求: 

(1) 将数据成员改为私有的;

(2) 将输入和输出的功能改为由成员函数实现;

(3) 在类体内定义成员函数。

#include 
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 t1,t2;
int main()
{
t1.set_time();
t1.show_time();
t2.set_time();
t2.show_time();
return 0;
}

3. (简答题)

 在上一题的基础上进行如下修改: 在类体内声明成员函数,而在类外定义成员函数。

#include 
using namespace std;
class Time{
public:
void set_time(void);
void show_time(void);
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1,t2;
t1.set_time();
t1.show_time();
t2.set_time();
t2.show_time();
return 0;
}
void Time::set_time(void)
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl;
}

4. (简答题)在本章第2.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value。上机调试并运行。

#include
using namespace std;
class Student
{
public:
void set_value();
void display();
private:
int num;
string name;
char sex;
};
//上面是我头文件底下是我的源文件
#include
#include"student.h"
void Student::set_value()
{
cin >> num >> name >> sex;
}
void Student::display()
{
cout << num << endl;
cout << name << endl;
cout << sex << endl;
}
//这是我主函数源文件
#include
#include"student.h"
using namespace std;
int main()
{
Student s;
s.set_value();
s.display();
return 0;
}

5. (简答题)

将本章的例2.4 改写为一个多文件的程序:

(1) 将类定义放在头文件arraymax.h中;

(2) 将成员函数定义放在源文件arraymax.cpp中;

(3) 主函数放在源文件file1.cpp中。

请写出完整的程序,上机调试并运行。

#include
using namespace std;
class Array_max
{
public:
void set_value();
void max_value();
void show_value();
private:
int array[10];
int max;
};
//上面是我的头文件底下是我的源文件
#include
#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;
}
//这个是我主函数的源文件
#include
#include"arraymax.h"
using namespace std;
int main()
{
Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}

6. (简答题)

需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能: 

(1) 由键盘分别输入3个长方柱的长、宽、高;

(2) 计算长方柱的体积;

(3) 输出3个长方柱的体积。

请编程序,上机调试并运行。

#include
using namespace std;
class Cube {
public:
    void set();
    void show();
private:
    double length;
    double width;
    double height;
    double VV();
};
void Cube::set() {
    cin >> length >> width >> height;
}
double Cube::VV() {
    return length * width * height;;
}
void Cube::show() {
    cout << VV() << endl;
}
int main() {
    Cube cube2, cube3, cube1;
    cube1.set();
    cube1.show();
    cube2.set();
    cube2.show();
    cube3.set();
    cube3.show();
    return 0;
}

你可能感兴趣的:(C++,c++)