C++ Object-Oriented Programming (Second Edition Chapter 2)

Chapter Two,
the characteristics of classes and objects


Introduction:

C++ Object-Oriented Programming (Second Edition) Chapter 2 Solutions to Related Exercises


Question one

Title: Rewrite the program in Example 2.1 of this chapter, requirements:
(1). Change the data members to private
(2). Change the input and output functions to be implemented by member functions
(3). Define member functions in the class body

The demo code is as follows:

#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;
}

Question two

Topic: Based on the first question, make the following modifications: declare functions in the class body, and define member functions outside the class.

The demo code is as follows:

#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;
}

Question three

Title: In section 2.3.3 of this chapter, the header file student.h containing class definitions, the source file student.cpp containing member function definitions, and the source file main.cpp containing the main function are given in section 2.3.3. Please improve the program, Add a member function set_value that assigns initial values ​​to data members in the class.

The demo code is as follows:

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;
}


Question four

Topic: Rewrite Example 2.4 of this chapter into a multi-file program:
(1). Put the class definition in the header file arraymax.h.
(2). Put the member function definition in the source file arraymax.cpp.
(3 ). The main function is placed in the source file file1.cpp

The demo code is as follows:

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;
}


Question five:

Topic: Need to require the volume of three rectangular columns, please write an object-based program. Data members include length, width, height. Member functions are required to achieve the following functions:
(1). Input 3 rectangular columns separately from the keyboard the length, width, height;
(2) calculating the volume of a rectangular parallelepiped;.
(3) the volume of the rectangular parallelepiped three outputs;.
Please programming, debugging and running on the machine.

The demo code is as follows:

#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;
}

Guess you like

Origin blog.csdn.net/qq_44250569/article/details/108933155