实验三
实验2
实验任务一 :
button.hpp:
#pragma once
#include <iostream>
#include <string>
class Button {
public:
Button(const std::string &label_);
const std::string& get_label() const;
void click();
private:
std::string label;
};
Button::Button(const std::string &label_): label{label_} {
}
inline const std::string& Button::get_label() const {
return label;
}
inline void Button::click() {
std::cout << "Button '" << label << "' clicked\n";
}
window.hpp:
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include "button.hpp"
// 窗口类
class Window{
public:
Window(const std::string &title_);
void display() const;
void close();
void add_button(const std::string &label);
void click_button(const std::string &label);
private:
bool has_button(const std::string &label) const;
private:
std::string title;
std::vector<Button> buttons;
};
Window::Window(const std::string &title_): title{title_} {
buttons.push_back(Button("close"));
}
inline void Window::display() const {
std::string s(40, '*');
std::cout << s << std::endl;
std::cout << "window : " << title << std::endl;
int cnt = 0;
for(const auto &button: buttons)
std::cout << ++cnt << ". " << button.get_label() << std::endl;
std::cout << s << std::endl;
}
inline void Window::close() {
std::cout << "close window '" << title << "'" << std::endl;
click_button("close");
}
inline bool Window::has_button(const std::string &label) const {
for(const auto &button: buttons)
if(button.get_label() == label)
return true;
return false;
}
inline void Window::add_button(const std::string &label) {
if(has_button(label))
std::cout << "button " << label << " already exists!\n";
else
buttons.push_back(Button(label));
}
inline void Window::click_button(const std::string &label) {
for(auto &button:buttons)
if(button.get_label() == label) {
button.click();
return;
}
std::cout << "no button: " << label << std::endl;
}
task1.cpp:
#include "window.hpp"
#include <iostream>
void test(){
Window w("Demo");
w.add_button("add");
w.add_button("remove");
w.add_button("modify");
w.add_button("add");
w.display();
w.close();
}
int main() {
std::cout << "用组合类模拟简单GUI:\n";
test();
}
运行截图:

问题1
1.是组合关系
2.(1) 优点是设置成public之后可在类外使用has_button;风险这个类的封闭性下降,可能有安全风险。
(2)public主要是面向类外程序使用,private是类内方法使用
3.接口一性能更好,少了一次拷贝,但是因为返回的是引用的原因,具有风险性。
4.能运行,修改完以后的运行效率更高
实验任务2
task2.cpp:
#include <iostream>
#include <vector>
void test1();
void test2();
void output1(const std::vector<int> &v);
void output2(const std::vector<int> &v);
void output3(const std::vector<std::vector<int>>& v);
int main() {
std::cout << "深复制验证1: 标准库vector<int>\n";
test1();
std::cout << "\n深复制验证2: 标准库vector<int>嵌套使用\n";
test2();
}
void test1() {
std::vector<int> v1(5, 42);
const std::vector<int> v2(v1);
std::cout << "**********拷贝构造后**********\n";
std::cout << "v1: "; output1(v1);
std::cout << "v2: "; output1(v2);
v1.at(0) = -1;
std::cout << "**********修改v1[0]后**********\n";
std::cout << "v1: "; output1(v1);
std::cout << "v2: "; output1(v2);
}
void test2() {
std::vector<std::vector<int>> v1{{1, 2, 3}, {4, 5, 6, 7}};
const std::vector<std::vector<int>> v2(v1);
std::cout << "**********拷贝构造后**********\n";
std::cout << "v1: "; output3(v1);
std::cout << "v2: "; output3(v2);
v1.at(0).push_back(-1);
std::cout << "**********修改v1[0]后**********\n";
std::cout << "v1: \n"; output3(v1);
std::cout << "v2: \n"; output3(v2);
}
void output1(const std::vector<int> &v) {
if(v.size() == 0) {
std::cout << '\n';
return;
}
std::cout << v.at(0);
for(auto i = 1; i < v.size(); ++i)
std::cout << ", " << v.at(i);
std::cout << '\n';
}
void output2(const std::vector<int> &v) {
if(v.size() == 0) {
std::cout << '\n';
return;
}
auto it = v.begin();
std::cout << *it;
for(it = v.begin()+1; it != v.end(); ++it)
std::cout << ", " << *it;
std::cout << '\n';
}
void output3(const std::vector<std::vector<int>>& v) {
if(v.size() == 0) {
std::cout << '\n';
return;
}
for(auto &i: v)
output2(i);
}
运行截图:

