实验3 c++

任务一:

button.hpp:

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
#pragma once
#include "button.hpp"
#include <vector>
#include <iostream>
using std::vector;
using std::cout;
using std::endl;
// 窗口类
class Window {
public:
    Window(const string& win_title);
    void display() const;
    void close();
    void add_button(const string& label);
private:
    string title;
    vector<Button> buttons;
};
Window::Window(const string& win_title) : title{ win_title } {
    buttons.push_back(Button("close"));
}
inline void Window::display() const {
    string s(40, '*');
    cout << s << endl;
    cout << "window title: " << title << endl;
    cout << "It has " << buttons.size() << " buttons: " << endl;
    for (const auto& i : buttons)
        cout << i.get_label() << " button" << endl;
    cout << s << endl;
}
void Window::close() {
    cout << "close window '" << title << "'" << endl;
    buttons.at(0).click();
}
void Window::add_button(const string& label) {
    buttons.push_back(Button(label));
}

  

window.hpp:

 

  

task1.cpp:

 

  

实验结果截图:

 问题一:自定义了两个类使用了标准库的iostream,string,vector类,button类和iostream,string组合,window类和button,iostream,vector类组合

问题二:如果成员函数只是访问数据而不改变数据,就可以加上const来增加代码的安全性。如果函数简单并且调用频繁,则可以加上inline来提高性能

问题三:代码的功能是生成一条分割线,可用于装饰和视觉辅助。

 

任务二:

task2.cpp:

实验结果截图:

问题一:创建一个整数向量v1,大小为5,所有元素初始化为42,创建向量v2,复制v1内容,并把v1的第一个元素改为-999。

问题二:创建一个二维向量v1,第一行为1,2,3、第二行为4、5、6、7,后创建一个v1的副本v2,最后在v1第一行的结尾插入-999

问题三:创建一个一维向量t1,拷贝v1第一行的内容,并输出t1的最后一个元素,同理t2。

问题四:vector内部封装的复制构造函数是深复制,为每个元素提供独立的副本。需要提供一个const成员函数作为接口,供const vector使用。

 

任务三:

vectorInt.hpp:

 

task3.cpp:

实验结果截图:

 问题一:是深复制,为新的代码对象分配了新的内存空间。

问题二:如果返回值改为int无法正确运行测试代码,因为无法通过at修改元素了,有潜在安全隐患,外部代码可能会修改内部数据。

问题三:不可以,如果返回值类型是 vectorint 而不是引用,则每次调用时,都会创建一个vectorint对象的副本。这意味着在函数返回之前,会发生一个完整的对象复制,对性能有显著影响。

任务四:

matrix.hpp:

  

task4.cpp:

  

实验结果截图:

任务五:

user.hpp:

  

task5.cpp:

  

实验结果截图:

任务六:

date.h:

  

date.cpp:

  

account.h:

  

account.cpp:

  

6_25.cpp:

  

实验结果截图:

 

posted @   观者如云  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示