实验一 类与对象
实验任务三
//Complex.hpp #include<iostream> #include<cmath> using namespace std; class Complex { public: Complex() : real{0}, imag{0} {}; Complex(double r, double i = 0.0) : real{r}, imag{i} {}; Complex(const Complex &obj) : real{obj.real}, imag{obj.imag} {}; double get_real() const; double get_imag() const; void show() const; void add(const Complex &x); friend Complex add(const Complex &a, const Complex &b); friend bool is_equal(const Complex &a, const Complex &b); friend double abs(const Complex &x); private: double real, 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 if(imag < 0) cout << real << " - " << imag*(-1) << "i"; else cout << real; } void Complex::add(const Complex &x) { real += x.real; imag += x.imag; } Complex add(const Complex &a, const Complex &b) { Complex ans; ans.real = a.real + b.real; ans.imag = a.imag + b.imag; return ans; } bool is_equal(const Complex &a, const Complex &b) { if(a.real == b.real && a.imag == b.imag) return true; else return false; } double abs(const Complex &x) { double ans = x.real*x.real + x.imag*x.imag; ans = sqrt(ans); return ans; }
//task3.cpp #include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(2.3, -4.9); const Complex c2(5.4); 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 #include<iostream> #include<string> using namespace std; class User { public: User(string name, string p = "111111", string e = ""); void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name, passwd, email; static int n; }; int User::n = 0; User::User(string name, string p/*="111111"*/, string e/*=""*/) : name{name}, passwd{p}, email{e} {n++;} void User::set_email() { cout << "Enter email address: "; cin >> email; cout << "email is set successfully..." << endl; } void User::change_passwd() { string temp; cout << "Enter old password: "; int count = 3; while(count) { cin >> temp; if(temp == passwd) { cout << "Enter new passwd: "; cin >> passwd; cout << "new passwd is set successfully..." << endl; break; } else { count--; if(count != 0) cout << "password input error. Please re-enter again: "; } } if(count == 0) cout << "password input error. Please try after a while." << endl; } 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; }
//task4.cpp #include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Bob", "114514", "hotdog@gmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Alice"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
实验总结:
1.函数成员如果没有修改数据成员的需求,可加const来保护数据成员,使程序更加安全
2.访问静态成员需要通过静态函数成员