一、实验内容:
实验任务4:
1.Complex.hpp:
#include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(double x = 0, double y = 0) : real{x}, imag{y} {} ~Complex() {} Complex(const Complex &m) : real{m.real}, imag{m.imag} {} double get_real() { return real; } double get_imag() const { return imag; } void show() const { if (imag > 0) cout << real << "+" << imag << "i" << endl; if (imag == 0) cout << real << endl; if (imag < 0) cout << real << imag << "i" << endl; } Complex add(const Complex &n) { real = n.real + real; imag = n.imag + imag; return Complex(real, imag); } friend Complex add(const Complex &p, const Complex &q) { double real = p.real + q.real; double imag = p.imag + q.imag; Complex r; r.real = real; r.imag = imag; return r; } friend bool is_equal(Complex &p, const Complex &q) { if ((p.real != q.real) || (p.imag != q.imag)) return false; else return true; } friend double abs(Complex &h) { return sqrt(h.real * h.real + h.imag * h.imag); } private: double real, imag; };
2.测试cpp:
#include <iostream> #include "task4 Complex.hpp" // 类测试 void test() { using namespace std; Complex c1(3, -4); const Complex c2(4.5); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; } int main() { test(); }
运行结果:
3.测试cpp(更换数据后):
#include <iostream> #include "task4 Complex.hpp" // 类测试 void test() { using namespace std; Complex c1(7, -1); const Complex c2(-2.5); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; } int main() { test(); }
运行结果:
实验任务5:
1.User.hpp:
// User.hpp #include <iostream> #include <string> #include <iomanip> using namespace std; class User { public: User(string x, string y = "111111", string z = "") : name(x), passwd(y), email(z) { n++; } ~User() { n--; } void set_email() { string m; cout << "Enter email address:"; cin >> m; cout << "email is set successfully..." << endl; email = m; } void change_passwd() { string a; int i; cout << "Enter old passwd:"; cin >> a; for (i = 1; i < 3 && a.compare(passwd) != 0; i++) { cout << "passwd input error.Please re-enter again:"; cin >> a; } if (a.compare(passwd) != 0)
cout << "passwd input error.Please try after a while." << endl; else { cout << "Enter new passwd:"; cin >> a; cout << "new passwd is set successfully..." << endl; passwd = a; } } void print_info() { int length; length = passwd.length(); cout << std::left << setw(8) << "name:" << name << endl; cout << std::left << setw(8) << "passwd:"; for (int i = 1; i <= length; i++) cout << "*"; cout << endl; cout << std::left << setw(8) << "email:" << email << endl; } static void print_n() { cout << "there are " << n << " users." << endl; } private: string name, passwd, email; static int n; }; int User::n = 0;
2.测试cpp:
#include "task5 User.hpp" #include <iostream> // 测试User类 void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
运行结果:
二、实验总结:
1.实验任务4的Complex初次撰写时,未注意到c2是一个const修饰的类,导致在测试代码时只要c2运用到的类成员函数都发生了错误。在之后,我修改了类Complex中有关成员函数,加上了const修饰,问题迎刃而解。
2.实验任务5的User撰写时,判断密码输入对错的循环体我书写错误,一开始以(i < 3 && a.compare(passwd) != 0)判断条件并且以if(i == 3)判断跳出位置,最后测试证明 此代码下即使第三次密码输入正确也会显示“try after a while”,最后索性用if( a.compare(passwd) != 0)判断,成功解决问题。
标签:const,cout,对象,Complex,实验,User,using,include From: https://www.cnblogs.com/jyksblog/p/16796386.html