实验1 类与对象
四、实验结论
1.实验任务1-2
2.实验任务3
- 
Complex.hpp文件源码: #ifndef Complex_hpp #define Complex_hpp #include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(){}; Complex(double x); Complex(double x, double y); Complex(const Complex &c); ~Complex() = default; double get_real() const; double get_imag() const; void show() const; void add(const Complex &c); friend Complex add(const Complex &c1, const Complex &c2); friend bool is_equal(const Complex &c1, const Complex &c2); friend double abs(const Complex &c); private: double real, imag; }; Complex::Complex(double x, double y) : real{x}, imag{y} {} Complex::Complex(double x) : real{x}, imag{0.0} {} Complex::Complex(const Complex &c) : real(c.real), imag(c.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; else cout << real << imag << "i"; } void Complex::add(const Complex &c) { real += c.real; imag += c.imag; } Complex add(const Complex &c1, const Complex &c2) { Complex c; c.imag = c1.imag + c2.imag; c.real = c1.real + c2.real; return c; } bool is_equal(const Complex &c1, const Complex &c2) { if (c1.imag == c2.imag && c1.real == c2.real) return true; else return false; } double abs(const Complex &c) { return sqrt(c.imag * c.imag + c.real * c.real); } #endif
- 
task3.cpp源码 #include "Complex.hpp" #include <iostream> using namespace std; int main() { Complex c1(1, -17); const Complex c2(3.6); 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; }
- 
运行测试结果截图 ![]() 
3.实验任务4
- 
User.hpp文件源码 #ifndef USER_HPP #define USER_HPP #include <iostream> #include <cstring> using namespace std; class User { public: User(string n); User(string n, string p, string e); ~User(){}; void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name, passwd, email; static int num; }; int User::num = 0; User::User(string n) : name{n}, passwd{"111111"}, email{""} { ++num; } User::User(string n, string p, string e) : name{n}, passwd{p}, email{e} { ++num; } void User::set_email() { cout << "Enter email address: "; cin >> email; cout << "email is set successfully..." << endl; } void User::change_passwd() { string oldpsw; int error = 0; cout << "Enter old password:"; cin >> oldpsw; while (oldpsw != passwd) { if ((++error) == 3) break; cout << "password input error. Please re-enter again:"; cin >> oldpsw; } if (error == 3) { cout << "password input error. Please try for a while." << endl; } else { cout << "Enter new password:"; cin >> passwd; 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() { if (num == 1) { cout << "there is 1 user." << endl; } else { cout << "there are " << num << " users." << endl; } } #endif
- 
task4.cpp源码 #include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("wyx117", "01117", "973047895@qq.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("yy"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
- 
运行测试结果截图 ![]() 
五、实验总结
因为很早就接触编程语言、接触算法竞赛,所以C++的基本语法已相当熟练,但是对面向对象的内容,比如类的运用,这块知识是空缺的。通过学习这门课,动手做了这些实验,让我感受到类的方便,尤其对我这种强迫症患者,这种层次分明的结构让我很舒适。
字符串的比较让我犹豫了一下,一开始写的是strcmp(oldpsw,passwd)!=0,报错*不存在从"std::__cxx11::string" 到 "const char " 的适当转换函数,后来我想起来C++中可以直接比较就换成了oldpsw != passwd。
 
                    
                     
                    
                 
                    
                

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号