#ifndef COMPLEX_HPP #define COMPLEX_HPP #include<iostream> #include<cmath> using namespace std; class Complex { private: double real; double imag; public: Complex(); Complex(double real0); Complex(double real0,double imag0); Complex(const Complex &obj); double get_real()const; double get_imag()const; void show()const; void add(const Complex &obj); friend Complex add(Complex obj1,Complex obj2); friend bool is_equal(const Complex &obj1,const Complex &obj2); friend double abs(const Complex &obj); }; Complex::Complex() { real=imag=0; } Complex::Complex(double real0) { real=real0; imag=0; } Complex::Complex(double real0,double imag0) { real=real0; imag=imag0; } Complex::Complex(const Complex &obj) { real=obj.real; imag=obj.imag; } double Complex::get_real()const { return real; } double Complex::get_imag()const { return imag; } void Complex::show()const { if(imag!=0) cout<<real<<imag<<"i"; else cout<<real; } void Complex::add(const Complex &obj) { real=real+obj.real; imag=imag+obj.imag; } Complex add(Complex obj1,Complex obj2) { obj1.real=obj1.real+obj2.real; obj1.imag=obj1.imag+obj2.imag; return obj1; } bool is_equal(const Complex &obj1,const Complex &obj2) { if(obj1.real==obj2.real&&obj1.imag==obj2.imag) return true; else return false; } double abs(const Complex &obj) { return sqrt(pow(obj.real,2)+pow(obj.imag,2)); }
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; }
#ifndef USER_HPP #define USER_HPP #include<iostream> #include<string> using namespace std; class User { public: User(string name0,string passwd0="111111",string email0=""):name(name0),passwd(passwd0),email(email0){n++;} void set_email(); void change_passwd(); void print_info(); static void print_n(); ~User(){} private: string name,passwd,email; static int n; }; int User::n=0; void User::set_email() { cout<<"Enter email address: "; cin>>email; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { string oldpasswd; int j=0; cout<<"Enter old password: "; cin>>oldpasswd; if (oldpasswd == passwd) { cout << "Enter new passwd: "; cin >> passwd; } else { do { j++; cout<<"password input error. Please re-enter again: "; cin>>oldpasswd; if(j==2) { cout<<"password input error. Please try after a while."<<endl; break; } }while(oldpasswd!=passwd); if(oldpasswd==passwd) cin>>passwd; } } void User::print_info() { cout<<"name: "<<name<<endl; cout<<"passwd: ******"<<endl; cout<<"email: "<<email<<endl; } void User::print_n() { cout<<"there are "<<n<<" users."<<endl; } #endif
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(); }