问题
1.第一行完成了带有参数的函数构造,第二行完成了拷贝函数的构造。v1与v2均含有5个42
2.三者分别为2,2,3
3.更改后可以实现相同效果
原本的代码中at()函数会做范围检查,越界时会抛出异常,这也会带来一点性能开销,修改后的代码性能会好一点。
4.
(1)不可以,at返回的时int&类型,无法通过编译
(2)优势是内存小,缺点是const函数限制修改返回参数
5.(1)深复制
(2)分别返回int&,const int&;at()必须携带const的重载函数
实验任务三
task3.cpp:
#pragma once
#include <iostream>
class vectorInt{
public:
vectorInt();
vectorInt(int n_);
vectorInt(int n_, int value);
vectorInt(const vectorInt &vi);
~vectorInt();
int size() const;
int& at(int index);
const int& at(int index) const;
vectorInt& assign(const vectorInt &vi);
int* begin();
int* end();
const int* begin() const;
const int* end() const;
private:
int n;
int *ptr;
};
vectorInt::vectorInt():n{0}, ptr{nullptr} {
}
vectorInt::vectorInt(int n_): n{n_}, ptr{new int[n]} {
}
vectorInt::vectorInt(int n_, int value): n{n_}, ptr{new int[n_]} {
for(auto i = 0; i < n; ++i)
ptr[i] = value;
}
vectorInt::vectorInt(const vectorInt &vi): n{vi.n}, ptr{new int[n]} {
for(auto i = 0; i < n; ++i)
ptr[i] = vi.ptr[i];
}
vectorInt::~vectorInt() {
delete [] ptr;
}
int vectorInt::size() const {
return n;
}
const int& vectorInt::at(int index) const {
if(index < 0 || index >= n) {
std::cerr << "IndexError: index out of range\n";
std::exit(1);
}
return ptr[index];
}
int& vectorInt::at(int index) {
if(index < 0 || index >= n) {
std::cerr << "IndexError: index out of range\n";
std::exit(1);
}
return ptr[index];
}
vectorInt& vectorInt::assign(const vectorInt &vi) {
if(this == &vi)
return *this;
int *ptr_tmp;
ptr_tmp = new int[vi.n];
for(int i = 0; i < vi.n; ++i)
ptr_tmp[i] = vi.ptr[i];
delete[] ptr;
n = vi.n;
ptr = ptr_tmp;
return *this;
}
int* vectorInt::begin() {
return ptr;
}
int* vectorInt::end() {
return ptr+n;
}
const int* vectorInt::begin() const {
return ptr;
}
const int* vectorInt::end() const {
return ptr+n;
}
运行截图:

问题
1.缺失了检查是否是自身的代码,后面赋值时使用已经被delete了的空间,在传入自身时程序会崩溃
2.(1)将非const类型的this指针转换为const类型的vectorint*
转换之前是vectorint,转换后是const vectorint;目的是避免跨界访问
(2)去除const,之前是const int&,修改后变成int&;目的是删除const使得参数引用允许被修改
3.v1调用int;v2调用const int;v1适用于需要修改的,v2适用于不需要修改的
4.可以,用于初始化,深复制,保护释放内存时v1的安全
实验任务4:
matrix.hpp
#pragma once
#include <iostream>
#include <algorithm>
#include <cstdlib>
// 类Matrix声明
class Matrix {
public:
Matrix(int rows_, int cols_, double value = 0);
Matrix(int rows_, double value = 0);
Matrix(const Matrix &x);
~Matrix();
void set(const double *pvalue, int size);
void clear();
const double& at(int i, int j) const;
double& at(int i, int j);
int rows() const;
int cols() const;
void print() const;
private:
int n_rows;
int n_cols;
double *ptr;
};
task4.cpp:
#include "matrix.hpp"
#include <iostream>
#include <cstdlib>
Matrix::Matrix(int rows_, int cols_, double value)
: n_rows(rows_), n_cols(cols_), ptr(new double[rows_ * cols_]) {
if (rows_ <= 0 || cols_ <= 0) {
std::exit(1);
}
for (int i = rows_ * cols_-1; i >=0; i--)
ptr[i] = value;
}
Matrix::Matrix(int rows_, double value)
: Matrix(rows_, rows_, value) {}
Matrix::Matrix(const Matrix &x)
: n_rows(x.n_rows), n_cols(x.n_cols), ptr(new double[x.n_rows * x.n_cols]) {
for (int i = rows_ * cols_-1; i >=0; i--)
ptr[i] = x.ptr[i];
}
Matrix::~Matrix() {
delete[] ptr;
}
void Matrix::set(const double *pvalue, int size) {
if (pvalue == nullptr) {
std::exit(1);
}
if (size != n_rows * n_cols) {
std::exit(1);
}
for (int i = 0; i < size; ++i) {
ptr[i] = pvalue[i];
}
}
void Matrix::clear() {
for (int i = 0; i < n_rows * n_cols; ++i) {
ptr[i] = 0.0;
}
}
const double& Matrix::at(int i, int j) const {
if (i < 0 || i >= n_rows || j < 0 || j >= n_cols) {
std::exit(1);
}
return ptr[i * n_cols + j];
}
double& Matrix::at(int i, int j) {
return const_cast<double&>(static_cast<const Matrix*>(this)->at(i, j));
}
int Matrix::rows() const {
return n_rows;
}
int Matrix::cols() const {
return n_cols;
}
void Matrix::print() const {
for (int i = 0; i < n_rows; ++i) {
for (int j = 0; j < n_cols; ++j) {
if (j > 0) {
std::cout << ",";
}
std::cout << ptr[i * n_cols + j];
}
std::cout << "\n";
}
}
运行截图:

