实验二 类和对象(2)
实验任务4
task_4.cpp
1 #include "Complex.hpp" 2 #include <iostream> 3 4 // 类测试 5 void test() { 6 using namespace std; 7 8 Complex c1(6, -8); 9 const Complex c2(9.0); 10 Complex c3(c1); 11 12 cout << "c1 = "; 13 c1.show(); 14 cout << endl; 15 16 cout << "c2 = "; 17 c2.show(); 18 cout << endl; 19 cout << "c2.imag = " << c2.get_imag() << endl; 20 21 cout << "c3 = "; 22 c3.show(); 23 cout << endl; 24 25 cout << "abs(c1) = "; 26 cout << abs(c1) << endl; 27 28 cout << boolalpha; 29 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 30 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 31 32 Complex c4; 33 c4 = add(c1, c2); 34 cout << "c4 = c1 + c2 = "; 35 c4.show(); 36 cout << endl; 37 38 c1.add(c2); 39 cout << "c1 += c2, " << "c1 = "; 40 c1.show(); 41 cout << endl; 42 } 43 44 int main() { 45 test(); 46 }
Complex.hpp
1 #pragma once 2 3 //Complex类的定义 4 #include<iostream> 5 #include<iomanip> 6 7 using std::cout; 8 using std::endl; 9 10 //Complex类的声明 11 12 class Complex { 13 public: 14 Complex(); 15 Complex(double a, double b); 16 Complex(double a); 17 Complex(const Complex& obj); 18 ~Complex() = default; 19 20 //成员函数 21 double get_real() const{ return real; } 22 double get_imag() const{ return imag; } 23 24 void show() const; 25 void add(Complex c2); 26 27 28 //友元函数 29 friend Complex add(Complex c1, Complex c2); 30 31 friend bool is_equal(Complex c1, Complex c2); 32 33 friend double abs(Complex c1); 34 private: 35 double real; 36 double imag; 37 }; 38 39 Complex::Complex() { 40 real = 0; 41 imag = 0; 42 } 43 Complex::Complex(double a, double b) { 44 real = a; 45 imag = b; 46 } 47 Complex::Complex(double a) { 48 real = a; 49 imag = 0; 50 } 51 Complex::Complex(const Complex& obj):real{obj.real},imag{obj.imag}{} 52 53 void Complex::show() const { 54 if (imag > 0) { 55 cout << real << "+" << imag << "i" << endl; 56 } 57 else 58 if (imag == 0) { 59 cout << real << endl; 60 } 61 else 62 cout << real << imag << "i" << endl; 63 } 64 65 void Complex::add(Complex c2){ 66 real += c2.real; 67 imag += c2.imag; 68 } 69 70 71 72 Complex add(Complex c1, Complex c2) { 73 Complex c4; 74 c4.real = c1.real + c2.real; 75 c4.imag = c1.imag + c2.imag; 76 return c4; 77 } 78 79 bool is_equal(Complex c1, Complex c2) { 80 if (c1.real == c2.real && c1.imag == c2.imag) 81 return true; 82 else 83 return false; 84 } 85 86 double abs(Complex c1) { 87 return sqrt(c1.real * c1.real + c1.imag * c1.imag); 88 }
已将测试数据改为:
c1(6,-8); c2(9.0);

实验任务5
User.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using std::cout; 6 using std::endl; 7 using std::string; 8 using std::cin; 9 10 class User { 11 public: 12 User(string mz); 13 User(string mz, string mm, string yx); 14 ~User() = default; 15 16 //成员函数 17 void set_email(); 18 void change_passwd(); 19 void print_info(); 20 static void print_n(); 21 22 private: 23 string name; 24 string passwd; 25 string email; 26 static int count; //静态数据成员声明 27 }; 28 29 int User::count = 0; //静态数据成员定义和初始化,使用类名限定 30 31 void User::print_n() { cout << "There are " << count << " Users." << endl; } 32 33 34 User::User(string mz) :name{ mz }, passwd{ "111111" }, email{""} { count++; } 35 36 User::User(string mz, string mm, string yx) :name{ mz }, passwd{ mm }, email{ yx } { count++; } 37 38 void User::set_email() { 39 string address; 40 cout << "enter email address:"; 41 cin >> address; 42 email = address; 43 cout << "email is set successfully..." << endl; 44 } 45 void User::change_passwd() { 46 string old_passwd; 47 string new_passwd; 48 int t = 1; 49 cout << "enter old passwd:"; 50 cin >> old_passwd; 51 do { 52 if (passwd == old_passwd) { 53 cout << "enter new passwd:"; 54 cin >> new_passwd; 55 passwd = new_passwd; 56 break; 57 } 58 else 59 { 60 t++; 61 cout << "passwd input error.Please re-enter again:"; 62 cin >> old_passwd; 63 } 64 } while (t < 3); 65 if (t == 3) { 66 cout << "passwd enter error.Please try after a while." << endl; 67 } 68 } 69 void User::print_info() { 70 int length = passwd.length(); 71 cout << "name: " << name << endl; 72 cout << "passwd: "; 73 while (length--) { 74 cout << "*"; 75 } 76 cout << endl; 77 cout << "email: " << email << endl; 78 }
task5.cpp
1 #include "User.hpp" 2 #include <iostream> 3 4 // 测试User类 5 void test() { 6 using std::cout; 7 using std::endl; 8 9 cout << "testing 1......\n"; 10 User user1("Jonny", "92197", "xyz@hotmail.com"); 11 user1.print_info(); 12 13 cout << endl 14 << "testing 2......\n\n"; 15 16 User user2("Leonard"); 17 user2.change_passwd(); 18 user2.set_email(); 19 user2.print_info(); 20 User::print_n(); 21 cout << endl; 22 23 } 24 25 int main() { 26 test(); 27 }

实验结论:
1、在编写代码的时候有意识的使用了hpp以及cpp的结构,使得结构更清晰明了
2、了解了count 这一static变量的使用方法
3、string s0{5,‘*’}在创建字符串时总是报错,,,不知道为什么

浙公网安备 33010602011771号