天天看點

c++面向對象程式設計(第二版第二章)簡介:題一題二題三題四題五:

第二章、

類和對象的特性

  • 簡介:
  • 題一
  • 題二
  • 題三
  • 題四
  • 題五:

簡介:

c++面向對象程式設計(第二版)第二章相關習題的解答

題一

題目:改寫本章例2.1程式,要求:

(1).将資料成員改為私有的

(2).将輸入和輸出的功能改為由成員函數實作

(3).在類體内定義成員函數

示範代碼如下:

#include<iostream>
using namespace std;
class Time
{
private:
	int hour;
	int minute;
	int sec;
public:
	void set_time(void)
	{
		cout<<"請輸入小時:"<<endl;
		cin>>hour;
		cout<<"請輸入分鐘:"<<endl;
	    cin>>minute;
		cout<<"請輸入秒:"<<endl;
		cin>>sec;
	}
	void show_time(void)
	{
		cout<<"最後時間為:"<<hour<<":"<<minute<<":"<<sec<<endl;
	}
};

int main()
{
	 Time T;
	 T.set_time();
	 T.show_time();
	 return 0;
}
           

題二

題目:在第一題的基礎上進行如下修改:在類體内聲明函數,而在類體外定義成員函數.

示範代碼如下:

#include<iostream>
using namespace std;
class Time
{
private:
	int hour;
	int minute;
	int sec;
public:
	void set_time(void);
	void show_time(void);
	
};
void Time::set_time(void)
{
    cout<<"請輸入小時:"<<endl;
	cin>>hour;
	cout<<"請輸入分鐘:"<<endl;
	cin>>minute;
    cout<<"請輸入秒:"<<endl;
	cin>>sec;
}
void Time::show_time(void)
{
		cout<<"最後時間為:"<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
	 Time T;
	 T.set_time();
	 T.show_time();
	 return 0;
}
           

題三

題目:在本章第2.3.3節中分别給出了包含類定義的頭檔案student.h,包含成員函數定義的源檔案student.cpp以及包含主函數的源檔案main.cpp.請完善改程式,在類中增加一個對資料成員賦初值的成員函數set_value.

示範代碼如下:

Student.h頭檔案代碼:
#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
	void set_value();
    void display();
private:
	int num;
	string name;
	char sex;
};

Student.cpp源檔案代碼:
#include<iostream>
#include<string>
#include"student.h"
using namespace std;
void Student::set_value()
{
	cout<<"please your num:"<<endl;
	cin>>num;
	cout<<"please your name:"<<endl;
	cin>>name;
	cout<<"please your sex:"<<endl;
	cin>>sex;
}
void Student::display()
{
	cout<<"num:"<<num<<endl;
	cout<<"name:"<<name<<endl;
	cout<<"sex:"<<sex<<endl;
}

Main.cpp源檔案代碼:
#include<iostream>
#include<string>
#include"student.h"
int main()
{
	Student S;
	S.set_value();
	S.display();
    return 0;
}

           

題四

題目:将本章的例2.4改寫為一個多檔案的程式:

(1).将類定義放在頭檔案arraymax.h中.

(2).将成員函數定義放在源檔案arraymax.cpp中.

(3).主函數放在源檔案file1.cpp中

示範代碼如下:

Arraymax.h頭檔案代碼:
#include<iostream>
using namespace std;
class Array_max
{
public:
	void set_value();
	void max_value();
	void show_value();
private:
	int array[10];
	int max;
};

Arraymax.cpp源檔案代碼:
#include<iostream>
#include"arraymax.h"
using namespace std;
void Array_max::set_value()
{
	int i;
cout<<"請輸入10個數:"<<endl;
	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;
}

File1.cpp源檔案代碼:
#include<iostream>
#include"arraymax.h"
using namespace std;
int main()
{
	Array_max arrmax;
	arrmax.set_value();
	arrmax.max_value();
	arrmax.show_value();
	return 0;
}

           

題五:

題目:需要求三個長方柱的體積,請編寫一個基于對象的程式.資料成員包括length,width,height.要求用成員函數實作以下功能:

(1).由鍵盤分别輸入3個長方柱的長,寬,高;

(2).計算長方體的體積;

(3).輸出3個長方體的體積;

請編寫程式,上機調試并運作.

示範代碼如下:

#include<iostream>
using namespace std;
class volume
{
public:
		void input();
		void show_volume(); 
		void show1_volume(); 
private:
	    int length;
		int width;
		int high;
		int Volume;
};

int main()
{
	volume v[3];
	int i=0;
	for(i=0;i<3;i++)
	{
		v[i].input();
	    v[i].show_volume();
	    v[i].show1_volume();
	}
	system("pause");
	return 0;
}
void volume::input()
{
	cout<<"please input length:"<<endl;
	cin>>length;
	cout<<"please input width:"<<endl;
	cin>>width;
	cout<<"please input high:"<<endl;
	cin>>high;
}
void volume::show_volume()
{
	Volume=length*width*high;
}
void volume::show1_volume()
{
	cout<<"volume="<<Volume<<endl<<endl;
}
           

繼續閱讀