实验一:类和对象
定义一个学生类 Student,学生类中有 3 个私有数据成员:name(姓名)、cls(班级)、
grade(年级);3 个公有成员函数;Student(构造函数)、display、~Student(析构函数)。使
用构造函数为 Student 类的对象赋值(name 使用动态内存分配空间),display 负责显示学生
的基本信息,在析构函数中释放动态分配的内存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <iostream> #include <cstring>
using namespace std;
class Students { private: char* name; int cls; int grade; public: Students(const char* n, int c, int g) { name = new char[strlen(n) + 1]; strcpy(name, n); cls = c; grade = g; }
void display() const { cout << "name=" << name << endl; cout << "cls=" << cls << endl; cout << "grade=" << grade << endl; } ~Students() { delete[] name; }
};
int main() { Students a("xiaomin", 3, 3); a.display(); return 0; } system("pause");
|
实验二:继承和派生
编写一个学生和教师数据输入和显示程序。其中,学生数据有编号、姓名、班级和成 绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类 Person; 然后设计类 Person 的派生类:学生类 Student 和教师类 Teacher;编写一个主函数,通过定 义 student、teacher 的对象,完成相应功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <iostream> using namespace std;
class Person { protected: int id; string name; public: void input() { cout << "请输入编号:"; cin >> id; cout << "请输入姓名:"; cin >> name; } void display() { cout << "编号:" << id << endl; cout << "姓名:" << name << endl; } };
class Student : public Person { protected: string className; int score; public: void input() { Person::input(); cout << "请输入班级:"; cin >> className; cout << "请输入成绩:"; cin >> score; } void display() { Person::display(); cout << "班级:" << className << endl; cout << "成绩:" << score << endl; } };
class Teacher : public Person { protected: string title; string department; public: void input() { Person::input(); cout << "请输入职称:"; cin >> title; cout << "请输入部门:"; cin >> department; } void display() { Person::display(); cout << "职称:" << title << endl; cout << "部门:" << department << endl; } };
int main() { Student student; Teacher teacher; cout << "请输入学生信息:" << endl; student.input(); cout << "请输入教师信息:" << endl; teacher.input(); cout << "学生信息如下:" << endl; student.display(); cout << "教师信息如下:" << endl; teacher.display(); return 0; }
|
实验四
以现实中银行卡为原型,进行需求调查,抽象出相应的类,设计类的成员函数和数据成员,完成银行账户管理系统。编写程序,帮助工作人员有效、准确并且高校的完成存取事件。(系统主要完成开户管理,存取钱管理,修改密码,注销等功能)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| #include <iostream> #include <string>
using namespace std;
struct User { string username; int account_number; string password; bool is_active; };
User users[10]; int user_count = 0;
void add_user() { if (user_count >= 10) { cout << "用户数已达上限!" << endl; return; } User user; cout << "请输入用户名:"; cin >> user.username; cout << "请输入账户号码:"; cin >> user.account_number; cout << "请输入密码:"; cin >> user.password; cout << "请输入是否激活(1表示激活,0表示未激活):"; cin >> user.is_active; users[user_count] = user; user_count++; cout << "添加成功!" << endl; }
User find_user(string username) { for (int i = 0; i < user_count; i++) { if (users[i].username == username) { return users[i]; } } return User(); }
void deposit() { string username, password; int amount; cout << "请输入存款账户号码:"; cin >> username; User user = find_user(username); if (user.username == "") { cout << "账户不存在!" << endl; return; } cout << "请输入存款金额:"; cin >> amount; cout << "请输入密码:"; cin >> password; if (user.password != password) { cout << "密码错误!" << endl; return; } user.account_number += amount; cout << "存款成功!账户余额为:" << user.account_number << endl; }
void withdraw() { string username, password; int amount; cout << "请输入取款账户号码:"; cin >> username; User user = find_user(username); if (user.username == "") { cout << "账户不存在!" << endl; return; } cout << "请输入取款金额:"; cin >> amount; if (amount > user.account_number) { cout << "取款失败!账户余额不足!" << endl; return; } cout << "请输入密码:"; cin >> password; if (user.password != password) { cout << "密码错误!" << endl; return; } user.account_number -= amount; cout << "取款成功!账户余额为:" << user.account_number << endl; }
void change_password() { string username, old_password, new_password; cout << "请输入账户号码:"; cin >> username; User user = find_user(username); if (user.username == "") { cout << "账户不存在!" << endl; return; } cout << "请输入旧密码:"; cin >> old_password; if (user.password != old_password) { cout << "密码错误!" << endl; return; } cout << "请输入新密码:"; cin >> new_password; user.password = new_password; cout << "密码修改成功!" << endl; }
int main() { int choice; do { cout << "请选择要进行的操作:" << endl; cout << "1. 添加用户" << endl; cout << "2. 存款" << endl; cout << "3. 取款" << endl; cout << "4. 修改密码" << endl; cout << "5. 退出" << endl; cin >> choice;
switch (choice) { case 1: add_user(); break; case 2: deposit(); break; case 3: withdraw(); break; case 4: change_password(); break; case 5: cout << "谢谢使用!" << endl; break; default: cout << "无效的选择,请重新输入。" << endl; break; } } while (choice != 5);
return 0; }
|