实验任务3
Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(); Complex(double re); Complex(double re,double im); Complex(const Complex &C); double get_real() const{return real;} double get_imag() const{return imag;} void show() const; void add(const Complex &x); friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(Complex c1,Complex c2); friend double abs(Complex c); private: double real; double imag; }; Complex::Complex():real(0),imag(0){} Complex::Complex(double re):real(re),imag(0){} Complex::Complex(double re,double im):real(re),imag(im){} Complex::Complex(const Complex &c) { real=c.real; imag=c.imag; } void Complex::show() const { if(imag>=0) cout<<real<<"+"<<imag<<"i"<<endl; if(imag<0) cout<<real<<imag<<"i"<<endl; } void Complex::add(const Complex &x) { real+=x.real; imag+=x.imag; } Complex add(const Complex &c1,const Complex &c2) { Complex c3; c3.real=c1.real+c2.real; c3.imag=c1.imag+c2.imag; return c3; } bool is_equal(Complex c1,Complex c2) { if(c1.real==c2.real&&c1.imag==c2.imag) return true; else return false; } double abs(Complex c) { return sqrt(c.real*c.real+c.imag*c.imag); } #endif
task3.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -7); const Complex c2(2.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; }
测试结果截图
实验任务4
User.hpp
#ifndef USER_HPP #define USER_HPP #include<iostream> #include<string> using namespace std; class User { public: User(string name0); User(string name0,string passwd0,string email0); User(User &u); 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 name0):name(name0),passwd("111111"),email(""){++count;} User::User(string name0,string passwd0,string email0):name(name0),passwd(passwd0),email(email0) { ++count; } User::User(User &u):name(u.name),passwd(u.passwd),email(u.email) { ++count; } void User::set_email() { string email0; cout<<"Enter email address:"; cin>>email0; email=email0; cout<<"email is set successfully..."<<endl; } void User::change_passwd() { string pass; int i=1; cout<<"Enter old password:"; cin>>pass; while(pass!=passwd&&i<3) { cout<<"password input error.Please re-enter again:"; cin>>pass; i++; } if(pass!=passwd&&i==3) { cout<<"password input error.Please try after a while."<<endl; } if(pass==passwd) { cout<<"Enter new passwd:"; cin>>pass; cout<<"new passwd is set successfully..."<<endl; } } void User::print_info() { cout<<"name:"<<name<<endl; cout<<"passwd:******"<<endl; cout<<"email:"<<email<<endl; } void User::print_n() { cout<<"there are "<<count<<"users."<<endl; } #endif
task4.cpp
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "111222", "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(); }
测试结果截图