实验一:类与对象
实验3:
Complex.hpp:
#ifndef Complex_hp #define Complex_hp #include <iostream> #include <cmath> using namespace std; class Complex { public: Complex() = default; Complex(double a, double b = 0) :real(a), image(b) {} Complex(const Complex& tag) :real(tag.real), image(tag.image) {} ~Complex() {} double get_real()const; double get_imag()const; void show()const; void add(const Complex& k); friend Complex add(const Complex& k1, const Complex& k2); friend bool is_equal(const Complex& k1, const Complex& k2); friend double abs(const Complex& k); private: double real; double image; }; double Complex::get_real()const { return real; } double Complex::get_imag()const { return image; } void Complex::show()const { if (image > 0) { cout << real << '+' << image << 'i' << endl; } else if (image < 0) { cout << real << image << 'i' << endl; } else { cout << real << endl; } } void Complex::add(const Complex& k) { real = real + k.real; image = image + k.image; } Complex add(const Complex& k1, const Complex& k2) { Complex k3; k3.real = k1.real + k2.real; k3.image = k1.image + k2.image; return k3; } bool is_equal(const Complex& k1, const Complex& k2) { if (k1.real == k2.real && k1.image == k2.image) return true; else return false; } double abs(const Complex& k) { return sqrt(k.real * k.real + k.image * k.image); } #endif //
task3.cpp:
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -8); 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; }
运行结果:
实验4:
User.hpp:
#ifndef User_hpp #define User_hpp #include <iostream> #include <string> using namespace std; class User { User(string s1, string s2 = "111111", string s3 = " ") : name(s1), password(s2), email(s3) { n++; } ~User() {} void set_email() { string s4; cout << "Enter email address:"; cin >> s4; cout << endl; cout << "email is set successfully" << endl; } void change_passwd() { string s4,s5; int m=1; cout << "Enter old password:"; cin >> s4; while (s4 != password && m < 3) { cout << "password input error.Please re-enter again:"; cin >>s4; m++; } if (m == 3) { cout << "password input error.Please try after a while." << endl; } if (s4 == password && m < 3) { cout << "enter newpassword:"; cin >> s5; } } void print_info() { cout << "name:"<<name<<endl; cout << "password:******"<<endl; cout << "email:" << email << endl; } static void print_n() { if(n==1) { cout << "there is" << n << "user" << endl; } else if (n==0) { cout<<"there is no user"<<endl; } else { cout << "there are" << n << "users" << endl; } } private: static int n; string name; string password; string email; }; int User::n=0; #endif
task4.cpp:
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Bill Gates", "114514", "appleissobad@outlook.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Tim Cook"); user2.change_passwd(); user2.set_email(); cout << endl; user2.print_info(); User::print_n(); }
运行结果: