实验一
Task3: Complex.hpp
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double m_real=0,double m_imag=0):real(m_real),imag(m_imag){ } Complex(const Complex &c){ real = c.real; imag = c.imag; } double get_real() const;//成员函数 double get_imag() const; void show() const; void add(const Complex &obj) { real+=obj.real; imag+=obj.imag; } friend double abs(const Complex &c1);//友元函数 friend bool is_equal( const Complex &c1, const Complex &c2); friend Complex add(const Complex &c1, const Complex &c2); ~Complex()=default; private: double real,imag; }; double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } void Complex::show() const{ if(real!=0){ if(imag){//判断imag是否为零 if(imag>0) cout << real << "+" << imag << "i"; else{ cout << real << imag << "i"; } } else cout << real; } else{ if(imag){//判断imag是否为零 if(imag>0) cout << real << "+" << imag << "i"; else{ cout << real << imag << "i"; } } else cout << real; } } Complex add(const Complex &c1, const Complex &c2){ Complex c3; c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; return c3; } double abs(const Complex &c1){ return sqrt(c1.real*c1.real+c1.imag*c1.imag); } bool is_equal(const Complex &c1,const Complex &c2){ if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false; }
task3.cpp
#include "Complex.hpp" #include <iostream> int main() { 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; }

task4: User.hpp
#include<iostream> #include<string> using namespace std; class User{ private: string m_name,m_password,m_email; static int n; public: User(){ n++; } User(string name,string password = "111111",string email = "") :m_name(name),m_password(password),m_email(email){n++;} void set_email() ; void change_passwd() ; void print_info() ; static void print_n(); }; int User::n = 0; void User::print_n(){ cout << "there are " << n << " users." << endl; } void User::set_email(){ cout << "Enter email address: " << endl ; cin >> m_email ; cout << "email is set successfully..."; } void User::change_passwd() { cout << "Enter old password: "; string t1; int num=0; string &t2 = m_password;//引用,给成员密码取别名 cin >> t1; for(int i=0;i<3;i++){ if(t1 == m_password){ cout << "Enter new passwd: "; cin >> t2 ; cout << "new passwd is set successfully..." << endl; return ; } else if(i!=2){ cout << "password input error. Please re-enter again: "; cin >> t1 ; } else{ cout << "password input error. Please try after a while. " << endl; return; } } } void User::print_info() { cout << "name: " << m_name << endl; cout << "passwd: ******" << endl; cout << "email: " << m_email << endl; }
task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }



浙公网安备 33010602011771号