# Shandong University "c + + Programming" lab3

## Objective:

Familiar with c + + pointer, reference, operator new and delete.
Familiar with c + + classes and objects.

## Experiment steps and contents:

1. In the main function, accept the three integers input from the keyboard, call the following three functions to sort, and then output the sorted results.

• The functions sort1 (int x, int y, int z), sort2 (int * Px, int * py, int * PZ), sort3 (int & X, int & Y, int & z) are implemented. The parameters are received by passing values, pointers, and references respectively. The three integers are sorted from small to large, and the sorted results are output.
• What are the differences among the three ways of passing parameters? Which way can get the sorted results correctly in the main function?
```#include<iostream>
using namespace std;
//Pass value
void sort1(int a, int b, int c){
//sort(&x,&y,&z);
int temp;
if(a>b)
{temp=a;a=b;b=temp;}
if(a>c)
{temp=a;a=c;c=temp;}
if(b>c)
{temp=b;b=c;c=temp;}
printf("The order of the three integers is:%d<%d<%d\n",a,b,c);
}
//Passing pointer
void sort2(int *a, int *b, int *c){
int temp;
if(*a>*b)
{temp=*a;*a=*b;*b=temp;}
if(*a>*c)
{temp=*a;*a=*c;*c=temp;}
if(*b>*c)
{temp=*b;*b=*c;*c=temp;}
printf("The order of the three integers is:%d<%d<%d\n",*a,*b,*c);}
//Citation
void sort3(int &a, int &b, int &c){
int temp;
if(a>b)
{temp=a;a=b;b=temp;}
if(a>c)
{temp=a;a=c;c=temp;}
if(b>c)
{temp=b;b=c;c=temp;}
printf("The order of the three integers is:%d<%d<%d\n",a,b,c);
}
int main(){
int a,b,c;
int *x,*y,*z;
scanf("%d %d %d",&a,&b,&c);
printf("The three integers you entered are:%d %d %d\n",a,b,c);
x=&a;
y=&b;
z=&c;
sort1(a,b,c);
sort2(x,y,z);
sort3(a,b,c);
}
```

(test results are as follows:) In the main function, sort2 and sort3 methods can get correct results, because the values of a, b and c are really changed by pointer passing and reference. In sort1 method, only the values are passed in as local variables, and the changed values cannot be obtained in other fields.

2. In the main function, first accept the number of array elements n input from the keyboard, then use new to dynamically create a one-dimensional array containing n elements, then input the array element values from the keyboard, and finally output the values of all elements of the array

```#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Please enter the number of array elements\n";
cin>>n;
int *arr=new int[n];
for(int i=0;i<n;i++) cin>>arr[i];
cout<<"The array you entered is:\n";
for(int i=0;i<n;i++) cout<<arr[i]<<" ";
return 0;
}
```

(test results are as follows:) 3. Please check the following program, find out the errors and correct them, and debug on the computer to make it run normally. When running, input the values of hour, minute and second from the keyboard, and check whether the output is correct. ```#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int sec;
public:
void  set_time(void)
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void  show_time(void)
{
cout<<hour<<" : "<<minute<<" : "<<sec<<endl;
}

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

(test results are as follows:) 4. Rewrite the procedure in 1, which requires:

• (1) Changing data members to private;
• (2) The input and output functions are implemented by member functions, and member functions are defined in the class body.
```#include<iostream>
using namespace std;
class Time{
private:
int hour;
int minute;
int sec;
public:
void set_time(){
cin>>this->hour;
cin>>this->minute;
cin>>this->sec;
}
void show_time(){
//When time shows single digit, most of them are filled with zeros
printf("%02d:%02d:%02d",this->hour,this->minute,this->sec);
}
};
int main(){
Time t;
t.set_time();
t.show_time();
return 0;
}
```

(test results are as follows:) 5. To find the volume of three boxes, please write an object-based program. The data members of the class include length, width and height. Three member functions are defined to implement the following functions:

• (1) Input the length, width and height of three cuboids by keyboard.
• (2) Calculate the volume of cuboid;
• (3) Output the volume of 3 boxes.

Please write the program, debug and run it.

```#include <iostream>
using namespace std;
class Cube{
int length,width,height;
public:
void getdata(){
cin>>length;
cin>>width;
cin>>height;
}
int volume(){
return length*width*height;
}
void printV(){
printf("long%d wide%d high%d The volume of the box of is%d\n",length,width,height,volume());  	}
};
int main(){
Cube c1,c2,c3;
//(1) Input the length, width and height of three cuboids by keyboard.
cout<<"First box:"<<endl;
c1.getdata();
cout<<"Second box:"<<endl;
c2.getdata();
cout<<"Third box:"<<endl;
c3.getdata();
cout<<endl;
//(2) Calculates the volume of the box.
cout<<"The volume of the first box is"<<c1.volume()<<endl;
cout<<"The volume of the second box is"<<c2.volume()<<endl;
cout<<"The volume of the third box is"<<c3.volume()<<endl;
cout<<endl;
//(3) Output the volume of 3 boxes.
c1.printV();
c2.printV();
c3.printV();
return 0;
}
```

(test results are as follows:)   Published 11 original articles, praised 0, visited 19

Posted on Tue, 14 Jan 2020 04:44:32 -0500 by sonnieboy