设计一个模板类SET,集合中可以存放不同类型的对象;然后编写一个演示程序利用SET创建并使用整数的集合、字符串的集合以及学生的集合,学生类STUDENT是自己设计的类类型,包括对学生的学号、姓名、年龄、系别等信息的描述及必要的行为。(提示:集合中的元素不允许重复,且元素之间是无序的)SET类的原型可参考如下:template <class T>class SET {private:T elements[MAX_LEN];// 定义元素集合,来记录SET中的元素intN;// 定义元素的实际个数public:SET(); SET(T data[], int n);SET(const SET &obj);~SET();int hasElement(T one); //判断元素one是否在集合中void inputElement(T one); // 向集合中加入一个元素void delElement(T one);// 从集合中删除一个元素void showAllElements(); //显示集合中的所有元素// SET对象之间的运算:交、并、差,分别采用运算符重载实现SET operator+(const SET &obj);SET operator*(const SET &obj);SET operator-(const SET &obj);SET operator=(const SET &obj);};STUDENT类的原型可参考如下:class STUDENT{private:long id;char name[MAX_LEN];char sex;int age;public:STUDENT();STUDENT(const STUDENT& obj);STUDENT(long idd, char namee[], char sexx, int agee);~STUDENT();int operator==(const STUDENT& obj);STUDENT operator=(const STUDENT& obj);// 定义友元函数friend ostream& operator<<(ostream& stream, const STUDENT& obj);};题目说明:1)SET中的数据可以使用固定长度的数组存储,也可以使用动态数组存储,也可
时间: 2024-02-09 22:10:45 浏览: 67
cpp代码-设计一个数组模板类(myvector),完成对int、char、teacher类型元素的管理。teacher类元素为简单数据类型
以其他方式存储;
2)在演示程序中,需要创建整数、字符串和学生的集合,并进行交、并、差等运算;
3)在STUDENT类中,需要重载==运算符和=运算符,并定义友元函数ostream& operator<<(ostream& stream, const STUDENT& obj),用于输出学生信息。
下面是一个可能的实现:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_LEN = 100;
template <class T>
class SET {
private:
T elements[MAX_LEN];
int N;
public:
SET() {
N = 0;
}
SET(T data[], int n) {
N = 0;
for (int i = 0; i < n; i++) {
inputElement(data[i]);
}
}
SET(const SET &obj) {
N = obj.N;
for (int i = 0; i < N; i++) {
elements[i] = obj.elements[i];
}
}
~SET() {}
int hasElement(T one) {
for (int i = 0; i < N; i++) {
if (elements[i] == one) {
return i;
}
}
return -1;
}
void inputElement(T one) {
if (hasElement(one) == -1) {
elements[N++] = one;
}
}
void delElement(T one) {
int index = hasElement(one);
if (index != -1) {
for (int i = index; i < N - 1; i++) {
elements[i] = elements[i + 1];
}
N--;
}
}
void showAllElements() {
for (int i = 0; i < N; i++) {
cout << elements[i] << " ";
}
cout << endl;
}
SET operator+(const SET &obj) {
SET result;
for (int i = 0; i < N; i++) {
result.inputElement(elements[i]);
}
for (int i = 0; i < obj.N; i++) {
result.inputElement(obj.elements[i]);
}
return result;
}
SET operator*(const SET &obj) {
SET result;
for (int i = 0; i < N; i++) {
if (obj.hasElement(elements[i]) != -1) {
result.inputElement(elements[i]);
}
}
return result;
}
SET operator-(const SET &obj) {
SET result;
for (int i = 0; i < N; i++) {
if (obj.hasElement(elements[i]) == -1) {
result.inputElement(elements[i]);
}
}
return result;
}
SET operator=(const SET &obj) {
N = obj.N;
for (int i = 0; i < N; i++) {
elements[i] = obj.elements[i];
}
return *this;
}
};
class STUDENT {
private:
long id;
char name[MAX_LEN];
char sex;
int age;
public:
STUDENT() {}
STUDENT(const STUDENT& obj) {
id = obj.id;
strcpy(name, obj.name);
sex = obj.sex;
age = obj.age;
}
STUDENT(long idd, char namee[], char sexx, int agee) {
id = idd;
strcpy(name, namee);
sex = sexx;
age = agee;
}
~STUDENT() {}
int operator==(const STUDENT& obj) {
return (id == obj.id && strcmp(name, obj.name) == 0 && sex == obj.sex && age == obj.age);
}
STUDENT operator=(const STUDENT& obj) {
id = obj.id;
strcpy(name, obj.name);
sex = obj.sex;
age = obj.age;
return *this;
}
friend ostream& operator<<(ostream& stream, const STUDENT& obj) {
stream << obj.id << " " << obj.name << " " << obj.sex << " " << obj.age;
return stream;
}
};
int main() {
SET<int> intSet1, intSet2;
intSet1.inputElement(1);
intSet1.inputElement(2);
intSet1.inputElement(3);
intSet2.inputElement(2);
intSet2.inputElement(3);
intSet2.inputElement(4);
SET<int> intSet3 = intSet1 + intSet2;
SET<int> intSet4 = intSet1 * intSet2;
SET<int> intSet5 = intSet1 - intSet2;
cout << "Int Set 1: ";
intSet1.showAllElements();
cout << "Int Set 2: ";
intSet2.showAllElements();
cout << "Int Set 1 + Int Set 2: ";
intSet3.showAllElements();
cout << "Int Set 1 * Int Set 2: ";
intSet4.showAllElements();
cout << "Int Set 1 - Int Set 2: ";
intSet5.showAllElements();
SET<string> stringSet1, stringSet2;
stringSet1.inputElement("hello");
stringSet1.inputElement("world");
stringSet2.inputElement("world");
stringSet2.inputElement("!");
SET<string> stringSet3 = stringSet1 + stringSet2;
SET<string> stringSet4 = stringSet1 * stringSet2;
SET<string> stringSet5 = stringSet1 - stringSet2;
cout << "String Set 1: ";
stringSet1.showAllElements();
cout << "String Set 2: ";
stringSet2.showAllElements();
cout << "String Set 1 + String Set 2: ";
stringSet3.showAllElements();
cout << "String Set 1 * String Set 2: ";
stringSet4.showAllElements();
cout << "String Set 1 - String Set 2: ";
stringSet5.showAllElements();
STUDENT student1(1, "Alice", 'F', 20), student2(2, "Bob", 'M', 21), student3(3, "Charlie", 'M', 22);
SET<STUDENT> studentSet1, studentSet2;
studentSet1.inputElement(student1);
studentSet1.inputElement(student2);
studentSet2.inputElement(student2);
studentSet2.inputElement(student3);
SET<STUDENT> studentSet3 = studentSet1 + studentSet2;
SET<STUDENT> studentSet4 = studentSet1 * studentSet2;
SET<STUDENT> studentSet5 = studentSet1 - studentSet2;
cout << "Student Set 1: ";
studentSet1.showAllElements();
cout << "Student Set 2: ";
studentSet2.showAllElements();
cout << "Student Set 1 + Student Set 2: ";
studentSet3.showAllElements();
cout << "Student Set 1 * Student Set 2: ";
studentSet4.showAllElements();
cout << "Student Set 1 - Student Set 2: ";
studentSet5.showAllElements();
return 0;
}
```
阅读全文