C++实验三:抽象与分类(数组作为函数参数的冒泡排序、求组合数、构造函数析构函数)

it2025-07-22  12

实验目的

1、学习函数的定义与调用。

2、理解函数的参数传递机制,掌握不同的参数传递方式。

3、掌握使用函数实现递归的方法。

4、掌握类的定义、类对象定义、赋值和基本访问方法,学习类的成员函数。

5、掌握用指针访问类的方法,学习类的访问权限控制。

6、掌握类的构造函数和析函数的原理和使用。

 

实验内容

1、传递一个数组作为函数参数的时候,必须传入一个数组或者一个指针,而不能传入引用。根据上面的知识,编写程序实现冒泡排序。基本的算法是,每次循环依次把末排元素的最小元素排在前面,传入参数为数组和一个表示要排序的元素个数的整数。请通过练习复习函数参数传递方式,学习传入数组的方法。

2、数学中一个常用的计算组合数Cnm,请编写程序,用递归的方法给出组合数的计算方法。

3、定义一个代表书的book类,要求包含下面几个属性:书名、作者、页数、价格。定义适当的构造函数,第1个构造函数能够初始化全部属性,第2个构造函数能够初始化书名和作者两个属性,最后一个构造函数没有参数,只输出一条表示新建一个对象的语句。用不同的构造函数定义3个的对象,输出其已经初始化的属性。接下来,添加析构函数,其内部实现为输出一条表示回收对象的语句。设计合适的输出语句,来观察回收对象顺序的先后。

4、(选作)在图形学中,一般用三角形组成的网格去表示三维的物体,一个三角面至少应该包含以下的信息:面的编号、面的颜色(可以用3个整数表示)、面的3个顶点的三维坐标。按照上面的格式定义一个表示顶点的Node类和一个表示面的Face类,其中Face类中应该包含Node类型的成员。定义Face类的对象f,输出它的编号、颜色值和其中任意一个点的三维坐标。

 

实验过程

实验一(函数参数传递数组)

代码

#include <iostream> #define ARR_LEN 255 //数组长度上限 #define elemType int //元素类型*/ using namespace std; void bubbleSort (elemType arr[], int len) //冒泡排序法 { elemType temp; int i, j; for (i=0; i<len-1; i++) // 外循环为排序趟数,len个数进行len-1趟 for (j=0; j<len-1-i; j++) { // 内循环为每趟比较的次数,第i趟比较len-i次 if (arr[j] > arr[j+1]) { // 相邻元素比较,若逆序则交换(升序为左大于右,降序反之) temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } int main() { int i; int len = 10; elemType arr[ARR_LEN] = {0}; cout << "Input 10 numbers:"<<endl; for (i=0; i<len; i++) cin >> arr[i]; bubbleSort (arr, len); for (i=0; i<len; i++) cout << arr[i] << " "; cout << endl; return 0; }

运行结果

 

实验二(组合数计算方法)

代码

#include <iostream> using namespace std; int c(int m,int n) { int t; if(m<n) //判断输入m,n是否合法 { cout << "Incorrect input format" << endl; t = NULL; } else // 利用递归方法求组合数 if(m==n||n==0) t = 1; else t = c(m-1,n-1)+c(m-1,n); return t; } int main() { int m,n; cout << "Input 2 numbers(n,m;n<m):" << endl; cin >> n >> m; cout << c(m,n) << endl; return 0; }

结果

 

实验三

代码(Book类)

#include <iostream> using namespace std; class Book{ public: Book(string newname,string newauthor,int newpages,float newprice); Book(string newname,string newauthor); Book(); ~Book(); void showBook(); string name,author; //书名、作者 int pages;//页数 float price; //价格 }; Book::Book(string newname,string newauthor,int newpages,float newprice){ name = newname; author = newauthor; pages = newpages; price = newprice; }; Book::Book(string newname,string newauthor){ name = newname; author = newauthor; }; Book::Book(){ cout << "Create a new Book object." << endl; }; Book::~Book(){ cout << "Delete a Book object:" << name <<endl; }; void Book::showBook(){ cout << "Name:" << name << endl; cout << "Author:" << author << endl; cout << "Pages:" << price << endl; cout << "Price" << price << endl; } int main(){ Book b1("《C++程序设计》","钱能",550,39.5); Book b2("《大话数据结构》","程杰"); Book b3; cout << "The name of the first book: " << b1.name << endl; cout << "The author of the first book: " << b1.author << endl; cout << "The pages of the first book; " << b1.pages << endl; cout << "The price of the first book: " << b1.price << endl; cout << "The name of the first book: " << b2.name << endl; cout << "The author of the first book: " << b2.author << endl; return 0; }

运行结果

 

 

实验四(几何类)

代码

#include <iostream> using namespace std; class Node{ public: float s[3]; //存放顶点坐标 }; class Face{ public: int index; //面的编号 int* color; //面的颜色,定义为指针 Node* node; //Node类型成员变量 }; int main() { int c[3]={110,120,119}; //数组表示颜色 Node n[3]={{1,3,5},{2,4,6},{3,7,9}}; Face f = {1,c,n}; //定义一个Face类的对象f cout << "面的编号:" << f.index << endl; cout << "面的颜色是("<<f.color[0]<<","<<f.color[1]<<","<<f.color[2]<<")"<<endl; cout << "面上一点三维坐标("<<f.node[1].s[0]<<","<<f.node[1].s[1]<<","<<f.node[1].s[2]<<")"<<endl; return 0; }

结果

 

 

 

总结

这次实验完成了数组作为参数传递、组合数计算、类与对象Book类、选做图形Face类实验,主要练习了C++面向对象中的抽象与分类。

实验一、实验二学习了数组作为参数的传递方式,复习了函数实现递归的方法。通过编程练习实验三、选做四,基本掌握了类的定义、类对象定义、赋值和基本访问方法。通过查阅相关资料,掌握了类的构造函数和析函数的原理和使用。

通过本次实验,学习掌握了面向对象中类与对象的基本知识,可以尝试使用面向对象的方法进行编程实验。同时,在实验过程中,我体会到了面向对象的语言之间在思想上有许多共通之处,不过在实现方法上大同小异。

最新回复(0)