实验二
task 4
Complex.hpp
#pragma once #include <iostream> #include <cmath> using namespace std; class Complex { friend Complex add(const Complex &c1, const Complex &c2); friend bool is_equal(const Complex &c1, const Complex &c2); friend float abs(const Complex &c); public: // 构造函数 Complex() : real(0), imag(0) {} Complex (float r) : real(r), imag(0) {} Complex (float r, double i) : real(r), imag(i) {} Complex (const Complex &c){ this->real = c.real; this->imag = c.imag; } float get_real() const{ return real; } float get_imag() const{ return imag; } void show() const{ float i = fabs(imag); if(imag >= 0){ cout << real << " + " << i << 'i' << endl; }else { cout << real << " - " << i << 'i' << endl; } } //用于输出复数。要求以 3 + 4i , 3 - 4i 这样的形式输出 void add(const Complex &c){ real += c.real; imag += c.imag; } public: float real; float imag; }; Complex add(const Complex &c1,const Complex&c2){ Complex c3; c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; return c3; } bool is_equal(const Complex &c1,const Complex&c2){ return c1.real == c2.real && c1.imag == c2.imag; } float abs(const Complex &c){ return sqrt(c.real * c.real + c.imag * c.imag); }
.cpp
#include "Complex.hpp" #include <iostream> // 类测试 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(); }

task 5
.hpp
#pragma once #include <iostream> #include <string> using namespace std; class User { public: User (string name) : name(name), passwd("111111"),email(""){ n++; } User(string name,string pwd,string email) :name(name), passwd(pwd),email(email) { n++; } void set_email(){ cout << "Enter email address:" << endl; string e; cin >> e; email = e; cout << "emai is set successfully..."; } void change_passwd(){ int count = 0; string pwd; cout << "Enter old password:"; cin >> pwd; while(pwd!=passwd){ if(count==2){ cout << "password input error.Please try after a while." << endl; break; } cout << "password input error.Please re-enter again: "; cin >> pwd; count++; } } void print_info(){ cout << "name: " << name << endl; cout << "passwd: "; for (int i = 0; i < passwd.length(); i++) cout << '*'; cout << endl; cout << "email: " << email << endl; } void static print_n(){ cout<<"there are "<<n<<" users"<<endl; } public: string name, passwd, email; static int n; }; int User::n = 0;
.cpp
#include "User.hpp" #include <iostream> // 测试User类 void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "xyz@hotmail.com"); 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(); }

小结:task5 静态成员n 类内声明 类外实现 : int User::n=0;

浙公网安备 33010602011771号