task3:
Complex.hpp源码:
#ifndef Complex_hpp #define Complex_hpp #include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(double a, double b = 0) : real { a }, imag { b }{} Complex(Complex const& ci) : real { ci.real }, imag { ci.imag }{} Complex() = default; double get_real()const { return real; } double get_imag()const { return imag; } void show()const { if ( imag > 0 ) { cout << real << " + " << imag << "i"; } else if ( imag < 0 ) { cout << real << " - " << -imag << "i"; } else { cout << real; } } void add(Complex const& ci) { real += ci.real; imag += ci.imag; } friend Complex add(Complex const& c1, Complex const& c2); friend bool is_equal(Complex const& c1, Complex const& c2); friend double abs(Complex const& ci); private: double real; double imag; }; Complex add(Complex const& c1, Complex const& c2) { Complex c3(0, 0); c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; return c3; } bool is_equal(Complex const& c1, Complex const& c2) { if ( c1.real == c2.real && c1.imag == c2.imag ) { return true; } else { return false; } } double abs(Complex const& ci) { double res = sqrt(ci.real * ci.real + ci.imag * ci.imag); return res; } #endif
Complex.cpp源码:
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -8); const Complex c2(4.2); 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; }
测试结果:
................................................
task4:
User.hpp源码:
#ifndef User_hpp #define User_hpp #include <iostream> #include <string> using namespace std; const int N = 1e3; class User { public: User(string na, string p = "111111", string e = "") :name { na }, passwd { p }, email { e } { n++; } void set_email() { cout << "Enter email address: "; cin >> email; } void change_passwd() { string tmp; int cnt = 0; cout << "Enter old password: "; cin >> tmp; while (1) { if ( tmp == passwd ) { cout << "Enter new passwd: "; cin >> tmp; cout << "new passwd is set successfully..." << endl; passwd = tmp; break; } else { cnt++; if ( cnt == 3 ) { cout << "password input error. Please try after a while." << endl; break; } cout << "password input error. Please re-enter again: "; cin >> tmp; } } } void print_info() { cout << "name: " << name << endl; cout << "passwd: ******" << endl; cout << "email: " << email << endl; } static void print_n() { cout << "there are " << n << " users." << endl; } private: string name; string passwd; string email; static int n; }; int User::n = 0; #endif
User.cpp源码:
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Rom", "114514514114", "Rom@163.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Oier"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
测试结果:
实验总结:
比较深刻的理解了类与对象的运用。通过实验巩固了对const的理解并且学会了通过static来定义类属性即通过类名就可以访问该成员。理解了友元的应用,通过友元可以直接访问类的私有成员这一特性,可以更方便地实现一些操作。