Skip to content
Snippets Groups Projects
Commit 45ff90b7 authored by Iori Ichinose's avatar Iori Ichinose :speech_balloon:
Browse files

Merge branch 'master' of git.nju.edu.cn:iori_ichinose/nju-ap2021

parents f1ccbbfa 6f8a128b
Branches master
No related tags found
No related merge requests found
......@@ -26,5 +26,5 @@ add_executable(hw65 homework/hw65.cpp)
add_executable(hw75 homework/hw75.cpp)
add_executable(hw81 homework/hw81.cpp)
add_executable(hw82 homework/hw82.cpp)
add_executable(hw101 homework/hw101.cpp)
add_executable(hw102 homework/hw102.cpp)
add_executable(hw101 homework/hw10/1.cpp)
add_executable(hw102 homework/hw10/2.cpp homework/hw10/main.cpp)
File moved
#include <algorithm>
#include <cctype>
#include <cstring>
#include <exception>
#include <fstream>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include "2.h"
class MyException : public std::exception {
private:
std::string msg;
public:
MyException(std::string msg) : msg(std::move(msg)) {}
const char *what() const noexcept override { return msg.c_str(); }
};
class User {
private:
char ID[11];
char password[16];
public:
User() = default;
User(const char *, const char *);
void createUsers(const char *, const char *);
void varifyUsers();
};
User::User(const char *ID, const char *password) {
User::User(const char* ID, const char* password) {
std::strcpy(this->ID, ID);
std::strcpy(this->password, password);
}
void User::createUsers(const char *ID, const char *password) {
void User::createUsers(const char* ID, const char* password) {
auto f = std::ofstream("user.dat", std::ios_base::app);
if (!f.is_open()) {
throw MyException("File cannot open!!!");
......@@ -79,15 +49,3 @@ void User::varifyUsers() {
}
throw MyException(ID);
}
int main() {
auto u = User{};
try {
u.createUsers("_t1145142", "1919810");
u.varifyUsers();
} catch (const MyException &msg) {
std::cout << msg.what();
} catch (const std::exception &e) {
std::cout << e.what();
}
}
#pragma once
#include <cstring>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
class MyException : public std::exception {
private:
std::string msg;
public:
MyException(std::string msg) : msg(std::move(msg)) {}
const char* what() const noexcept override { return msg.c_str(); }
};
class User {
private:
char ID[11];
char password[16];
public:
User() = default;
User(const char*, const char*);
void createUsers(const char*, const char*);
void varifyUsers();
};
1. 应该按照从子类到父类的顺序进行。因为`catch`异常时,使用父类异常可以捕获到子类异常,而反过来则不行。如果先捕捉父类,则无法再捕捉到子类异常。
2. 用途
1. `static_cast`:静态类型转换,可用于进行强制隐式转换。例如非const对象转为const对象,long转为int等。但不具备动态类型检查,因此向下转换是不安全的;`static_cast`不能用于去除变量的const、volatile等属性。
2. `dynamic_cast`:动态类型转换,可以将一个基类指针或引用,如果转换失败会抛出`std::bad_cast`异常。由于具有类型检查,故比`static_cast`更加安全,但具有更大的运行时消耗。
3. `const_cast`:用于消除变量的`const`或者`volatile`属性,但滥用可能会导致未定义行为(例如将字符串常量强制转为char类型指针)。
4. `reinterpret_cast`:用于将一个指针强制转为整形,可移植性较弱。例如将`void*`类型强转为`int*`等。
3. 当转换目标与被转换的对象不是父子类关系时,或者转换双方不都是指针或不都是引用时,或转换目标不具备虚函数表时,会抛出`std::bad_cast`异常。是通过指针指向的对象的虚函数表决定的。当通过虚函数表可以确定转换对象类型符合时即可安全的转换。可以通过try-catch块确保正常的转换。
\ No newline at end of file
#include "2.h"
int main() {
auto u = User{};
try {
u.createUsers("_t1145142", "1919810");
u.varifyUsers();
} catch (const MyException& msg) {
std::cout << msg.what();
} catch (const std::exception& e) {
std::cout << e.what();
}
}
## 逻辑结构
1. 新建一个用户实例,并调用`createUser`设置用户名与密码,如果用户名或密码不合理,则抛出异常;新建后
2. 对上述实例调用`varifyUser`验证密码是否正确。
3. 重新运行程序,直接通过用户名与密码新建实例,并验证是否满足已注册用户;若不满足
## 异常处理方式
1. `createUser`:中止新建过程,并告知用户。
2. `varifyUser`:提示用户密码错误,请用户重新输入。
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment