Task1
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 }View Code
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 }View Code
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 }View Code
result1
问题一 这个模拟简单GUI的示例代码中,自定义了几个类?使用到了标准库的哪几个类?,哪些类和类之间存在组合关系?
代码中,自定义了两个类Window,Button。使用了标准库的vector,string类。Window和Button之间,Button和vector之间存在着组合关系
问题二 在自定义类Button和Window中,有些成员函数定义时加了const, 有些设置成了inline。如果你是类的设计者,目前那些没有加const或没有设置成inline的,适合添加const,适合设置成inline吗?陈述你的答案和理由。
const用于保护数据,对于那些不需要改变值的成员函数都应该加上const。inline用于提高程序效率,适合修饰简单,常用的函数。
click也可以设置为inline或者const,因为它实际上只是显示了一行字符串。
问题三 类Window的定义中,有这样一行代码,其功能是?
此代码的功能是构成图形界面的边框。
Task2
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 }View Code
result2
问题一 测试1模块中,这三行代码的功能分别是?
line21:创建一个储存int的vector对象,初始化有五个元素,每个元素初始化为42。
line22:用v1的数据初始化v2
line23:将v1第0个元素赋值为-999
问题二 测试2模块中,这三行代码的功能分别是?
line32:创建一个二维vector,第一行1,2,3第二行4,5,6,7
line33:用v1的数据来创建v2
line34:在v1的第0行最后加入-999
问题三 测试2模块中,这四行代码的功能分别是?
line39:用v1第0行的数据来创建一个一维vector
line40:输出其最后一个元素
line42:用v2第0行的数据来创建一个一维vector
line43:输出其最后一个元素
问题四
根据执行结果,反向分析、推断:
① 标准库模板类vector内部封装的复制构造函数,其实现机制是深复制还是浅复制?
② 模板类vector的接口at(), 是否至少需要提供一个const成员函数作为接口?
1.深复制
2.至少需要提供一个const成员函数作为接口。C++设计原则在于不去扩大权限而保证安全。应当先保证const的,再去重载非const的。
Task3
vectorInt.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 // 动态int数组对象类 10 class vectorInt { 11 public: 12 vectorInt(int n); 13 vectorInt(int n, int value); 14 vectorInt(const vectorInt& vi); 15 ~vectorInt(); 16 17 int& at(int index); 18 const int& at(int index) const; 19 20 vectorInt& assign(const vectorInt& v); 21 int get_size() const; 22 23 private: 24 int size; 25 int* ptr; // ptr指向包含size个int的数组 26 }; 27 28 vectorInt::vectorInt(int n) : size{ n }, ptr{ new int[size] } { 29 } 30 31 vectorInt::vectorInt(int n, int value) : size{ n }, ptr{ new int[size] } { 32 for (auto i = 0; i < size; ++i) 33 ptr[i] = value; 34 } 35 36 vectorInt::vectorInt(const vectorInt& vi) :size{ vi.size }, ptr{ new int[size] } { 37 for (auto i = 0; i < size; ++i) 38 ptr[i] = vi.ptr[i]; 39 } 40 41 vectorInt::~vectorInt() { 42 delete[] ptr; 43 } 44 45 const int& vectorInt::at(int index) const { 46 assert(index >= 0 && index < size); 47 48 return ptr[index]; 49 } 50 51 int& vectorInt::at(int index) { 52 assert(index >= 0 && index < size); 53 54 return ptr[index]; 55 } 56 57 vectorInt& vectorInt::assign(const vectorInt& v) { 58 delete[] ptr; // 释放对象中ptr原来指向的资源 59 60 size = v.size; 61 ptr = new int[size]; 62 63 for (int i = 0; i < size; ++i) 64 ptr[i] = v.ptr[i]; 65 66 return *this; 67 } 68 69 int vectorInt::get_size() const { 70 return size; 71 }View Code
task3.cpp
1 #include "vectorInt.hpp" 2 #include <iostream> 3 4 using std::cin; 5 using std::cout; 6 7 void output(const vectorInt& vi) { 8 for (auto i = 0; i < vi.get_size(); ++i) 9 cout << vi.at(i) << ", "; 10 cout << "\b\b \n"; 11 } 12 13 14 void test1() { 15 int n; 16 cout << "Enter n: "; 17 cin >> n; 18 19 vectorInt x1(n); 20 for (auto i = 0; i < n; ++i) 21 x1.at(i) = i * i; 22 cout << "x1: "; output(x1); 23 24 vectorInt x2(n, 42); 25 vectorInt x3(x2); 26 x2.at(0) = -999; 27 cout << "x2: "; output(x2); 28 cout << "x3: "; output(x3); 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 }View Code
result3
问题一vectorInt类中,复制构造函数(line14)的实现,是深复制还是浅复制?
深复制
问题二vectorInt类中,这两个at()接口,如果返回值类型改成int而非int&(相应地,实现部分也同步修改),测试代码还能正确运行吗?如果把line18返回值类型前面的const掉,针对这个测试代码,是否有潜在安全隐患?尝试分析说明。
修改后程序无法运行,expression must be a modifiable lvalue,变成了一个不可修改的左值。
如果把返回值的const去掉的话,会让const对象调用时,变得可以修改值,且在一些低级的编译器中,可能会无法识别出这个错误。
问题三vectorInt类中,assign()接口,返回值类型可以改成vectorInt吗?你的结论,及,原因分析。
在理论上是可以的,程序可以执行。但是,这样会产生额外开销,特别是vector这样的大对象,需要拷贝构造,生成一个临时的对象。
Task4
matrix.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 // 类Matrix的声明 10 class Matrix { 11 public: 12 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵, 初始值为value 13 Matrix(int n); // 构造函数,构造一个n*n的矩阵, 初始值为value 14 Matrix(const Matrix& x); // 复制构造函数, 使用已有的矩阵X构造 15 ~Matrix(); 16 17 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 18 void clear(); // 把矩阵对象的值置0 19 20 const double& at(int i, int j) const; // 返回矩阵对象索引(i,j)的元素const引用 21 double& at(int i, int j); // 返回矩阵对象索引(i,j)的元素引用 22 23 int get_lines() const; // 返回矩阵对象行数 24 int get_cols() const; // 返回矩阵对象列数 25 26 void display() const; // 按行显示矩阵对象元素值 27 28 private: 29 int lines; // 矩阵对象内元素行数 30 int cols; // 矩阵对象内元素列数 31 double* ptr; 32 }; 33 34 Matrix::Matrix(int n, int m) : lines{ n }, cols{ m }, ptr{ new double[lines * cols] } { 35 } 36 37 Matrix::Matrix(int n) : Matrix(n, n) { 38 } 39 40 Matrix::Matrix(const Matrix& x) : lines{ x.lines }, cols{ x.cols }, ptr{ new double[x.lines * x.cols] } { 41 for (auto i = 0; i < lines * cols; ++i) 42 ptr[i] = x.ptr[i]; 43 } 44 45 Matrix::~Matrix() { 46 delete[] ptr; 47 } 48 49 void Matrix::set(const double* pvalue) { 50 for (auto i = 0; i < lines * cols; ++i) 51 ptr[i] = pvalue[i]; 52 } 53 54 void Matrix::clear() { 55 for (auto i = 0; i < lines * cols; ++i) 56 ptr[i] = 0; 57 } 58 59 const double& Matrix::at(int i, int j) const { 60 assert(i >= 0 && i < lines && j >= 0 && j < cols); 61 return ptr[i * cols + j]; 62 } 63 64 double& Matrix::at(int i, int j) { 65 assert(i >= 0 && i < lines && j >= 0 && j < cols); 66 return ptr[i * cols + j]; 67 } 68 69 inline int Matrix::get_lines() const { 70 return lines; 71 } 72 73 inline int Matrix::get_cols() const { 74 return cols; 75 } 76 77 void Matrix::display() const { 78 for (auto newline = 0; newline < lines; ++newline) { 79 for (int i = 0; i < cols;++i) 80 cout << ptr[newline * cols + i] << " "; 81 cout << endl; 82 } 83 }
task4.cpp
1 #include "matrix.hpp" 2 #include <iostream> 3 #include <cassert> 4 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 9 10 const int N = 1000; 11 12 // 输出矩阵对象索引为index所在行的所有元素 13 void output(const Matrix& m, int index) { 14 assert(index >= 0 && index < m.get_lines()); 15 16 for (auto j = 0; j < m.get_cols(); ++j) 17 cout << m.at(index, j) << ", "; 18 cout << "\b\b \n"; 19 } 20 21 void test1() { 22 double x[1000] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 23 24 int n, m; 25 cout << "Enter n and m: "; 26 cin >> n >> m; 27 28 Matrix m1(n, m); // 创建矩阵对象m1, 大小n×m 29 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 30 31 Matrix m2(m, n); // 创建矩阵对象m1, 大小m×n 32 m2.set(x); // 用一维数组x的值按行为矩阵m1赋值 33 34 Matrix m3(2); // 创建一个2×2矩阵对象 35 m3.set(x); // 用一维数组x的值按行为矩阵m4赋值 36 37 cout << "矩阵对象m1: \n"; m1.display(); cout << endl; 38 cout << "矩阵对象m2: \n"; m2.display(); cout << endl; 39 cout << "矩阵对象m3: \n"; m3.display(); cout << endl; 40 } 41 42 void test2() { 43 Matrix m1(2, 3); 44 m1.clear(); 45 46 const Matrix m2(m1); 47 m1.at(0, 0) = -999; 48 49 cout << "m1.at(0, 0) = " << m1.at(0, 0) << endl; 50 cout << "m2.at(0, 0) = " << m2.at(0, 0) << endl; 51 cout << "矩阵对象m1第0行: "; output(m1, 0); 52 cout << "矩阵对象m2第0行: "; output(m2, 0); 53 } 54 55 int main() { 56 cout << "测试1: \n"; 57 test1(); 58 59 cout << "测试2: \n"; 60 test2(); 61 }View Code
result4
Task5
User.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <vector> 5 #include <iomanip> 6 7 using std::string; 8 using std::cout; 9 using std::cin; 10 using std::endl; 11 using std::setw; 12 using std::left; 13 14 class User { 15 public: 16 User(string user_name, string user_password = "123456", string user_email = ""); 17 ~User() = default; 18 19 void set_email(); 20 void change_password(); 21 void display() const; 22 23 private: 24 string name; 25 string password; 26 string email; 27 }; 28 29 User::User(string user_name, string user_password, string user_email) { 30 name = user_name; 31 password = user_password; 32 email = user_email; 33 } 34 35 void User::set_email() { 36 string temp; 37 cout << "Enter email address: "; 38 39 while (cin >> temp) { 40 if (temp.find('@') != std::string::npos) { 41 cout << "email is set successfully..." << endl; 42 email = temp; 43 break; 44 } 45 else { 46 cout << "illegal email. Please re-enter email: "; 47 } 48 } 49 } 50 51 void User::change_password() { 52 string temp; 53 int count = 0; 54 55 cout << "Enter old password: "; 56 while (cin >> temp) { 57 58 59 if (temp == password) { 60 cout << "Enter new password: "; 61 cin >> temp; 62 password = temp; 63 cout << "new password is set successfully..." << endl; 64 break; 65 } 66 else { 67 cout << "password input error. Please re-enter again "; 68 count++; 69 if (count >= 3) { 70 cout << "password input error. Please try after a while." << endl; 71 break; 72 } 73 } 74 } 75 } 76 77 void User::display() const { 78 cout << setw(8) << left << "name: " << name << endl; 79 80 string show_password; 81 for (auto i = 0; i < password.size(); ++i) show_password += '*'; 82 cout << setw(8) << left << "pass: " << show_password << endl; 83 84 cout << setw(8) << left << "email: " << email << endl; 85 86 }
task5.cpp
1 #include "user.hpp" 2 #include <iostream> 3 #include <vector> 4 #include <string> 5 6 using std::cin; 7 using std::cout; 8 using std::endl; 9 using std::vector; 10 using std::string; 11 12 void test() { 13 vector<User> user_lst; 14 15 User u1("Alice", "2024113", "[email protected]"); 16 user_lst.push_back(u1); 17 cout << endl; 18 19 User u2("Bob"); 20 u2.set_email(); 21 u2.change_password(); 22 user_lst.push_back(u2); 23 cout << endl; 24 25 User u3("Hellen"); 26 u3.set_email(); 27 u3.change_password(); 28 user_lst.push_back(u3); 29 cout << endl; 30 31 cout << "There are " << user_lst.size() << " users. they are: " << endl; 32 for (auto& i : user_lst) { 33 i.display(); 34 cout << endl; 35 } 36 } 37 int main() { 38 test(); 39 }View Code
result5
Task6
date.hpp
1 #pragma once 2 3 #include<iostream> 4 #include<cstdlib> 5 6 using namespace std; 7 class Date { 8 private: 9 int year; 10 int month; 11 int day; 12 int totalDays; 13 public: 14 Date(int year, int month, int day); 15 int getYear()const { 16 return year; 17 } 18 int getMonth()const { 19 return month; 20 } 21 int getDay()const { 22 return day; 23 } 24 int getMaxDay()const; 25 bool isLeapYear()const { 26 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 27 } 28 void show() const; 29 int distance(const Date& date)const { 30 return totalDays - date.totalDays; 31 } 32 }; 33 namespace { 34 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 35 } 36 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 37 if (day <= 0 || day > getMaxDay()) { 38 cout << "Invalid date: "; 39 show(); 40 cout << endl; 41 exit(1); 42 } 43 int years = year - 1; 44 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 45 if (isLeapYear() && month > 2) totalDays++; 46 } 47 int Date::getMaxDay()const { 48 if (isLeapYear() && month == 2) 49 return 29; 50 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 51 } 52 void Date::show()const { 53 cout << getYear() << "-" << getMonth() << "-" << getDay(); 54 }View Code
account.hpp
1 #pragma once 2 3 #include"date.hpp" 4 #include<string> 5 #include<cmath> 6 #include<iostream> 7 8 using namespace std; 9 10 class SavingsAccount { 11 private: 12 string id; 13 double balance; 14 double rate; 15 Date lastDate; 16 double accumulation; 17 static double total; 18 void record(const Date& date, double amount, const string& desc); 19 void error(const string& msg) const; 20 double accumulate(const Date& date)const { 21 return accumulation + balance * date.distance(lastDate); 22 } 23 public: 24 SavingsAccount(const Date& date, const string& id, double rate); 25 const string& getId()const { 26 return id; 27 } 28 double getBalance()const { 29 return balance; 30 } 31 double getRate()const { 32 return rate; 33 } 34 static double getTotal() { 35 return total; 36 } 37 void deposit(const Date& date, double amount, const string& desc); 38 void withdraw(const Date& date, double amount, const string& desc); 39 void settle(const Date& date); 40 void show() const; 41 }; 42 double SavingsAccount::total = 0; 43 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) : 44 id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 45 date.show(); 46 cout << "\t#" << id << "created" << endl; 47 } 48 void SavingsAccount::record(const Date& date, double amount, const string& desc) { 49 accumulation = accumulate(date); 50 lastDate = date; 51 amount = floor(amount * 100 + 0.5) / 100; 52 balance += amount; 53 total += amount; 54 date.show(); 55 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 56 } 57 void SavingsAccount::error(const string& msg) const { 58 cout << "Error(#" << id << "):" << msg << endl; 59 } 60 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 61 record(date, amount, desc); 62 } 63 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 64 if (amount > getBalance()) 65 error("not enough money"); 66 else 67 record(date, -amount, desc); 68 } 69 void SavingsAccount::settle(const Date& date) { 70 double interest = accumulate(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 71 if (interest != 0) record( date,interest,"interest" ); 72 accumulation = 0; 73 } 74 void SavingsAccount::show()const { 75 cout << id << "\tBalance: " << balance; 76 }View Code
result6
标签:const,string,对象,编程,int,实验,vectorInt,include,cout From: https://www.cnblogs.com/nuist0415/p/18525597