实验一 类与对象
实验任务3
"Complex.hpp"
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <bits/stdc++.h> using namespace std; class Complex { public: Complex(double a=0.0, double b=0.0):real(a),image(b){} Complex(const Complex& c): real(c.real),image(c.image){} double get_real() const{ return real;} //返回real double get_imag() const{ return image;} //返回虚部 void show(); //输出复数 void show() const; void add(Complex c2){ real += c2.get_real(); image += c2.get_imag();} //用于把一个复数加到自己身上 friend Complex add(Complex const &c1, Complex const &c2); //实现两个复数相加 friend string is_equal(Complex const &c1, Complex const &c2); //判断两个复数是否相等 friend double abs(Complex c1); //取模运算 private: double real, image; }; void Complex::show() //输出复数 { cout << real <<" "; if(image > 0.0) cout << "+ " <<fabs(image) <<"i"; else if(image < 0.0) cout << "- " <<fabs(image) <<"i"; } void Complex::show() const //输出复数 { cout << real <<" "; if(image > 0.0) cout << "+ " <<fabs(image) <<"i"; else if(image < 0.0) cout << "- " <<fabs(image) <<"i"; } Complex add(Complex const &c1, Complex const &c2) //实现两个复数相加 { Complex res(c1.real+c2.real, c1.image+c2.image); return res; } string is_equal(Complex const &c1, Complex const &c2) //判断两个复数是否相等 { string res; if(c1.real==c2.real&&c1.image==c2.image) res = "true"; else res = "false"; return res; } double abs(Complex c1) //取模运算 { double res; res = sqrt(pow(c1.get_real(),2) + pow(c1.get_imag(),2)); return res; }
"task3.cpp"
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(5, -12); 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.cpp"
#ifndef USER_HPP #define USER_HPP #include <bits/stdc++.h> using namespace std; class User { public: User(){n++;} User(string na, string pass = "111111", string em = ""):name(na), passwd(pass), email(em){ n++; } void set_email(); //设置邮箱 void change_passwd(); //修改密码 void print_info(); //打印数据 static void print_n(); //打印用户数 private: string name; //姓名 string passwd; //密码 string email; //邮箱 static int n; //用户数 }; int User::n = 0; void User::set_email() //设置邮箱 { cout << "Enter email addres: "; string temp; cin >> temp; email = temp; cout << "Eamil is set successfully..." <<endl; } void User::change_passwd() //修改密码 { cout << "Enter old password: "; int count = 0; string temp; do { cin >> temp; if( temp == passwd) { string temp2; cout << "Enter new password: "; cin >> temp2; passwd = temp2; cout << "New password is set successfully..." << endl; return; } else { cout << "Passsword input error. Please re-enter again: "; } count ++; }while(temp != passwd && count <= 2); cout << "Password input erroe. Please try after a while."<<endl; return ; } void User::print_info() //打印数据 { cout << "name: " << name << endl; cout << "passord: " <<"******" <<endl; cout << "email: " << email << endl; } void User::print_n() //打印用户数 { if(n==0) cout << "there is no users."; else if (n==1) cout << "there is 1 user."; else cout << "there are " << n << " users."; cout << endl; } #endif
"task4.cpp"
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Handsome_LSY", "163", "Shuaige@gmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("WuYanZu"); user2.change_passwd(); user2.set_email(); cout <<endl; user2.print_info(); User::print_n(); }
运行结果1:
运行结果2: