实验4
#pragma once #include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double r = 0.0, double i = 0.0) :real(r), imag(i) {} Complex(Complex& t) :real(t.real), imag(t.imag) {} double get_real() const { return real; } double get_imag() const { return imag; } void add(const Complex& t) { real += t.real; imag += t.imag; } void show() const; private: double real, imag; private: friend Complex add(const Complex& p1,const Complex& p2); friend bool is_equal(const Complex& p1, const Complex& p2); friend double abs(const Complex& p); }; void Complex::show() const { if (imag > 0) { cout << real << "+" << imag << "i" ; } else if(imag==0) { cout << real ; } else { cout << real << imag << "i"; } } Complex add(const Complex& t1, const Complex& t2) { Complex t; t.real = t1.real + t2.real; t.imag = t1.imag + t2.imag; return t; } bool is_equal(const Complex& t1, const Complex& t2) { if (t1.real == t2.real && t1.imag == t2.imag) { return true; } else { return false; } } double abs(const Complex& t) { double T = sqrt(t.real * t.real + t.imag * t.imag); return T; }
#include"Complex.h" #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(); }
实验5
#pragma once #include<iostream> #include<string> #include<iomanip> using namespace std; class User { public: User(string name_0); User(string name_0, string passwd_0, string email_0); void print_info(); void change_passwd(); void set_email(); static void print_n(); private: string name; string passwd,password,password_new,passwdNum; string email,email_new; static int count; }; int User::count = 0; User::User(string name_0) :name{ name_0 } { passwd = "111111"; email = ""; ++count; } User::User(string name_0, string passwd_0, string email_0) : name{ name_0 }, passwd{ passwd_0 }, email{ email_0 } { ++count; } void User::print_info() { int number = size(passwd); cout << left << setw(2) << "name: " << name << endl; cout << setw(2) << "passwd: "; while (number--) { cout << '*'; } cout << endl; cout << setw(2) << "email: " << email << endl; } void User::change_passwd() { int count = 0; cout << "Enter old password: "; cin >> password; while (password != passwd) { if (count < 2) { cout << "password input error. Please re-enter again: "; cin >> password; count++; } else { cout << "password input error. Please try after a while." << endl; break; } } if (password == passwd) { cout << "Enter new passwd: "; cin >> password_new; passwd = password_new; cout << "new password is set successfully..." << endl; } } void User::set_email() { cout << "Enter email address: "; cin >> email_new; email = email_new; cout << "email is set sucessfully..." << endl; } void User::print_n() { cout << "there are " << count << " users" << endl; }
#include"User.h" #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("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
标签:const,string,对象,void,passwd,Complex,实验,email From: https://www.cnblogs.com/lyjlyjlyj/p/16796333.html