#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <iomanip> #include <string> #include <cmath> using namespace std; class Complex{ public: Complex(){} Complex(double real_):real(real_),imag(0){} Complex(double real_, double imag_):real(real_),imag(imag_){} Complex(const Complex &complex):real(complex.real),imag(complex.imag){} 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; } else{ cout << real << " - " << -1*imag << "i"; } } void add(Complex complex){ real = real + complex.real; imag = imag + complex.imag; } friend Complex add(const Complex &complex1, const Complex &complex2){ Complex complex; complex.real = complex1.get_real() + complex2.get_real(); complex.imag = complex1.get_imag() + complex2.get_imag(); return complex; } friend bool is_equal(const Complex &complex1, const Complex &complex2){ if(complex1.real==complex2.real && complex1.imag==complex2.imag){ return true; } else{ return false; } } friend double abs(const Complex &complex){ return sqrt(complex.real*complex.real+complex.imag*complex.imag); } private: double real; double imag; }; #endif
#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; }

#ifndef USER_HPP #define USER_HPP #include <iostream> #include <iomanip> #include <string> using namespace std; class User { public: User(string name_); User(string name_, string passwd_, string email_); void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name; string passwd; string email; static int count; }; int User::count = 0; User::User(string name_): name{name_}, passwd{"111111"}, email{""} { ++count; } User::User(string name_, string passwd_, string email_): name{name_}, passwd{passwd_}, email{email_} { ++count; } void User::set_email() { cout << "Enter email address:"; cin >> email; cout << "email is set successfully..." << endl; } void User::change_passwd() { string passwd_; int flag=0; int countdown = 2; cout << "Enter old password:"; cin >> passwd_; if(passwd==passwd_){ flag=1; } while(flag==0&&countdown>0){ countdown--; cout << "password input error. Please re-enter again:"; cin >> passwd_; if(passwd==passwd_){ flag=1; break; } } if(flag==1){ cout << "Enter new passwd:"; cin >> passwd; cout << "newpasswd is set successfully..." << endl; } else if(flag==0){ cout << "password input error. Please try after a while." << endl; } } void User::print_info() { cout << "name:\t" << name << endl; cout << "passwd:\t" << "******" << endl; cout << "email:\t" << email <<endl; } void User::print_n() { cout << "there are " << count << " users.\n"; } #endif
#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号