实验任务5
#pragma once
#include <iostream>
#include <string>
// 联系人类
class Contact {
public:
Contact(const std::string &name_, const std::string &phone_);
const std::string &get_name() const;
const std::string &get_phone() const;
void display() const;
private:
std::string name;
std::string phone;
};
Contact::Contact(const std::string &name_, const std::string &phone_):name{name_}, phone{phone_} {
}
const std::string& Contact::get_name() const {
return name;
}
const std::string& Contact::get_phone() const {
return phone;
}
void Contact::display() const {
std::cout << name << ", " << phone;
}
# pragma once
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "contact.hpp"
class ContactBook {
public:
void add(const std::string &name, const std::string &phone);
void remove(const std::string &name);
void find(const std::string &name) const;
void display() const;
size_t size() const;
private:
int index(const std::string &name) const;
void sort();
private:
std::vector<Contact> contacts;
};
void ContactBook::add(const std::string &name, const std::string &phone) {
if(index(name) == -1) {
contacts.push_back(Contact(name, phone));
std::cout << name << " add successfully.\n";
sort();
return;
}
std::cout << name << " already exists. fail to add!\n";
}
void ContactBook::remove(const std::string &name) {
int i = index(name);
if(i == -1) {
std::cout << name << " not found, fail to remove!\n";
return;
}
contacts.erase(contacts.begin()+i);
std::cout << name << " remove successfully.\n";
}
void ContactBook::find(const std::string &name) const {
int i = index(name);
if(i == -1) {
std::cout << name << " not found!\n";
return;
}
contacts[i].display();
std::cout << '\n';
}
void ContactBook::display() const {
for(auto &c: contacts) {
c.display();
std::cout << '\n';
}
}
size_t ContactBook::size() const {
return contacts.size();
}
int ContactBook::index(const std::string &name) const {
for (size_t i = 0; i < contacts.size(); ++i) {
if (contacts[i].get_name() == name) {
return static_cast<int>(i);
}
}
return -1;
}
void ContactBook::sort() {
std::sort(contacts.begin(), contacts.end(),
[](const Contact &a, const Contact &b) {
return a.get_name() < b.get_name();
});
}
#include "contactBook.hpp"
void test() {
ContactBook contactbook;
std::cout << "1. add contacts\n";
contactbook.add("Bob", "18199357253");
contactbook.add("Alice", "17300886371");
contactbook.add("Linda", "18184538072");
contactbook.add("Alice", "17300886371");
std::cout << "\n2. display contacts\n";
std::cout << "There are " << contactbook.size() << " contacts.\n";
contactbook.display();
std::cout << "\n3. find contacts\n";
contactbook.find("Bob");
contactbook.find("David");
std::cout << "\n4. remove contact\n";
contactbook.remove("Bob");
contactbook.remove("David");
}
int main() {
test();
}
运行截图:

浙公网安备 33010602011771号