实验一(类与对象)
Complex.hpp
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double r = 0.0, double i = 0.0) :real(r), imag(i) {} Complex(const Complex& p) { real = p.real;imag = p.imag; } double get_real() const{ return real; } double get_imag() const{ return imag; } void show() const; void add(const Complex &p); friend Complex add(Complex p, Complex q); friend bool is_equal(const Complex &p, const Complex &q); friend double abs(Complex p); private: double real, imag; }; void Complex::show() const { if (get_imag() > 0) cout << get_real() << '+' << get_imag() << 'i'; else if (get_imag() == 0) cout << get_real(); else cout << get_real() << get_imag() << 'i'; } void Complex::add(const Complex &p){ real += p.get_real(); imag += p.get_imag(); } Complex add(Complex p, Complex q) { Complex T; T.real = p.real + q.real; T.imag = p.imag + q.imag; return T; } bool is_equal(const Complex &p, const Complex &q) { if (p.get_real() == q.get_real() && p.get_imag() == q.get_imag()) return true; else return false; } double abs(Complex p) { return sqrt(p.real*p.real+p.imag*p.imag); }
task3.cpp
#include"Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(-3, -4); const Complex c2(1.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; }

User.hpp
#include<iostream> #include<string.h> using namespace std; class User { public: User(string mingzi, string mima = "111111", string youjian = "") :name(mingzi), passward(mima), email(youjian) { ++n; } void set_email(); void change_passward(); void print_info(); static void print_n() { cout <<"There are "<< n<<" users"<<endl; } private: string name; string passward; string 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_passward() { string mima; cout << "Enter old passward:" ; int i; for (i = 0;i < 3;i++) { cin >> mima; if (mima == passward) { cout << "Enter new passward:"; cin >> passward; cout << "Newpassward is set succesfully..." << endl; break; } else if (mima != passward && i <= 1) { cout << "passward input error,please enter again:"; } else cout << "passward input error,please try after a while." << endl; } } void User::print_info() { cout << "name:" << name << endl; cout << "passward:"<<"******" << endl; cout << "email:" << email << endl; }
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_passward(); user2.set_email(); user2.print_info(); User::print_n(); }


实验总结:
重载报错没有合适的复制构造函数:在复制构造函数作为参数传进去的对象前加const

浙公网安备 33010602011771号