任务1
button.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using std::string; 7 using std::cout; 8 9 // 按钮类 10 class Button { 11 public: 12 Button(const string &text); 13 string get_label() const; 14 void click(); 15 16 private: 17 string label; 18 }; 19 20 Button::Button(const string &text): label{text} { 21 } 22 23 inline string Button::get_label() const { 24 return label; 25 } 26 27 void Button::click() { 28 cout << "Button '" << label << "' clicked\n"; 29 }
window.hpp
1 #pragma once 2 #include "button.hpp" 3 #include <vector> 4 #include <iostream> 5 6 using std::vector; 7 using std::cout; 8 using std::endl; 9 10 // 窗口类 11 class Window{ 12 public: 13 Window(const string &win_title); 14 void display() const; 15 void close(); 16 void add_button(const string &label); 17 18 private: 19 string title; 20 vector<Button> buttons; 21 }; 22 23 Window::Window(const string &win_title): title{win_title} { 24 buttons.push_back(Button("close")); 25 } 26 27 inline void Window::display() const { 28 string s(40, '*'); 29 30 cout << s << endl; 31 cout << "window title: " << title << endl; 32 cout << "It has " << buttons.size() << " buttons: " << endl; 33 for(const auto &i: buttons) 34 cout << i.get_label() << " button" << endl; 35 cout << s << endl; 36 } 37 38 void Window::close() { 39 cout << "close window '" << title << "'" << endl; 40 buttons.at(0).click(); 41 } 42 43 void Window::add_button(const string &label) { 44 buttons.push_back(Button(label)); 45 }
task1.cpp
1 #include "window.hpp" 2 #include <iostream> 3 4 using std::cout; 5 using std::cin; 6 7 void test() { 8 Window w1("new window"); 9 w1.add_button("maximize"); 10 w1.display(); 11 w1.close(); 12 } 13 14 int main() { 15 cout << "用组合类模拟简单GUI:\n"; 16 test(); 17 }
运行结果:
问题:
问题1:这个模拟简单GUI的示例代码中,自定义了几个类?使用到了标准库的哪几个类?,哪些 类和类之间存在组合关系?
答:自定义了两个类Button和Window。用的了标准库类string和vector。Window和Button类之间存在组合关系。
问题2:在自定义类Button和Window中,有些成员函数定义时加了const, 有些设置成了inline。如 果你是类的设计者,目前那些没有加const或没有设置成inline的,适合添加const,适合设置成 inline吗?陈述你的答案和理由。
适合添加const,因为它不会修改类的状态。适合添加inline,实现简单,代码量少。
问题3:类Window的定义中,有这样一行代码,其功能是?
创建了一个名为s的string对象,初始化又40个‘*’字符组成的字符串
任务2
task2.cpp
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 void output1(const vector<int> &v) { 7 for(auto &i: v) 8 cout << i << ", "; 9 cout << "\b\b \n"; 10 } 11 12 void output2(const vector<vector<int>> v) { 13 for(auto &i: v) { 14 for(auto &j: i) 15 cout << j << ", "; 16 cout << "\b\b \n"; 17 } 18 } 19 20 void test1() { 21 vector<int> v1(5, 42); 22 const vector<int> v2(v1); 23 24 v1.at(0) = -999; 25 cout << "v1: "; output1(v1); 26 cout << "v2: "; output1(v2); 27 cout << "v1.at(0) = " << v1.at(0) << endl; 28 cout << "v2.at(0) = " << v2.at(0) << endl; 29 } 30 31 void test2() { 32 vector<vector<int>> v1{{1, 2, 3}, {4, 5, 6, 7}}; 33 const vector<vector<int>> v2(v1); 34 35 v1.at(0).push_back(-999); 36 cout << "v1: \n"; output2(v1); 37 cout << "v2: \n"; output2(v2); 38 39 vector<int> t1 = v1.at(0); 40 cout << t1.at(t1.size()-1) << endl; 41 42 const vector<int> t2 = v2.at(0); 43 cout << t2.at(t2.size()-1) << endl; 44 } 45 46 int main() { 47 cout << "测试1:\n"; 48 test1(); 49 50 cout << "\n测试2:\n"; 51 test2(); 52 }
运行结果:
任务3
vectorlnt.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 // 动态int数组对象类 9 class vectorInt{ 10 public: 11 vectorInt(int n); 12 vectorInt(int n, int value); 13 vectorInt(const vectorInt &vi); 14 ~vectorInt(); 15 16 int& at(int index); 17 const int& at(int index) const; 18 19 vectorInt& assign(const vectorInt &v); 20 int get_size() const; 21 private: 22 int size; 23 int *ptr; // ptr指向包含size个int的数组 24 }; 25 vectorInt::vectorInt(int n): size{n}, ptr{new int[size]} { 26 } 27 vectorInt::vectorInt(int n, int value): size{n}, ptr{new int[size]} { 28 for(auto i = 0; i < size; ++i) 29 ptr[i] = value; 30 vectorInt::vectorInt(const vectorInt &vi): size{vi.size}, ptr{new int[size]} { 31 for(auto i = 0; i < size; ++i) 32 ptr[i] = vi.ptr[i]; 33 } 34 vectorInt::~vectorInt() { 35 delete [] ptr; 36 } 37 38 const int& vectorInt::at(int index) const { 39 assert(index >= 0 && index < size); 40 41 return ptr[index]; 42 } 43 44 int& vectorInt::at(int index) { 45 assert(index >= 0 && index < size); 46 47 return ptr[index]; 48 } 49 vectorInt& vectorInt::assign(const vectorInt &v) { 50 delete[] ptr; // 释放对象中ptr原来指向的资源 51 52 size = v.size; 53 ptr = new int[size]; 54 55 for(int i = 0; i < size; ++i) 56 ptr[i] = v.ptr[i]; 57 58 return *this; //this现在指向正在初始化的对象,*this即为这个对象 59 } 60 61 int vectorInt::get_size() const { 62 return size; 63 }
task3.cpp
1 #include "vectorInt.hpp" 2 #include <iostream> 3 4 using std::cin; 5 using std::cout; 6 void output(const vectorInt &vi) { 7 for(auto i = 0; i < vi.get_size(); ++i) 8 cout << vi.at(i) << ", "; 9 cout << "\b\b \n"; 10 } 11 12 13 void test1() { 14 int n; 15 cout << "Enter n: "; 16 cin >> n; 17 18 vectorInt x1(n); 19 for(auto i = 0; i < n; ++i) 20 x1.at(i) = i*i; 21 cout << "x1: "; output(x1); 22 23 vectorInt x2(n, 42); 24 vectorInt x3(x2); 25 x2.at(0) = -999; 26 cout << "x2: "; output(x2); 27 cout << "x3: "; output(x3); 28 } 29 30 31 void test2() { 32 const vectorInt x(5, 42); 33 vectorInt y(10, 0); 34 35 cout << "y: "; output(y); 36 y.assign(x); 37 cout << "y: "; output(y); 38 39 cout << "x.at(0) = " << x.at(0) << endl; 40 cout << "y.at(0) = " << y.at(0) << endl; 41 } 42 43 int main() { 44 cout << "测试1: \n"; 45 test1(); 46 47 cout << "\n测试2: \n"; 48 test2(); 49 }
运行结果截图:
问题:
1.深复制
2.不能,会有潜在安全隐患
3.返回值类型为 vectorInt& 时:返回当前对象的引用 *this,意味着调用 assign() 后,调用者获得的是调用对象本身。这允许方法链调用
标签:const,cout,int,实验,vectorInt,ptr,size From: https://www.cnblogs.com/rxt711/p/18525747