# C + +: Topic 5 experiment (classes and functions)

## C + +: Topic 5 experiment (classes and functions)

### Purpose of the experiment:

1. Learn the definition and call of functions.
2. Understand the parameter transfer mechanism of function and master different parameter transfer methods.
3. Master the method of using function to realize recursion.
4. Master class definition, class object definition, assignment and basic access methods, and learn class member functions.
5. Master the method of accessing classes with pointers and learn the access control of classes.
6. Master the principle and use of class constructor and analytic function.

### Experiment content:

1. When passing an array as a function parameter, you must pass in an array or a pointer, not a reference. According to the above knowledge, write a program to realize bubble sorting. The basic algorithm is to put the smallest element of the last row of elements in front of each cycle, and the input parameters are array and an integer representing the number of elements to be sorted. Please review the method of passing function parameters through practice and learn the method of passing in arrays.
2. A commonly used calculation of combinatorial number C, n and m in mathematics, please write a program to give the calculation method of combinatorial number by recursive method.
3. Define a Book class representing a book. It is required to include the following attributes: book name, author, number of pages and price. Define an appropriate constructor. The first constructor can initialize all attributes, the second constructor can initialize the two attributes of book name and author, and the last constructor has no parameters and only outputs a statement representing a new object. Define three different objects with different constructors and output their initialized properties. Next, add a destructor whose internal implementation is to output a statement representing the recycled object. Design appropriate output statements to observe the order of recycling objects.
4. (optional) in graphics, a mesh composed of triangles is used to represent three-dimensional objects in a hundred years. A triangular surface should contain at least the following information: the number of the surface, the color of the surface (which can be expressed by three integers), and the three-dimensional coordinates of the three vertices of the surface. Define a Node class representing vertices and a Face class representing faces according to the above format. The Face class should contain members of Node type. Define the object f of Face class, and output its number, color value and 3D coordinates of any point.

#### (1) Write a program to realize bubble sorting. Please review the method of passing function parameters through practice and learn the method of passing in arrays.

```#include <iostream>

using namespace std;

int bubbleSort(int arr[], int len)
{
for(int i = len; i > 0; i--)
for(int j = 0; j < len - 1; j++)
{
if(arr[j] > arr[j + 1])
{
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}

for(int k = 0; k < len; k++)
{
if(k == len - 1)
cout << arr[k] << endl;
else
cout << arr[k] << "<";
}

return 0;
}

int main()
{
int len = 10;
int a[len];
cout << "Please enter 10 numbers (separated by spaces):" << endl;

for(int i = 0; i < len; i++)
cin >> a[i];

bubbleSort(a, len);
return 0;
}
```

#### (2) A commonly used calculation of combinatorial number C, n and m in mathematics, please write a program to give the calculation method of combinatorial number by recursive method.

```#include <iostream>

using namespace std;

int factorial(int m)

{
if(m == 1)
return 1;

return factorial(m - 1) * m;
}

int main()
{
int m, n;
cout << "Please enter Cmn Medium m and n(m>=n):" << endl;
cout << "m = ";
cin >> m;
cout << "n = ";
cin >> n;

if(m >= n)
cout << "Cmn The value of is:" << factorial(m) / factorial(n) / factorial(m - n) << endl;
else
cout << "Wrong input!" << endl;

return 0;
}
```

#### (3) Define a Book class representing a book. It is required to include the following attributes: book name, author, number of pages and price. Define an appropriate constructor. The first constructor can initialize all attributes, the second constructor can initialize the two attributes of book name and author, and the last constructor has no parameters and only outputs a statement representing a new object. Define three different objects with different constructors and output their initialized properties. Next, add a destructor whose internal implementation is to output a statement representing the recycled object. Design appropriate output statements to observe the order of recycling objects.

```#include <iostream>

using namespace std;

class Book
{
public:

Book(string Name, string Author, int Pages, float Price);
Book(string Name, string Author);
Book();
~Book();

string name;
string author;
int pages;
float price;
};

Book::Book(string Name, string Author, int Pages, float Price)
{
name = Name, author = Author, pages = Pages, price = Price;
};

Book::Book(string Name, string Author)
{
name = Name, author = Author;
};

Book::Book()
{
cout << "A Book object has been created." << endl;
};

Book::~Book()
{
cout << "The Book object: " << name << " has been deleted." << endl;
};

int main()
{
Book b1("Advanced mathematics", "Fudan University Press ", 150, 28);
Book b2("linear algebra", "Central China Normal University Press");
Book b3;
cout << "b1:" << b1.name << " " << b1.author << " " << b1.pages << "page " << b1.price << "element " << endl;
cout << "b2:" << b2.name << " " << b2.author << endl;
cout << "b3:" << endl;
return 0;
}
```

#### (4) (optional) in graphics, a mesh composed of triangles is used to represent three-dimensional objects in a hundred years. A triangular surface should contain at least the following information: the number of the surface, the color of the surface (which can be expressed by three integers), and the three-dimensional coordinates of the three vertices of the surface. Define a Node class representing vertices and a Face class representing faces according to the above format. The Face class should contain members of Node type. Define the object f of Face class, and output its number, color value and 3D coordinates of any point.

```#include <iostream>

using namespace std;

class Node
{
public:
float vertex[3];
};

class Face
{
public:
int number;
//Experience the function of using pointers for these two parameters
int* color;
Node* node;

Face(int Num, int colr[3], Node N[3]);
};

Face::Face(int Num, int colour[3], Node N[3])
{
number = Num, color = colour, node = N;
}

int main()
{
int colour[3] = {108, 234, 255};
Node N[3] = {{1, 2, 3}, {2, 1, 3}, {3, 2, 1}};
Face f(1, colour, N);
cout << "f:number " << f.number << endl;
cout << "color (" << f.color[0] << "," << f.color[1] << "," << f.color[2] << ")" << endl;
cout << "onenode " << f.node[2].vertex[0] << ", " << f.node[2].vertex[1] << ", " << f.node[2].vertex[2] << endl;
return 0;
}
```

Tags: C++ Back-end

Posted on Tue, 16 Nov 2021 23:39:25 -0500 by mjedman1