#include <iostream> using namespace std; #include <iostream> #include <cstring> using namespace std; class Student { /********* Begin *********/ //在此处声明Student类 private: int age; string name; public: Student(int m,string n); Student( ); void SetInfo(int m,string n); int getAge(); string getName(); /********* End *********/ }; /********* Begin *********/ //在此处定义Student类 Student::Student(int m,string n) { age=m; name=n; } Student::Student( ) { age=0; name="No"; } void Student::SetInfo(int m,string n) { age=m; name=n; } int Student::getAge() { return age; } string Student::getName() { return name; } /********* End *********/
时间: 2023-07-18 14:50:58 浏览: 88
这是一个简单的 C++ 程序,定义了一个名为 Student 的类。类中有两个私有成员变量 age 和 name,以及四个公有成员函数:构造函数 Student(int m, string n)、无参构造函数 Student()、SetInfo(int m, string n) 和 getAge()、getName()。构造函数用于初始化对象,无参构造函数默认初始化 age 为 0,name 为 "No"。SetInfo() 函数用于设置学生的信息,包括年龄和姓名。getAge() 和 getName() 函数分别用于获取学生的年龄和姓名。
相关问题
#include<iostream> #include<cstring> using namespace std; class Student { int age; char *name; public: Student ( ):age(18) { name = new char[10]; strcpy(name,"noName"); cout<<"default construct "<<endl; } Student (int a , char *str) { A-4 name = new char[strlen(str)+1]; strcpy(name,str); age= a>0 ? a: 0; cout<<"construct "<<name<<endl; }~ Student () { cout<<"destruct "<<name<<endl; if(name )delete[] name; } }; int main() { Student * pp = new Student (18,"Zhang"); Student p; delete pp; return 0输出什么
这段代码的输出应该是:
```
construct Zhang
default construct
destruct Zhang
destruct noName
```
首先,通过 `Student * pp = new Student (18,"Zhang");` 创建了一个名为 pp 的指向堆上 Student 对象的指针,并调用了带参数的构造函数,输出了 `construct Zhang`。
然后,通过 `Student p;` 创建了一个名为 p 的 Student 对象,并调用了默认构造函数,输出了 `default construct`。
接着,通过 `delete pp;` 删除了在堆上创建的 Student 对象,调用了析构函数,输出了 `destruct Zhang`。
最后,程序结束时,会自动调用 p 对象的析构函数,输出了 `destruct noName`。
用C语言改一下#include<iostream> #include<cstring> #include<iomanip> using namespace std; struct student{ char name[20]; int num; //学号 int number; //票数 int Class; char major[30]; char grade[10]; char achievement[200]; }; class votesystem{ private: studentst; int count; //当前元素个数 int size; //哈希表长 public: void InitHashTable(int n); //初始化哈希表 int Hash(charname); //计算哈希地址 void collision(int &s); //冲突,计算下一个地址 int search(char name,int &s); //查找 void insert(charname); //插入 int vote(char*name); //投票 void display(); //显示哈希表 void showvote(int k); //按哈希表下标显示票数 void showrank(); //显示排行榜 };
使用C语言改写该代码如下:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME_LEN 20
#define MAX_MAJOR_LEN 30
#define MAX_GRADE_LEN 10
#define MAX_ACHIEVEMENT_LEN 200
#define HASH_TABLE_SIZE 100
typedef struct student {
char name[MAX_NAME_LEN];
int num; //学号
int number; //票数
int Class;
char major[MAX_MAJOR_LEN];
char grade[MAX_GRADE_LEN];
char achievement[MAX_ACHIEVEMENT_LEN];
} student;
typedef struct HashNode {
student st;
int flag; //标记该位置是否被占用
} HashNode;
typedef struct votesystem {
HashNode table[HASH_TABLE_SIZE];
int count; //当前元素个数
} votesystem;
void InitHashTable(votesystem *vs) {
vs->count = 0;
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
vs->table[i].flag = 0;
}
}
int Hash(char *name) {
int hash_val = 0;
for (int i = 0; name[i] != '\0'; i++) {
hash_val = (hash_val * 31 + name[i]) % HASH_TABLE_SIZE;
}
return hash_val;
}
void collision(int *s) {
(*s)++;
if (*s == HASH_TABLE_SIZE) {
*s = 0;
}
}
int search(votesystem *vs, char *name, int *s) {
int hash_val = Hash(name);
while (vs->table[hash_val].flag != 0 && strcmp(vs->table[hash_val].st.name, name) != 0) {
collision(s);
hash_val = Hash(name) + *s;
}
if (vs->table[hash_val].flag == 0) {
return -1; //未找到该元素
} else {
return hash_val;
}
}
void insert(votesystem *vs, student st) {
if (vs->count == HASH_TABLE_SIZE) {
printf("Hash table is full!\n");
} else {
int s = 0;
int hash_val = Hash(st.name);
while (vs->table[hash_val].flag == 1) {
collision(&s);
hash_val = Hash(st.name) + s;
}
vs->table[hash_val].st = st;
vs->table[hash_val].flag = 1;
vs->count++;
}
}
int vote(votesystem *vs, char *name) {
int s = 0;
int hash_val = search(vs, name, &s);
if (hash_val == -1) {
printf("Student %s not found!\n", name);
return -1;
} else {
vs->table[hash_val].st.number++;
return vs->table[hash_val].st.number;
}
}
void display(votesystem *vs) {
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
if (vs->table[i].flag == 1) {
printf("name: %s, num: %d, number: %d, Class: %d, major: %s, grade: %s, achievement: %s\n",
vs->table[i].st.name, vs->table[i].st.num, vs->table[i].st.number,
vs->table[i].st.Class, vs->table[i].st.major, vs->table[i].st.grade,
vs->table[i].st.achievement);
}
}
}
void showvote(votesystem *vs, int k) {
if (k >= 0 && k < HASH_TABLE_SIZE && vs->table[k].flag == 1) {
printf("name: %s, number: %d\n", vs->table[k].st.name, vs->table[k].st.number);
} else {
printf("Invalid index!\n");
}
}
void showrank(votesystem *vs) {
student *tmp_st = (student *) malloc(sizeof(student) * vs->count);
int tmp_count = 0;
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
if (vs->table[i].flag == 1) {
tmp_st[tmp_count++] = vs->table[i].st;
}
}
for (int i = 0; i < tmp_count - 1; i++) {
for (int j = i + 1; j < tmp_count; j++) {
if (tmp_st[i].number < tmp_st[j].number) {
student tmp = tmp_st[i];
tmp_st[i] = tmp_st[j];
tmp_st[j] = tmp;
}
}
}
for (int i = 0; i < tmp_count; i++) {
printf("%d. name: %s, number: %d\n", i + 1, tmp_st[i].name, tmp_st[i].number);
}
free(tmp_st);
}
int main() {
votesystem vs;
InitHashTable(&vs);
student st1 = {"Tom", 1001, 0, 1, "Computer Science", "Grade 1", "Excellent"};
student st2 = {"Jerry", 1002, 0, 1, "Computer Science", "Grade 1", "Good"};
student st3 = {"Bob", 1003, 0, 1, "Computer Science", "Grade 1", "Average"};
insert(&vs, st1);
insert(&vs, st2);
insert(&vs, st3);
vote(&vs, "Tom");
vote(&vs, "Jerry");
vote(&vs, "Tom");
vote(&vs, "Tom");
display(&vs);
showvote(&vs, Hash("Tom"));
showrank(&vs);
return 0;
}
```
阅读全文