实验任务4:
Complex.hpp:
#pragma once #include<iostream> #include<string> #include<iomanip> #include<math.h> using namespace std; class Complex { public: Complex(); Complex(double x); Complex(double x,double y); Complex(const Complex&c); double get_real () const {return shibu;} double get_imag()const {return xubu;} void show() const; void add(const Complex&c); private: double shibu,xubu; friend Complex add(const Complex&c1, const Complex&c2); friend bool is_equal(const Complex&c1, const Complex&c2); friend double abs(const Complex&c1); }; Complex::Complex():shibu{0},xubu{0}{} Complex::Complex(double x):shibu{x},xubu{0}{} Complex::Complex(double x,double y):shibu{x},xubu{y}{} Complex::Complex(const Complex&c):shibu{c.shibu},xubu{c.xubu}{} void Complex::show() const { if(xubu>0) cout<<shibu<<" + "<<xubu<<"i"; else if(xubu<0) cout<<shibu<<" "<<xubu<<"i"; else cout<<shibu; } void Complex::add(const Complex&c) { shibu+=c.shibu; xubu+=c.xubu; } Complex add(const Complex&c1, const Complex&c2) { Complex c; c.shibu=c1.shibu+c2.shibu; c.xubu=c1.xubu+c2.xubu; return c; } bool is_equal(const Complex&c1, const Complex&c2) { return c1.shibu==c2.shibu&&c1.xubu==c2.xubu; } double abs(const Complex&c1) { return sqrt(c1.shibu*c1.shibu+c1.xubu*c1.xubu); }
test4:
#include "Complex.hpp" #include <iostream> void test() { using namespace std; Complex c1(5, -3); const Complex c2(2.3); 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:
User.hpp:
#include<iostream> #pragma once #include<cstring> #include<string.h> using namespace std; class User { public: User(string x,string y="111111",string z=""): name{x}, passwd{y}, email{z} {count++;} void set_email(); void change_passwd(); void print_info(); void static print_n() { cout<<endl<<"there are "<<count<<" users"<<endl; } private: string name; string passwd; string email; static int count; }; int User::count=0; void User::set_email() { cout<<"Enter email address: "; cin>>email; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { int k=1; string a; cout<<"Enter old password: "; while(cin>>a) { if(k==3) { cout<<"password input error, Please try after a while"<<endl; break; } if(a!=passwd) { k++; cout<<"password input error, Please re-enter again: "; continue; } if(a==passwd) { cout<<"Enter new password: "; cin>>passwd; cout<<"new password is set successfully..."<<endl; break; } } } void User::print_info() { cout<<"Name: "<<name<<endl; string k=passwd; cout<<"Passwd: "; string password(passwd.length(),'*'); cout<<"password: "<<password<<endl; cout << endl; cout << "email: " << email << endl; }
test5:
#include "User.hpp" #include <iostream> 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("RNGXiaohu","2200"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
初始运行截图:
更改数据截图:
标签:const,cout,double,void,Complex,实验,include From: https://www.cnblogs.com/202183290359gyk/p/16793956.html