实验一 类与对象
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 //complex类定义 5 #include<iostream> 6 #include<math.h> 7 using namespace std; 8 class Complex 9 { 10 public: 11 12 Complex(); 13 Complex(double x); 14 Complex(double a, double b); 15 Complex(const Complex &p); 16 17 double get_real()const; 18 double get_imag()const ; 19 void show() const; 20 void add(const Complex &c2); 21 22 //友元函数 23 friend Complex add(const Complex &c1, const Complex &c2); 24 friend bool is_equal(const Complex &c1, const Complex &c2); 25 friend double abs(const Complex &c1); 26 27 private: 28 double m_real; 29 double m_imag; 30 }; 31 32 Complex::Complex() {} 33 34 Complex::Complex(double x): m_real{x}, m_imag{0} {} 35 36 Complex::Complex(double a, double b): m_real{a}, m_imag{b} {} 37 38 Complex::Complex(const Complex &p): m_real{p.m_real}, m_imag{p.m_imag} {} 39 40 //返回复数实部 41 double Complex::get_real() const 42 { 43 return m_real; 44 } 45 46 //返回复数虚部 47 double Complex::get_imag() const 48 { 49 return m_imag; 50 } 51 52 //输出复数 53 void Complex::show() const 54 { 55 if(m_real < 0 && m_imag < 0) 56 cout << "-" << " " <<fabs(m_real )<< " " << "-" << " " << fabs(m_imag )<< "i"; 57 else if(m_real < 0 && m_imag > 0) 58 cout << "-" << " " << fabs(m_real )<< " " <<"+" << " " << fabs(m_imag )<< "i"; 59 else if(m_real < 0 && m_imag == 0) 60 cout << "-" << " " << fabs(m_real); 61 else if(m_real > 0 && m_imag < 0) 62 cout <<fabs(m_real )<< " " << "-" << " " << fabs(m_imag )<< "i"; 63 else if(m_real > 0 && m_imag > 0) 64 cout << fabs(m_real )<< " " <<"+" << " " << fabs(m_imag )<< "i"; 65 else if(m_real > 0 && m_imag == 0) 66 cout << fabs(m_real); 67 else if(m_real == 0 && m_imag < 0) 68 cout << "-" << " " << fabs(m_imag )<< "i"; 69 else if(m_real == 0 && m_imag > 0) 70 cout << fabs(m_imag )<< "i"; 71 else if(m_real == 0 && m_imag == 0) 72 cout << "0"; 73 74 } 75 76 //复数加到自身 77 void Complex::add(const Complex &c1) 78 { 79 m_real += c1.m_real ; 80 m_imag += c1.m_imag ; 81 } 82 83 //友元函数,复数相加 84 Complex add(const Complex &c1, const Complex &c2) 85 { 86 Complex temp; 87 temp.m_real = c1.m_real + c2.m_real; 88 temp.m_imag = c1.m_imag + c2.m_imag; 89 90 return temp; 91 } 92 93 //判断复数是否相等 94 bool is_equal(const Complex &c1, const Complex &c2) 95 { 96 if(c1.get_imag() == c2.get_imag() && c1.get_real() == c2.get_real() ) 97 return true; 98 else 99 return false; 100 } 101 102 //复数取模 103 double abs(const Complex &c1) 104 { 105 return sqrt(c1.get_imag() *c1.get_imag() + c1.get_real() *c1.get_real() ); 106 } 107 108 #endif
1 #include "Complex.hpp" 2 #include <iostream> 3 4 int main() 5 { 6 using namespace std; 7 8 Complex c1(3, -6); 9 const Complex c2(5.5); 10 Complex c3(c1); 11 12 cout << "c1 = "; 13 c1.show(); 14 cout << endl; 15 16 cout << "c2 = "; 17 c2.show(); 18 cout << endl; 19 cout << "c2.imag = " << c2.get_imag() << endl; 20 21 cout << "c3 = "; 22 c3.show(); 23 cout << endl; 24 25 cout << "abs(c1) = "; 26 cout << abs(c1) << endl; 27 28 cout << boolalpha; 29 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 30 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 31 32 Complex c4; 33 c4 = add(c1, c2); 34 cout << "c4 = c1 + c2 = "; 35 c4.show(); 36 cout << endl; 37 38 c1.add(c2); 39 cout << "c1 += c2, " << "c1 = "; 40 c1.show(); 41 cout << endl; 42 }

实验任务四
# user.cpp
1 #ifndef USER_HPP 2 #define USER_HPP 3 4 #include<iostream> 5 #include<string> 6 using namespace std; 7 8 int cnt = 1; 9 class User 10 { 11 public: 12 User(string name); 13 User(string name, string passwd, string email); 14 15 void set_email(); 16 void change_passwd(); 17 void print_info(); 18 19 static void print_n(); 20 21 private: 22 string m_name; 23 string m_passwd; 24 string m_email; 25 static int n;//记录有几个user 26 }; 27 28 int User::n = 0; 29 30 User::User(string name) : m_name{name}, m_passwd{"111111"}, m_email{""} 31 { 32 n++; 33 } 34 35 User::User(string name, string passwd, string email) :m_name{name}, m_passwd{passwd},m_email{email} 36 { 37 n++; 38 } 39 40 void User::set_email() 41 { 42 string email0; 43 cout << "Enter email sddress: "; 44 cin >> email0; 45 m_email = email0; 46 cout << "email is set successfully..." << endl; 47 } 48 49 void User::change_passwd() 50 { 51 string oldpasswd; 52 string newpasswd; 53 cout << "Enter old password: "; 54 cin >> oldpasswd; 55 while(cnt <= 3) 56 { 57 if(oldpasswd != m_passwd) 58 { 59 cnt++; 60 if(cnt == 4) 61 break; 62 cout << "password input error. Please re-enter again: "; 63 cin >> oldpasswd; 64 65 66 } 67 else break; 68 69 } 70 if(cnt <= 3) 71 { 72 cout << "Enter new passwd: "; 73 cin >> newpasswd; 74 cout << "new passwd is set successfully..." << endl; 75 } 76 else 77 { 78 cout << "password input error. Please try after a while." << endl; 79 } 80 } 81 82 void User::print_info() 83 { 84 cout << "name: " << m_name << endl; 85 cout << "passwd: " << "******" << endl; 86 cout << "email: " << m_email << endl; 87 } 88 89 void User::print_n() 90 { 91 cout << "there are " << n << " users." << endl; 92 } 93 94 #endif
#main.cpp
1 #include "User.hpp" 2 #include <iostream> 3 4 int main() 5 { 6 using namespace std; 7 8 cout << "testing 1......" << endl; 9 User user1("Jonny", "92197", "xyz@hotmail.com"); 10 user1.print_info(); 11 12 cout << endl 13 << "testing 2......" << endl 14 << endl; 15 User user2("Leonard"); 16 user2.change_passwd(); 17 user2.set_email(); 18 user2.print_info(); 19 20 User::print_n(); 21 }



浙公网安备 33010602011771号